Calculate the number of set bits for every number from 0 to N | Javascript implementation of the approach ; Function to find the count of set bits in all the integers from 0 to n ; Driver code
function count ( n ) { let count = 0 ; while ( n > 0 ) { count += n & 1 ; n >>= 1 ; } return count ; } function findSetBits ( n ) { for ( let i = 0 ; i <= n ; i ++ ) document . write ( count ( i ) + " " ) ; } let n = 5 ; findSetBits ( n ) ;