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 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
// 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
# 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#
// 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 |
Javascript
<script>// JavaScript program of number of leading zeros in// binary representation of a given numberlet 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++
// 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
// 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
# 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#
// 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// 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 |
Javascript
<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 occurance: %d", __builtin_clz(n)); return 0;} |
Output: Count of leading zeros before the 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)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
In case you wish to attend live classes with industry experts, please refer DSA Live Classes


