题目描述
世界上有10种人,一种懂二进制,一种不懂。那么你知道两个int32整数m和n的二进制表达,有多少个位(bit)不同么?

class Solution {
public:
    /**
     * 获得两个整形二进制表达位数不同的数量
     * 
     * @param m 整数m
     * @param n 整数n
     * @return 整型
     */
    int countBitDiff(int m, int n) {
        int ans=0;
        while(m || n)
        {
            if(m%2 != n%2) ans++;
            m/=2;
            n/=2;
        }
        return ans;
    }
};
Last modification:October 31st, 2019 at 08:03 pm
如果觉得我的文章对你有用,请随意赞赏