int countBitDiff(int m, int n)
{
/*
解法一
int dif=m^n;//先将二者做异或运算,得到结果;
int cnt=0;
while(dif!=0){
dif=dif&(dif-1);
++cnt;
} //统计一个整数dif含有多少个1;
return cnt;
*/
int x = 0,count=0;
x = m ^ n;
while(x != 0){
if((x - (x>>1<<1)) == 1)
{
count+=1;
}
x= (x>>1);
}
return count;
}
int main()
{
printf("%d",countBitDiff(1999, 2999) );
return 0;
}
小米笔试 懂二进制?
最新推荐文章于 2021-03-01 13:21:38 发布