标签:
世界上有10种人,一种懂二进制,一种不懂。那么你知道两个int32整数m和n的二进制表达,有多少个位(bit)不同么?
输入例子:
1999 2299
输出例子:
7
class Solution {
public:
/**
* 获得两个整形二进制表达位数不同的数量
*
* @param m 整数m
* @param n 整数n
* @return 整型
*/
int countBitDiff(int m, int n) {
m^=n;
int a=0;
for(int k=0;k<32;k++){
if(((unsigned)m>>k)&1)
a++;
}
return a;
}
};
标签:
原文地址:http://blog.csdn.net/a819721810/article/details/45258011