//leetcode 461 高票解法
public class Solution {
public int hammingDistance(int x, int y) {
return Integer.bitCount(x ^ y);
}
}
//计算二进制中1的个数
class Solution {
public int hammingDistance(int x, int y) {
int diff = x ^ y; //xor for the difference of x and y,same=0,diff=1
return countOne(diff);
}
private int countOne(int num){
int count = 0;
while(num != 0){
num = num & (num - 1);
count++;
}
return count;
}
}