[LeetCode] 461. 汉明距离
蛮简单的一个题,位运算
public int hammingDistance(int x, int y) {
int cnt = 0;
while (x>0&&y>0) {
int a = x & 1;
int b = y & 1;
if (a!=b) cnt++;
x>>=1;
y>>=1;
}
if (x!=0) {
while (x>0) {
if ((x&1) == 1) cnt++;
x>>=1;
}
}
if (y!=0) {
while (y>0) {
if ((y&1) == 1) cnt++;
y>>=1;
}
}
return cnt;
}