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++
// 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; } |
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
[tabbyending]
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 |
25
Time Complexity: The time complexity of this approach is O(1)
Space Complexity: The space complexity of this approach is O(1)
Recommended Posts:
- Count number of trailing zeros in Binary representation of a number using Bitset
- Comparing leading zeros in binary representations of two numbers
- Check if the binary representation of a number has equal number of 0s and 1s in blocks
- Binary representation of a given number
- Binary representation of previous number
- Largest number with binary representation is m 1's and m-1 0's
- Next greater number than N with exactly one bit different in binary representation of N
- Occurrences of a pattern in binary representation of a number
- Find the n-th number whose binary representation is a palindrome
- Check if binary representation of a number is palindrome
- Prime Number of Set Bits in Binary Representation | Set 1
- Check if actual binary representation of a number is palindrome
- Find consecutive 1s of length >= n in binary representation of a number
- Number of mismatching bits in the binary representation of two integers
- Check if binary representation of a given number and its complement are anagram
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.



