题目描述
世界上有10种人,一种懂二进制,一种不懂。那么你知道两个int32整数m和n的二进制表达,有多少个位(bit)不同么?
输入例子:
1999 2299
输出例子:
7
public class Solution {
/**
* 获得两个整形二进制表达位数不同的数量
*
* @param m 整数m
* @param n 整数n
* @return 整型
*/
public int countBitDiff(int m, int n) {
int total=0;
int r=m^n;
String a=Integer.toBinaryString(r);
for(char b:a.toCharArray())
{
if (b=='1')
total++;
}
return total;
}
}