You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

26 lines
603 B
C

/*
* C Program to Count Number of bits set to 0 in a Integer x
*/
#include <stdio.h>
#define NUM_BITS_INT (8*sizeof(int))
int count_unset(int);
int main()
{
int i, num, snum, res, count = 0;
printf("\nEnter the number");
scanf("%d", &num);
/*
* 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)
count++;
}
printf("%d", count);
}