leetcode-Hamming Distance

2017-08-22
leetcode

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Solution {
public int hammingDistance(int x, int y) {
int i = 32;
int count = 0;
int tmp;

for( i = 0 ; i < 32; i++) {

tmp = 1 << i;

if ( (x & tmp) != (y & tmp)) {
count++;
}
}
return count;
}
}