Here is source code of the C Program to number of bits set to 0 in a integer x. The C program is successfully compiled and run on a Linux system. The program output is also shown below.
* C Program to Count Number of bits set to 0 in a Integer x
#define NUM_BITS_INT (8*sizeof(int))
int i, num, snum, res, count = 0;
printf("\nEnter the number");
* Check each bit whether the bit is set or unset
* Uses >> and & operator for checking individual bits
for (i = 0;i <= NUM_BITS_INT;i++)
snum = num >> i;
res = snum & 1;
if (res == 0)
$ gcc bit1.c $ a.out Enter the number128 31 $ a.out Enter the number -127 6
Sanfoundry Global Education & Learning Series – 1000 C Programs.