[Link] https://leetcode.com/problems/hamming-distance/
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Solution {
public int hammingDistance(int x, int y) {
int bitX, bitY, cnt = 0;
if(x < y) {
int tmp = x;
x = y;
y = tmp;
}
while(x > 0) {
bitX = x & 1;
bitY = y & 1;
if(bitX != bitY) cnt++;
x = x>>1;
y = y>>1;
}
return cnt;
}
}