Number of leading zeros in binary representation of a given number

Given an integer n, output the no. of leading zeros in it’s 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 it’s binary form and then count the no. of leading zeros. It uses expensive divide operations.

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 zeros
int 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 function
int main()
{
    int x = 101;
    cout << countZeros(x);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program of number of leading zeros in 
// binary representation of a given number
class GFG 
{
static byte sizeofInt = 8;
  
// Function to count the no. of leading zeros
static 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 Code
public static void main(String[] args) 
{
    int x = 101;
    System.out.println(countZeros(x));
}
}
  
// This code is contributed by PrinciRaj1992

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program of number of 
# leading zeros in binary 
# representation of a given number
  
# Function to count the 
# no. of leading zeros
def 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 Code
x = 101
print(countZeros(x))
  
# This code is contributed 
# by Mohit Kumar

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program of number of leading zeros in 
// binary representation of a given number
using System;
  
class GFG 
{
static byte sizeofInt = 8;
  
// Function to count the 
// no. of leading zeros
static 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 Code
public static void Main(String[] args) 
{
    int x = 101;
    Console.WriteLine(countZeros(x));
}
}
  
// This code is contributed by Rajput-Ji

chevron_right


Output:

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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 zeros
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;
}
  
// Main function
int main()
{
    int x = 101;
    cout << countZeros(x);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program of number of leading zeros in 
// binary representation of a given number
import java.io.*;
  
class GFG {
    // Function to count the no. of leading zeros 
static 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.    
}

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program of number of leading zeros in
# binary representation of a given number
  
  
# Function to count the no. of leading zeros
def 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 function
def main():
    x = 101;
    print(countZeros(x))
  
  
if __name__ == '__main__':
    main()

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program of number of leading zeros in 
// binary representation of a given number
using System;
  
class GFG
{
// Function to count the no. of 
// leading zeros 
static 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 Code
static public void Main ()
{
    int x = 101; 
    Console.WriteLine(countZeros(x)); 
}
}
  
// This code is contributed by ajit

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program of number of leading zeros in 
// binary representation of a given number
  
// Function to count the no. of leading zeros
function 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

chevron_right


Output:

25

Time Complexity: The time complexity of this approach is O(1)
Space Complexity: The space complexity of this approach is O(1)

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.