4.6
刚开始忽略了正数负数的问题,循环结束的条较为 tmp > 0 ,是不对的。
后来改成了循环32次。
class Solution {
/**
*@param a, b: Two integer
*return: An integer
*/
public static int bitSwapRequired(int a, int b) {
int count = 0;
int tmp = a^b;
for(int i = 0 ;i<32;i++){
if((tmp & 1) == 1 ){
count ++;
}
tmp >>=1;
}
return count;// write your code here
}
};