I have to create a function bitParity(int x) that takes an integer and returns 1 if there is an odd number of 0's in the bit form of x, and 0 otherwise.
Ex: bitParity(5) = 0, bitParity(7) = 1 Legal ops: ! ~ & ^ | + << >>
My buddy shared his code with me, but when I did the calculation from scratch, it seems to get the opposite of what he gets, can someone please explain this code a little bit so I get the idea of how it functioning?
int bitParity(int x) {
x = ( x >> 16 ) ^ x;
x = ( x >> 8 ) ^ x;
x = ( x >> 4 ) ^ x;
x = ( x >> 2 ) ^ x;
x = ( x >> 1 ) ^ x;
return (x & 1);
}