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