编程实现: 两个int(32位)整数m和n的二进制表达中, 有多少个位(bit)不同?

#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
int main()
{
int num1 = 1999;
int num2 = 2299;
int count = 0;
int ret = num1^num2; //比特位不同异或后结果为1
while (ret)
{
ret = ret&(ret - 1);
count++;
} //将异或后的1输出,即有几个比特位不同
printf("%d", count);
system("pause");
return 0;
}

猜你喜欢

转载自blog.51cto.com/14239789/2377671