Skip to content
Related Articles

Related Articles

Number of leading zeros in binary representation of a given number

View Discussion
Improve Article
Save Article
  • Difficulty Level : Expert
  • Last Updated : 27 May, 2022
View Discussion
Improve Article
Save Article

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




// 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;
}

Java




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

Python3




# 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

C#




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

Javascript




<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 zeros
function 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 Code
let x = 101;
document.write(countZeros(x));
     
 
// This code is contributed by unknown2108
 
</script>

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




// 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;
}

Java




// 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.   
}

Python3




# 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()

C#




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

PHP




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

Javascript




<script>
 
// JavaScript program of number of leading zeros in
// binary representation of a given number
 
 // Function to count the no. of leading zeros
function 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 function
let x = 101;
document.write(countZeros(x));
 
 
// This code is contributed by patel2127
 
</script>

Output

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;
}

Output

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));
    }
}

Output

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)
 


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!