- #include
//两个int(32位)整数m和n的二进制表达中,有多少个位(bit)不同 - int Acount_diff_bit(int m, int n)
- {
- int count = 0;
- int i = 0;
- for (i = 0; i < 32; i++)
- {
- if ((m >> i) & 1 != (n >> i) & 1) //两个整形数据的最右位比较
- {
- count++;
- }
- }
- return count;
- }
- int Bcount_diff_bit(int m, int n)
- {
- int count = 0;
- //异或操作符
- //相同为0,相异为1
- int ret = m ^ n;
- //统计一下ret中二进制位有几个1
- //有几个1(相异),则能算出两个整数二进制位有几个不同
- while (ret)
- {
- ret = ret & (ret - 1);
- count++;
- }
- return count;
- }
- int main()
- {
- int m = 0;
- int n = 0;
- scanf("%d %d",&m,&n);
- int ref = Acount_diff_bit(m,n);
- int bef = Bcount_diff_bit(m,n);
- printf("%d\n",ref);
- printf("%d\n",bef);
- return 0;
- }