如果要将整数A转换为B,需要改变多少个bit位?
您在真实的面试中是否遇到过这个题?
Yes
样例
如把31转换为14,需要改变2个bit位。
(31)10=(11111)2
(14)10=(01110)2
挑战
标签
Expand
你能想出几种方法?
class Solution {
/**
*@param a, b: Two integer
*return: An integer
*/
public static int bitSwapRequired(int a, int b) {
// write your code here
int count = 0;
int x = a^b;
while(x!=0){
if((x&1)==1){
count++;
}
x = x>>>1;
}
return count;
}
};