两个int32整数m和n的二进制表达,有多少个位(bit)不同么?
public class Solution {
/**
* 获得两个整形二进制表达位数不同的数量
*
* @param m 整数m
* @param n 整数n
* @return 整型
*/
public int countBitDiff(int m, int n) {
int count=0;
int v=m^n;<span style="color:#FF0000;">//异或得到结果,判断结果中1的个数即可解决问题</span>
while(v!=0){
count+=v & 1;
v>>=1;
}
return count;
}
}