题目:
整数转换。编写一个函数,确定需要改变几个位才能将整数A转成整数B。
示例1:
输入:A = 29 (或者0b11101), B = 15(或者0b01111)
输出:2
示例2:
输入:A = 1,B = 2
输出:2
代码实现:
class Solution {标签:count,yyds,temp,示例,int,金典,整数,LeetCode,输入 From: https://blog.51cto.com/u_13321676/5978714
public int convertInteger(int A, int B) {
int temp = A ^ B;
int count = 0;
while (temp != 0) {
temp &= (temp - 1); // 去掉二进制表示的最右边的1
count++;
}
return count;
}
}