How to Count number of bits to be flipped to convert A to B ?
- Write the program to count number of bits needed to be flipped to convert ‘a’ to ‘b’. For Example given two numbers a = 6 and b = 12; then the output is 2
- Binary representation of a is 00000110
- Binary representation of b is 00001100
- We need to flip highlighted two bits in a
- To make it b.
Explanation
- Calculate XOR of A and B.
- Count the set bits in the above Calculated XOR result.