Number of leading zeros in binary representation of a given number | C ++ 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. ; Main function
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countZeros ( unsigned int x ) { int total_bits = sizeof ( x ) * 8 ; int res = 0 ; while ( ! ( x & ( 1 << ( total_bits - 1 ) ) ) ) { x = ( x << 1 ) ; res ++ ; } return res ; } int main ( ) { int x = 101 ; cout << countZeros ( x ) ; return 0 ; }