题目:编写一个函数,确定需要改变几个位,才能将整数A转变成整数B。给定两个整数int A,int B。请返回需要改变的数位个数。
分析:将A和B异或,判断异或后1的个数
class Transform {
public:
int calcCost(int A, int B) {
// write code here
int AB=A^B;
int count=0;
while(AB){
int t=AB%2;
if(t){
count++;
}
AB/=2;
}
return count;
}
};