Keep shifting x by one until leftmost bit does not become 1.
let total_bits = sizeofInt * 8 ; let res = 0 ; while ( ( x & ( 1 << ( total_bits - 1 ) ) ) == 0 ) { x = ( x << 1 ) ; res ++ ; } return res ; }