Hi ...
I read this question in one magzine.
2 ints A and B are given, find the no of bits that need to be flipped in A to get B.
Can any one tell me how to solve this....
Thanks in advance.
Printable View
Hi ...
I read this question in one magzine.
2 ints A and B are given, find the no of bits that need to be flipped in A to get B.
Can any one tell me how to solve this....
Thanks in advance.
Kindly do not cross post. Since I could not determine whether you intended it to be C, C++ or just a question of an algorithm, it has been moved to the Tech board.
Here's one possible algorithm: set a counter to 0. Start from the first bit of A, and compare it with the first bit of B. If the bits are different, increment the counter. Repeat for the remaining bits.
It's the number of 1's in A XOR B.
How about xor the two ints, then count the 1 bits in the result. (just as an alternative).
Beat me to it :)