求两个输入数字的汉明距离
作者:互联网
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<assert.h>
int hammingDistance(int x, int y) {
int b = x ^ y;//首先二者异或去求不同的位数
int count = 0;
int i = 0;
for (i = 0; i < 32; i++)//要点是32位字节循环
{
if (((b >> i) & 1 )== 1)//按位右移一位与1与去确定不同的位数
count++;
}
return count;
}
int main()
{
int x = 0, y = 0;
scanf("%d%d", &x, &y);
int ret = hammingDistance(x, y);
printf("%d", ret);
}
标签:count,数字,int,hammingDistance,ret,++,汉明,include,输入 来源: https://blog.51cto.com/14736509/2489955