这题太简单了,我用了位运算
int hammingDistance(int x, int y) {
int count = 0;
while(x!=0||y!=0){
if ((x&0x1) != (y&0x1)) count++;
x = x>>1;
y = y>>1;
}
return count;
}
本文介绍了一种使用位运算计算两个整数之间的汉明距离的方法。通过对比每个位上的二进制值来确定不同位的数量,进而得出两数的汉明距离。
这题太简单了,我用了位运算
int hammingDistance(int x, int y) {
int count = 0;
while(x!=0||y!=0){
if ((x&0x1) != (y&0x1)) count++;
x = x>>1;
y = y>>1;
}
return count;
}