整数转化

题目描述

编写一个函数,确定需要改变几个位,才能将整数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;
    }
};

 

posted on 2017-04-20 03:27  123_123  阅读(87)  评论(0编辑  收藏  举报