So I am supposed to find the parity of a number where parity is odd if there are odd number of set bits in the binary representation of the given number and even otherwise. I have a solution but the code is not very understandable and thus am looking for an explanation to it.
int y = x ^ (x >> 1); //given number is x.
y = y ^ (y >> 2);
y = y ^ (y >> 4);
y = y ^ (y >> 8);
y = y ^ (y >> 16);
// Rightmost bit of y holds the parity value
// if (y&1) is 1 then parity is odd else even
if (y & 1)
return odd;
return even;