Find consecutive 1 s of length >= n in binary representation of a number | Function to count the number of leading zeros ; Function to find the string of n consecutive 1 's ; Initialize position to return . ; Skip leading 0 's ; Set position after leading 0 's ; Count first group of 1 's. ; If length of consecutive 1 's  is greater than or equal to n ; Not enough 1 's  skip over to next group ; Update the position ; if no string is found ; Driver code
function countLeadingZeros ( x ) { let y ; let n ; n = 32 ; y = x >> 16 ; if ( y != 0 ) { n = n - 16 ; x = y ; } y = x >> 8 ; if ( y != 0 ) { n = n - 8 ; x = y ; } y = x >> 4 ; if ( y != 0 ) { n = n - 4 ; x = y ; } y = x >> 2 ; if ( y != 0 ) { n = n - 2 ; x = y ; } y = x >> 1 ; if ( y != 0 ) return n - 2 ; return n - x ; } function FindStringof1s ( x , n ) { let k , p ; p = 0 ; while ( x != 0 ) { k = countLeadingZeros ( x ) ; x = x << k ; p = p + k ; k = countLeadingZeros ( ~ x ) ; if ( k >= n ) return p + 1 ; x = x << k ; p = p + k ; } return - 1 ; } let x = 35 ; let n = 2 ; document . write ( FindStringof1s ( x , n ) ) ;