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.
Note:
0 ≤ x, y < 2^31.
My solution
class Solution{
public:
int hammingDistance(int x, int y){
int diff = x ^ y;
int count = 0;
//bitCount
while(diff != 0){
count +=diff % 2;
diff /= 2;
}
return count;
}
}