Check if bits of a number has count of consecutive set bits in increasing order | JavaScript program to find if bit - pattern of a number has increasing value of continuous - 1 or not . ; Returns true if n has increasing count of continuous - 1 else false ; store the bit - pattern of n into bit bitset - bp ; set prev_count = 0 and curr_count = 0. ; increment current count of continuous - 1 ; traverse all continuous - 0 ; check prev_count and curr_count on encounter of first zero after continuous - 1 s ; check for last sequence of continuous - 1 ; Driver code
function dec2bin ( dec ) { return ( dec >>> 0 ) . toString ( 2 ) ; } function findContinuous1 ( n ) { let bp = dec2bin ( n ) let bits = bp . length let prev_count = 0 let curr_count = 0 let i = 0 while ( i < bits ) { if ( bp [ i ] == ' ' ) { curr_count += 1 ; i += 1 ; } else if ( bp [ i - 1 ] == ' ' ) { i += 1 curr_count = 0 continue } else { if ( curr_count < prev_count ) return 0 i += 1 prev_count = curr_count curr_count = 0 } } if ( prev_count > curr_count && ( curr_count != 0 ) ) return 0 return 1 } n = 179 if ( findContinuous1 ( n ) ) document . write ( " " ) else document . write ( " " )