Search code examples
bit-manipulationbitbitwise-operatorshamming-distance

Count number of bits to be flipped to convert A to B


Suppose, I have two numbers A and B. I need to find out how many numbers of bits needed to be changed to convert A to B.

Like:

A = 1101101
B = 1011011
     ^^ ^^

Here, we need to change 4 bits to convert A to B

How can I do this?


Solution

  • You can simply do this :

    int need=__builtin_popcountll(A^B);
    cout<<need;