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
However, this is difficult as I can only use bit operators on this problem (! ˜ & ˆ | + << >>
are the only legal ones). That means, no loops, if-then
, or anything of the sort. Constants can be used.
So far, what I have doesn't work, but I figured that I should shift the bits of the integer 16
, 8
, and 4
times and XOR
the remaining integers.
Can anyone offer some advice? Thanks.