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 ; Main function
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countZeros ( int x ) { unsigned y ; int 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 ; } int main ( ) { int x = 101 ; cout << countZeros ( x ) ; return 0 ; }