Count total unset bits in all the numbers from 1 to N | Function to return the count of unset bits in the binary representation of all the numbers from 1 to n ; To store the count of unset bits ; For every integer from the range [ 1 , n ] ; A copy of the current integer ; Count of unset bits in the current integer ; If current bit is unset ; Driver code
function countUnsetBits ( n ) { var cnt = 0 ; for ( var i = 1 ; i <= n ; i ++ ) { var temp = i ; while ( temp ) { if ( temp % 2 == 0 ) cnt ++ ; temp = parseInt ( temp / 2 ) ; } } return cnt ; } var n = 5 ; document . write ( countUnsetBits ( n ) ) ;