Determine the number of bits required to flip if you want to convert integer n to integer m.

Example Given n = 31 (11111), m = 14 (01110), return 2.

class Solution {
    /**
     *@param a, b: Two integer
     *return: An integer
     */
    public static int bitSwapRequired(int a, int b) {
        int n = a ^ b;
        int count = 0;
        while(n != 0) {
            count++;
            n = n & (n - 1);
        }
        return count;
    }
};

We first use the XOR operator ^ to find the different bits. Then we use the expression n & (n - 1), which removes the lowest 1 bit. This is called Kernighan’s Bit Counting Algorithm.