Skip to main content

Check if all bits of a number are set in c

C program for Check if all bits of a number are set. Here more information.

// Include header file
#include <stdio.h>

/*
  C program for
  Check if all bits of a number are set or active
*/
// Determine that given number 
// contains all active bits or not
void isActiveBits(int num)
{
	printf("\n Number : %d\n", num);
	if (num != 0 && ((num + 1) & num) == 0)
	{
		// When all set bit exist in given number
		printf(" Yes\n");
	}
	else
	{
		printf(" No\n");
	}
}
int main()
{
	// Test A
	// (-1) Binary 0001 => 1110 (1's) => 1111 (2s)
	isActiveBits(-1);
	// Test B
	// (-4) 00000100 = > 11111011 (1s) => 11111100 (2s)
	isActiveBits(-4);
	// Test C
	// (15) 1111 => Yes
	isActiveBits(15);
	// Test D
	// (100) 1100100 
	isActiveBits(100);
}

Output

 Number : -1
 Yes

 Number : -4
 No

 Number : 15
 Yes

 Number : 100
 No




Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment