C Program to Count Leading Zeros in Number using Bitwise Operations

This is a C Program to use bitwise operations to count the number of leading zero’s in a number x.

Problem Description

This C Program uses Bitwise operations to count the number of leading zero’s in a number x.

Problem Solution

Take input from the user and perform bitwise operations as shown in the program below.

Program/Source Code

Here is source code of the C Program to use Bitwise operations to count the number of leading zero’s in a number x. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

/*
 * C Program to Use Bitwise Operations to Count the Number of 
 * Leading Zero's in a Number x
 */
#include <stdio.h>
#include <malloc.h>
 
int main()
{
	int lim = sizeof(int) * 8;
	// the mBit is 1000 0000 0000 0000 0000 0000 0000 0000
	int mBit = 1 << (lim - 1);
	int num = 0, count = 0;
	printf ("Enter the digit: ");
	scanf ("%d", &num);	
	while (!(num & mBit))
	{
		num = (num << 1);
		count ++;
	}
	printf ("\nNumber of leading zero's is: %d\n", count);
	return 0;
}
Program Explanation

1. Take the input from the user and store it in the num variable. Leading zero’s in a binary number is equal to zeros preceding the highest order set bit of the number.
2. int lim = sizeof(int) * 8; is used to find the total number of bits required to store an integer in memory.
3. For Setting MSG, initialize a variable and set its MSB to 1. We can achieve this by using int mBit = 1 << (lim – 1);. So the mBit will be 10000000 00000000 00000000 00000000. It stores the number of leading zero’s in the variable count.
4. While statement is used to find the leading set bit. If the leading set bit is found we terminate the loop, else right shift the bit by 1 and we increase the value of count by 1.
5. Print the number of leading zero’s in the given number using print statement.

Runtime Test Cases

Test case 1 – Here, the entered digit is 16.

advertisement
advertisement
gcc leading_zeros.c -o leading-zero
./leading-zero
 
Enter the digit: 16
Number of leading zero's is: 27

Test case 2 – Here, the entered digit is 64.

Note: Join free Sanfoundry classes at Telegram or Youtube
gcc leading_zeros.c -o leading-zero
./leading-zero
 
Enter the digit: 64
Number of leading zero's is: 25

Test case 3 – Here, the entered digit is 1.

gcc leading_zeros.c -o leading-zero
./leading-zero
 
Enter the digit: 1
Number of leading zero's is: 31

Sanfoundry Global Education & Learning Series – 1000 C Programs.

advertisement

Here’s the list of Best Books in C Programming, Data-Structures and Algorithms

If you wish to look at programming examples on all topics, go to C Programming Examples.

advertisement

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.