编程实现:
两个int(32位)整数m和n的二进制表达中,有多少个位(bit)不同?
输入例子:
1999 2299
输出例子:7
#include<stdio.h>
#include<stdlib.h>
int count_one_bits(unsigned int value) //求二进制数中1的个数
{
int count = 0;
while (value)
{
if (value % 2 == 1) // 取数字的二进制序列中的一个 1
{
count++;
}
value /= 2; //减掉所得的一个1
}
return count;
}
int main()
{
int a = 1999;
int b = 2299;
int result = a^b; //取异或,相同为0,相异为1
int count = count_one_bits(result); //求1的个数即为不同的个数
printf("二进制序列中不同的个数count = %d\n", count);
system("pause");
}