Number of leading zeros in binary representation of a given number
Last Updated :
11 Apr, 2023
Given a positive integer N, the task is to find the number of leading zeros in its binary representation.
A leading zero is any 0 digit that comes before the first nonzero digit in a number’s binary form.
Examples:
Input : N = 16
Output : 27
Explanation: As Binary(16) = (00000000000000000000000000010000)
Input : N = 33
Output : 26
Explanation: 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++
#include <bits/stdc++.h>
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 = 0;
cout << countZeros(x);
return 0;
}
|
Java
class GFG {
static byte sizeofInt = 8 ;
static int countZeros( int x)
{
int total_bits = sizeofInt * 8 ;
int res = 0 ;
while ((x & ( 1 << (total_bits - 1 ))) == 0 ) {
x = (x << 1 );
res++;
}
return res;
}
public static void main(String[] args)
{
int x = 101 ;
System.out.println(countZeros(x));
}
}
|
Python3
def countZeros(x):
total_bits = 32
res = 0
while ((x & ( 1 << (total_bits - 1 ))) = = 0 ):
x = (x << 1 )
res + = 1
return res
x = 101
print (countZeros(x))
|
C#
using System;
class GFG {
static byte sizeofInt = 8;
static int countZeros( int x)
{
int total_bits = sizeofInt * 8;
int res = 0;
while ((x & (1 << (total_bits - 1))) == 0) {
x = (x << 1);
res++;
}
return res;
}
public static void Main(String[] args)
{
int x = 101;
Console.WriteLine(countZeros(x));
}
}
|
Javascript
<script>
let sizeofInt = 8;
function countZeros(x)
{
let total_bits = sizeofInt * 8;
let res = 0;
while ((x & (1 << (total_bits - 1))) == 0)
{
x = (x << 1);
res++;
}
return res;
}
let x = 101;
document.write(countZeros(x));
</script>
|
Time complexity: O(1)
Auxiliary space: O(1)
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++
#include <bits/stdc++.h>
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;
}
|
Java
import java.io.*;
class GFG {
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;
}
public static void main (String[] args) {
int x = 101 ;
System.out.println (countZeros(x));
}
}
|
Python3
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;
def main():
x = 101 ;
print (countZeros(x))
if __name__ = = '__main__' :
main()
|
C#
using System;
class GFG
{
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;
}
static public void Main ()
{
int x = 101;
Console.WriteLine(countZeros(x));
}
}
|
PHP
<?php
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 ;
}
$x = 101;
echo countZeros( $x );
|
Javascript
<script>
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;
}
let x = 101;
document.write(countZeros(x));
</script>
|
Time complexity: O(1)
Auxiliary space: O(1)
Solution 3: Using predefined functions
In Java, the numberOfLeadingZeros() method of the Integer and Long class is used to count the leading zeros of the integer.
C
#include <stdio.h>
int main()
{
int n = 19;
printf ( "Count of leading zeros before first "
"occurrence: %d" ,
__builtin_clz(n));
return 0;
}
|
C++
#include <iostream>
using namespace std;
int main()
{
int n = 19;
cout << "Count of leading zeros before first "
"occurrence: "
<< __builtin_clz(n) << endl;
return 0;
}
|
Java
class GFG {
public static void main(String[] args)
{
int n = 19 ;
System.out.println(
"Count of leading zeros before first occurrence: "
+ Integer.numberOfLeadingZeros(n));
}
}
|
Python3
n = 19
print ( "Count of leading zeros before first occurrence: " , bin (n)[ 2 :].zfill( 32 ).index( '1' ))
|
C#
using System;
public class GFG {
public static void Main( string [] args)
{
int n = 19;
string binary = Convert.ToString(n, 2).PadLeft(
32, '0' );
Console.WriteLine(
"Count of leading zeros before first occurrence: "
+ binary.IndexOf(
'1' ));
}
}
|
Javascript
let n = 19;
let binary = n.toString(2).padStart(32, '0' );
console.log( "Count of leading zeros before first occurrence: " ,binary.indexOf( '1' ));
|
Output
Count of leading zeros before first occurrence: 27
Time complexity: O(1)
Auxiliary space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...