Count number of bits to be flipped to convert A to B | Count number of bits to be flipped to convert A into B ; Function that count set bits ; Function that return count of flipped number ; Return count of set bits in a XOR b ; Driver code
#include <iostream> NEW_LINE using namespace std ; int countSetBits ( int n ) { int count = 0 ; while ( n > 0 ) { count ++ ; n &= ( n - 1 ) ; } return count ; } int FlippedCount ( int a , int b ) { return countSetBits ( a ^ b ) ; } int main ( ) { int a = 10 ; int b = 20 ; cout << FlippedCount ( a , b ) << endl ; return 0 ; }