Number of mismatching bits in the binary representation of two integers | C ++ implementation of the approach ; compute number of different bits ; since , the numbers are less than 2 ^ 31 run the loop from '0' to '31' only ; right shift both the numbers by ' i ' and check if the bit at the 0 th position is different ; Driver code ; find number of different bits
#include <bits/stdc++.h> NEW_LINE using namespace std ; void solve ( int A , int B ) { int count = 0 ; for ( int i = 0 ; i < 32 ; i ++ ) { if ( ( ( A >> i ) & 1 ) != ( ( B >> i ) & 1 ) ) { count ++ ; } } cout << " Number ▁ of ▁ different ▁ bits ▁ : ▁ " << count << endl ; } int main ( ) { int A = 12 , B = 15 ; solve ( A , B ) ; return 0 ; }