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 < 231.
answer:
class Solution {
public:
int hammingDistance(int x, int y) {
int hd=0;
for(int i=30;i>=0;i--){
int m=pow(2,i);
if(y/m!=x/m){
x=x%m;
y=y%m;
hd++;
}
else{
x=x;
y=y;
}
}
return hd;
}
};