如果要将整数A转换为B,需要改变多少个bit位?
注意事项
Both n and m are 32-bit integers.
样例
如把31转换为14,需要改变2个bit位。
(31)10=(11111)2
(14)10=(01110)2
public class FlipBits {
public static int bitSwapRequired(int a, int b){
int count=0;
for(int c=a^b;c!=0;c=c>>>1){ //>>>无符号右移
count += c&1;
}
return count;
}
}