I'm trying to find the number of overlapping 1 bits between 2 given numbers.
For example, given 5
and 6
:
5 // 101
6 // 110
There is 1 overlapping 1 bit (the first bit)
I have following code
#include <iostream>
using namespace std;
int main() {
int a,b;
int count = 0;
cin >> a >> b;
while (a & b != 0) {
count++;
a >>= 1;
b >>= 1;
}
cout << count << endl;
return 0;
}
But when I entered 335
and 123
it returned 7
but I think it is not correct
Can someone see a problem with my code?