Count number of bits to be flipped to convert A to B | Function that count set bits ; Function that return count of flipped number ; Return count of set bits in a XOR b ; Driver code
function countSetBits ( n ) { var count = 0 ; while ( n != 0 ) { count ++ ; n &= ( n - 1 ) ; } return count ; } function FlippedCount ( a , b ) { return countSetBits ( a ^ b ) ; } var a = 10 ; var b = 20 ; document . write ( FlippedCount ( a , b ) ) ;