C/C++ Program to Count set bits in an integer (original) (raw)
Last Updated : 14 May, 2025
Write an efficient program to count number of 1s in binary representation of an integer.
**Examples :
Input : n = 6
Output : 2
Binary representation of 6 is 110 and has 2 set bits
Input : n = 13
Output : 3
Binary representation of 11 is 1101 and has 3 set bits
**1. Simple Method
Loop through all bits in an integer, check if a bit is set and if it is then increment the set bit count. See below program.
C `
#include <stdio.h>
/* Function to get no of set bits in binary representation of positive integer n */ unsigned int countSetBits(unsigned int n) { unsigned int count = 0; while (n) { count += n & 1; n >>= 1; } return count; }
/* Program to test function countSetBits */ int main() { int i = 9; printf("%d", countSetBits(i)); return 0; }
`
**Recursive Approach :
C++ `
// Cpp implementation of recursive // approach to find the number // of set bits in binary representation // of positive integer n #include <bits/stdc++.h> using namespace std;
// recursive function to count set bits int countSetBits(int n) { // base case if (n == 0) return 0;
else
// if last bit set add 1 else add 0
return (n & 1) + countSetBits(n >> 1);
}
// driver code int main() { // get value from user int n = 9;
// function calling
cout << countSetBits(n);
return 0;
}
// This code is contributed by Raj.
`
Please refer complete article on
for more details!
Similar Reads
- How to count set bits in a floating point number in C? Given a floating point number, write a function to count set bits in its binary representation. For example, floating point representation of 0.15625 has 6 set bits (See this). A typical C compiler uses single precision floating point format. We can use the idea discussed here. The idea is to take a 2 min read
- Counting Set bit in C In C programming, counting set bits is the process of determining the number of bits that are set to 1 in a binary number. This operation is useful in various applications including network programming, error detection, and cryptography.In this article, we will learn how to count the number of set b 4 min read
- Count number of bits to be flipped to convert A to B | Set-2 Given two integers A and B, the task is to count the number of bits needed to be flipped to convert A to B.Examples: Input: A = 10, B = 7 Output: 3 binary(10) = 1010 binary(7) = 0111 1010 0111 3 bits need to be flipped.Input: A = 8, B = 7 Output: 4 Approach: An approach to solve this problem has alr 5 min read
- Setting Bits in C In C programming, setting a bit is the process of setting a specific bit of a binary number to 1. This operation is crucial in various applications, including memory management, data processing, and hardware control.In this article, we will learn how to set a bit at a given position in a binary numb 3 min read
- Convert Decimal to Binary in C In this article, we will learn to write a C program to convert a decimal number into a binary number. The decimal number system uses ten digits from 0 to 9 to represent numbers and the binary number system is a base-2 number system that uses only 0 and 1 to represent numbers. Algorithm to Convert De 2 min read
- How to print range of basic data types without any library function and constant in C? How to write C code to print range of basic data types like int, char, short int, unsigned int, unsigned char etc? It is assumed that signed numbers are stored in 2's complement form. We strongly recommend to minimize the browser and try this yourself first. Following are the steps to be followed fo 4 min read
- Bitwise Complement Operator (~ tilde) Pre-requisite:Bitwise Operators in C/ C++Bitwise Operators in JavaThe bitwise complement operator is a unary operator (works on only one operand). It takes one number and inverts all bits of it. When bitwise operator is applied on bits then, all the 1's become 0's and vice versa. The operator for th 3 min read
- showbits( ) Function in C with Examples Bitwise operators are operators (just like +, *, &&, etc.) that operate on ints and units at the binary level. This means they look directly at the binary digits or bits of an integer. This all sounds scary, but in truth, bitwise operators are quite easy to use and also quite useful! It is i 4 min read
- Builtin functions of GCC compiler These are four important built-in functions in GCC compiler: 1. __builtin_popcount(x) This function is used to count the number of one's(set bits) in an integer. Example: if x = 4 binary value of 4 is 100 Output: No of ones is 1. C // C program to illustrate _builtin_popcount(x) #include <stdio.h 3 min read
- Bitmasking In C In this article, we are going to learn about Bitmask in C which is a powerful way to basically work with bits and functions upon boolean logic. It can be used to store data compactly and with much more efficiency in certain cases. What is a Bit? A bit is the smallest unit of data which can either st 4 min read