我编写了一个比较2位无符号整数的新程序。用汉明距离进行比较。但我的算法并不完美。你能告诉我这个代码有什么问题吗:(非常感谢!
这是我的计数方法;
int countHammDist(unsigned int n, unsigned int m)
{
int i=0;
unsigned int count = 0 ;
for(i=0; i<8; i++){
if( n&1 != m&1 ) {
count++;
}
n >>= 1;
m >>= 1;
}
return count;
}
A和b 8位二进制文件。
PrintInBinary(a);
PrintInBinary(b);
printf("\n %d", countHammDist(a,b));
让我给你看输出;
Enter two unsigned integers (0-99): 55 64
Your choices are 55 and 64
Number A: 00110111
Number B: 01000000
Hamming distance is ; 5
发布于 2013-11-06 23:50:56
把副词放在n&1和m&1附近。
if ((n&1) != (m&1))
http://ideone.com/F7Kyzg
这是因为!= is前面&:http://www.swansontec.com/sopc.html
发布于 2013-11-06 23:07:39
您也需要移动m
以比较正确的位元。
不管平等测试是否通过,您都需要转移它们。(将移位移出内部}
)
https://stackoverflow.com/questions/19824740
复制相似问题