您的位置:首页 > 其它

Flip Bits

2016-07-15 05:31 381 查看
Determine the number of bits required to flip if you want to convert integer n to integer m.

Notice

Both n and m are 32-bit integers.

Example

Given n =
31
(11111), m =
14
(01110), return
2
.

class Solution {
/**
*@param a, b: Two integer
*return: An integer
*/
public static int bitSwapRequired(int a, int b) {
// write your code here
int diff = a ^ b;
int count = 0;
while (diff != 0) {
count++;
// remove the last 1
diff = diff & (diff - 1);  // 这种方法很好。
}
return count;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: