Flip bits of the sum of count of set bits of two given numbers | Function to count number of set bits in integer ; Variable for counting set bits ; Function to invert bits of a number ; Calculate number of bits of N - 1 ; ; Function to invert the sum of set bits in A and B ; Stores sum of set bits ; Driver Code
function countSetBits ( n ) { var count = 0 ; while ( n != 0 ) { n &= ( n - 1 ) ; count ++ ; } return count ; } function invertBits ( n ) { var x = parseInt ( ( Math . log ( n ) / Math . log ( 2 ) ) ) ; var m = 1 << x ; m = m | m - 1 ; n = n ^ m ; return n ; } function invertSum ( A , B ) { var temp = countSetBits ( A ) + countSetBits ( B ) ; document . write ( invertBits ( temp ) ) ; } var A = 5 ; var B = 7 ; invertSum ( A , B ) ;