C Program to Check if All Bits of Integer is One

This is a C Program to check if all the bits of a given integer is one.

Problem Description

This C Program to check if all the bits of a given integer is one.

Problem Solution

Take input from the user and performs bits operations as shown in the program below.

Program/Source Code

Here is source code of the C Program to check if all the bits of a given integer is one. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

/*
 * C Program to check if all the bits of a given integer is one(1)
 */ 
#include <stdio.h>
#include <stdlib.h>
int main ()
{
	int num = 0, count = 0, n = 0, i = 0;
 
	printf ("\nEnter the number : ");
	scanf ("%d", &num);
	n = num;
	if (num == 0)
	{
		printf ("\nFALSE : ALL BITS ARE NOT SET\n");
		exit (0);
	}
	while (n)
	{
		count ++;
		n = n >> 1;
	}
	for (i = 0; i < count; i++)
	{
		if (((num >> i) & 1) == 1)
		{
			continue;
		}
		else
		{
			printf ("\nFALSE : ALL BITS ARE NOT SET\n");
			exit (0);
		}
	}
	printf ("\nTRUE : ALL BITS ARE SET\n");
	return 0;
}
Program Explanation

1. In this C program, we are reading the number using ‘num’ variable. Take the input number from the user and make a copy of the number to n.
2. If the number given is zero we just print NOT SET and exit.
3. While loop is used to count the number of bits. n = n >> 1; it performs right shift by 1 bit. Traverse through each bit. continue if the bit is set else print NOT SET and exit.
4. (num >> n) & 1; is used to retrieve the nth bit from the number num.
5. For loop is used to check whether all bits are set. If all bits are set then print “ALL BITS ARE SET” and return.

advertisement
advertisement
Runtime Test Cases

Test case 1 – Here, the entered number is 0.

$ cc bit22.c
$ a.out
Enter the number : 0
 
FALSE : ALL BITS ARE NOT SET

Test case 2 – Here, the entered number is 5.

$ cc bit22.c
$ a.out
Enter the number : 5
 
FALSE : ALL BITS ARE NOT SET

Test case 3 – Here, the entered number is 7.

$ cc bit22.c
$ a.out
Enter the number : 7
 
TRUE : ALL BITS ARE SET

Test case 4 – Here, the entered number is 127.

advertisement
$ cc bit22.c
$ a.out
Enter the number : 127
 
TRUE : ALL BITS ARE SET

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.

If you find any mistake above, kindly email to [email protected]

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.