Here is source code of the C Program to Count the Number of Bits set to One using Bitwise Operations. The C program is successfully compiled and run on a Linux system. The program output is also shown below.
* C Program to Count the Number of Bits set to One using
* Bitwise Operations
unsigned int number;
int count = 0;
printf("Enter the unsigned integer:\n");
while (number != 0)
if ((number & 1) == 1)
number = number >> 1;
printf("number of one's are :\n%d\n", count);
$ cc bit2.c $ a.out Enter the unsigned integer: 128 number of one's are : 1 $ a.out Enter the unsigned integer: -127 number of one's are : 26
Sanfoundry Global Education & Learning Series – 1000 C Programs.