I am trying to find Hamming distance between two integers, and I got it to work with:
int count = 0;
for(int i = 0; i < 32; i++)
{
if((x ^ y) & 1)
count++;
x >>= 1;
y >>= 1;
}
However, it does not work with:
if(x & 1 != y & 1)
When x = 1 and y = 4, the correct result is 2. However, the second version outputs 1. Sounds like I need a lesson in discrete logic.
How can I rewrite the second if-statement to make it work?