题解 | #懂二进制#
懂二进制
https://www.nowcoder.com/practice/120e406db3fd46f09d55d59093f13dd8
class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param m int整型
* @param n int整型
* @return int整型
*/
int countBitDiff(int m, int n) {
// 1. 异或结果有多少个1即是有多少bit不同
int temp = m ^ n;
int res = 0;
// 2. x 与 (x-1)的与运算 即是消除最后一位的1
while (temp != 0) {
temp &= (temp - 1);
++res;
}
return res;
}
};
