Woodstock Blog

a tech blog for general algorithmic interview questions

[CC150v5] 5.5 Calculate Bits Conversion Required

Question

Write a function to determine the number of bits (change) required to convert integer A to integer B.

Solution

Using XOR operation, we can find out number of bits that are different.

The next important thing is to know how to count the number of ‘1’s in a integer. It’s like this:

c = c & (c - l) clears the least significant bit of ‘1’.

Keep doing this until all ‘1’s are cleared.

Code

code 1

public static int calcBitsSwapMe1(int a, int b) {
    int num = a ^ b;
    int count = 0;
    while (num != 0) {
        count += num & 1;
        num = num >>> 1;
    }
    return count;
}

code 2

public static int calcBitsSwapMe2(int a, int b) {
    int num = a ^ b;
    int count = 0;
    while (num != 0) {
        num = num & (num - 1);
        count++;
    }
    return count;
}