C Program to Count Set Bits using Bitwise Operations

This is a C Program to count the number of bits set to one using bitwise operations.

Problem Description

This C Program Counts the Number of Bits set to One using Bitwise Operations.

Problem Solution

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

Program/Source Code

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
 */
#include <stdio.h>
 
int main()
{
    unsigned int number;
    int count = 0;
 
    printf("Enter the unsigned integer:\n");
    scanf("%d", &number);
    while (number != 0)
    {
        if ((number & 1) == 1)
            count++;
        number = number >> 1;
    }
    printf("number of one's are :\n%d\n", count);
    return 0;
}
Program Explanation

In this C Program, we are reading the unsigned integer using ‘number’ variable. Using while loop count the number of bits set to one using bitwise operations.

advertisement
advertisement

If condition statement is used to check the value which copies a bit to the result if it exists in both operands using Binary AND operator is equal to 1. If the condition is true, then execute the statement and increment the number of bits set to 1 using count variable.

Using Binary Right Shift Operator, the left operand’s value is moved right by the number of bits specified by the right operands and assign the value to ‘number’ variable. Hence we are displaying the output of the program. Print the count of the number of bits set to one using bitwise operations.

Runtime Test Cases
 
$ 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.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

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.

advertisement
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.