class Solution {
public:
/**
* 获得两个整形二进制表达位数不同的数量
*
* @param m 整数m
* @param n 整数n
* @return 整型
*/
int countBitDiff(int m, int n) {
int diff = m^n;//m与n亦或,不相同的置1
int count = 0;
while(diff)
{
diff = diff&(diff-1);
count++;
}
return count;
}
};
2023-07-28 11:30:28
class Solution {
public:
/**
* 获得两个整形二进制表达位数不同的数量
*
* @param m 整数m
* @param n 整数n
* @return 整型
*/
int countBitDiff(int m, int n) {
int diff = m^n;//m与n亦或,不相同的置1
int count = 0;
while(diff)
{
diff = diff&(diff-1);
count++;
}
return count;
}
};