Problem solving with programming: Number of bits to change from one number to other
Given two numbers, calculate the number of bits required to change from one number to the other.
We can find out the difference bits as follows. We first find out the XOR of two numbers. This gives us a bit pattern in which there is a set bit, if two bits differ. Then we can calculate the number of set bits in the XOR which gives the required answer.
Read full article from Problem solving with programming: Number of bits to change from one number to otherint getBitDiff(int num1, int num2){int diff = num1 ^ num2;//find number of set bits in XOR valueint count = 0;while ( diff > 0 ){count++;diff = diff & (diff-1);}return count;}