Number of leading zeros in binary representation of a given number | JavaScript program of number of leading zeros in binary representation of a given number ; Function to count the no . of leading zeros ; Keep shifting x by one until leftmost bit does not become 1. ; Driver Code
let sizeofInt = 8 ; function countZeros ( x ) { let total_bits = sizeofInt * 8 ; let res = 0 ; while ( ( x & ( 1 << ( total_bits - 1 ) ) ) == 0 ) { x = ( x << 1 ) ; res ++ ; } return res ; } let x = 101 ; document . write ( countZeros ( x ) ) ;