The problem link is Hamming Distance.
The Hamming distance between two integers is the number of positions at which
the corresponding bits are different.Given two integers x and y, calculate
the Hamming distance.
xor means if the corresponding bit is different,then we get a bit 1 else we get a bit 0.We first xor x and y and then count how many 1 bit in the result. You can find more Bit Twiddling Hacks.
public static int hammingDistance(int x, int y) {
int xor = x ^ y;
int ret = 0;
while (xor != 0) {
ret++;
xor &= (xor - 1);
}
return ret;
}