题目描述
编写一个函数,确定需要改变几个位,才能将整数A转变成整数B。
给定两个整数int A,int B。请返回需要改变的数位个数。
测试样例:
10,5
返回:4
import java.util.*;
public class Transform {
public int calcCost(int A, int B) {
// write code here
int p = 0;
int cnt = 0;
while(A>0&&B>0){
if((A&1)!=(B&1)){
++cnt;
}
A = A>>1;
B = B>>1;
}
while(A>0){
if((A&1)==1){
++cnt;
}
A = A>>1;
}
while(B>0){
if((B&1)==1){
++cnt;
}
B = B>>1;
}
return cnt;
}
}