top of page

Count number of bits to be flipped to convert a to b.



#include <iostream>

using namespace std;

int main()
{
    int a = 3 , b = 4;
    int count = 0;
    int x = a ^ b;
    cout<<x<<endl;
    
    while(x) {
        if(x & 1)
        {
            count++;
        }
        x = x>>1;
    }
    cout<<count<<endl;
}

Comments


bottom of page