lintcode Flip Bits

image.png

看异或之后1的个数

class Solution {
public:
    /**
     * @param a: An integer
     * @param b: An integer
     * @return: An integer
     */
    int bitSwapRequired(int a, int b) {
        // write your code here
        unsigned int c = a ^ b;
        int count = 0;
        for(c; c != 0; c = c >> 1){
            count += (c & 1);
        }
        return count;
    }
};

你可能感兴趣的:(lintcode Flip Bits)