簡體   English   中英

計算C語言中8位二進制值的漢明距離

[英]Counting Hamming Distance for 8-bit binary Values in C Language

我寫了一個比較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

把parantheses放在n&1和m&1附近。

if ((n&1) != (m&1))

http://ideone.com/F7Kyzg

這是因為!=在&: http//www.swansontec.com/sopc.html之前

你也需要移動m來比較正確的位。

無論相等測試是否通過,您都需要移動它們。 (將移動移到內部}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM