Flip Bits(将整数A转换为B)

http://www.lintcode.com/en/problem/flip-bits/

class Solution {
    /**
     * @param a, b: Two integer
     *           return: An integer
     */
    public static int bitSwapRequired(int a, int b) {
        // write your code here
        //先用异或求出ab不同的位数,相同的位置全为0,不同的为1
//        下边问题就转化成找c里边有多少个1
        int c = a ^ b;
        int res = 0;
        while (c != 0) {
            if ((c & 1) == 1) {
                res++;
            }
            c = c >>> 1;
        }
        return res;
    }
};

你可能感兴趣的:(Flip Bits(将整数A转换为B))