Number of leading zeros in binary representation of a given number

27 May, 2022
Given an integer n, output the no. of leading zeros in its binary form.A leading zero is any 0 digit that comes before the first nonzero digit in a number’s binary form. Examples:  
Input : 16
Output :27
As Binary(16) = (00000000000000000000000000010000)

Input :33
Output :26
As Binary(16)=(00000000000000000000000000100001)
 
Solution 1: A naive approach is to convert the no. into its binary form and then count the no. of leading zeros. It uses expensive divide operations.  
C++
Java
Python3
C#
Javascript
// C++ program of number of leading zeros in// binary representation of a given number#include <bits/stdc++.h>using namespace std; // Function to count the no. of leading zerosint countZeros(unsigned int x){    // Keep shifting x by one until leftmost bit    // does not become 1.    int total_bits = sizeof(x) * 8;    int res = 0;    while ( !(x & (1 << (total_bits - 1))) )    {        x = (x << 1);        res++;    }     return res;} // Main functionint main(){    int x = 101;    cout << countZeros(x);    return 0;}
// Java program of number of leading zeros in// binary representation of a given numberclass GFG{static byte sizeofInt = 8; // Function to count the no. of leading zerosstatic int countZeros(int x){    // Keep shifting x by one until leftmost bit    // does not become 1.    int total_bits = sizeofInt * 8;    int res = 0;    while ((x & (1 << (total_bits - 1))) == 0)    {        x = (x << 1);        res++;    }     return res;} // Driver Codepublic static void main(String[] args){    int x = 101;    System.out.println(countZeros(x));}} // This code is contributed by PrinciRaj1992
# Python3 program of number of# leading zeros in binary# representation of a given number # Function to count the# no. of leading zerosdef countZeros(x):         # Keep shifting x by one until    # leftmost bit does not become 1.    total_bits = 32    res = 0    while ((x & (1 << (total_bits - 1))) == 0):        x = (x << 1)        res += 1     return res # Driver Codex = 101print(countZeros(x)) # This code is contributed# by Mohit Kumar
// C# program of number of leading zeros in// binary representation of a given numberusing System; class GFG{static byte sizeofInt = 8; // Function to count the// no. of leading zerosstatic int countZeros(int x){    // Keep shifting x by one until    // leftmost bit does not become 1.    int total_bits = sizeofInt * 8;    int res = 0;    while ((x & (1 << (total_bits - 1))) == 0)    {        x = (x << 1);        res++;    }    return res;} // Driver Codepublic static void Main(String[] args){    int x = 101;    Console.WriteLine(countZeros(x));}} // This code is contributed by Rajput-Ji
<script> // JavaScript program of number of leading zeros in// binary representation of a given number let sizeofInt = 8; // Function to count the no. of leading zerosfunction countZeros(x){    // 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;} // Driver Codelet x = 101;document.write(countZeros(x));      // This code is contributed by unknown2108 </script>
25
Solution 2: An efficient approach is to use Bitwise right shift operation to achieve the same. The steps in the algorithm are: Let x be our no. then 
    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;
The above approach executes in only 12 to 20 instructions. 
C++
Java
Python3
C#
PHP
Javascript
// C++ program of number of leading zeros in// binary representation of a given number#include <bits/stdc++.h>using namespace std; // Function to count the no. of leading zerosint 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;} // Main functionint main(){    int x = 101;    cout << countZeros(x);    return 0;}
// Java program of number of leading zeros in// binary representation of a given numberimport java.io.*; class GFG {    // Function to count the no. of leading zerosstatic int countZeros(int x){    int 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;} // Main function    public static void main (String[] args) {    int x = 101;    System.out.println (countZeros(x));    }//This code is contributed by @Tushil.   }
# Python3 program of number of leading zeros in# binary representation of a given number  # Function to count the no. of leading zerosdef countZeros(x):    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;  # Main functiondef main():    x = 101;    print(countZeros(x))  if __name__ == '__main__':    main()
// C# program of number of leading zeros in// binary representation of a given numberusing System; class GFG{// Function to count the no. of// leading zerosstatic int countZeros(int x){    int 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;} // Driver Codestatic public void Main (){    int x = 101;    Console.WriteLine(countZeros(x));}} // This code is contributed by ajit
<?php// PHP program of number of leading zeros in// binary representation of a given number // Function to count the no. of leading zerosfunction countZeros($x){    $y;    $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;} // Driver Code$x = 101;echo countZeros($x); // This code is contributed// by Akanksha Rai
<script> // JavaScript program of number of leading zeros in// binary representation of a given number  // Function to count the no. of leading zerosfunction countZeros(x){    let y;    let 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;} // Main functionlet x = 101;document.write(countZeros(x));  // This code is contributed by patel2127 </script>
25
Solution 3: Using the GCC  __builtin_clz(x): This function is used to count the leading zeros of the integer where clz stands for count leading zero’s. It counts a number of zeros before the first occurrence of one(set bit).
C
#include <stdio.h>int main(){    int n = 19; //00000000 00000000 00000000 010011    printf(        "Count of leading zeros before first occurrence: %d",        __builtin_clz(n));    return 0;}
Count of leading zeros before first occurrence: 27
Output: Count of leading zeros before the first occurrence: 27
Solution 4: Using predefined functions
In Java, the numberOfLeadingZeros() method of the Integer and Long class is used to count the leading zeros of the integer.
Java
// Java Program that counts the number of leading zeroes class GFG {    public static void main(String[] args)    {        int n = 19; // 00000000 00000000 00000000 010011        System.out.println(            "Count of leading zeros before first occurrence: "            + Integer.numberOfLeadingZeros(n));    }}
Count of leading zeros before first occurrence: 27

Time Complexity: The time complexity of this approach is O(1) Space Complexity: The space complexity of this approach is O(1) 
jit_t
Akanksha_Rai
29AjayKumar
mohit kumar 29
princiraj1992
Rajput-Ji
arun singh
unknown2108
patel2127
gabaa406
phasing17
Technical Scripter 2018
Bit Magic
Technical Scripter
Bit Magic
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.