How to Count Set Bits in an Integer in C++? (original) (raw)
Last Updated : 03 Jul, 2024
In binary representation of a number, a set bit is defined as the binary digit (bit) that is set to 1. In this article, we will learn how to count the set bits in a given integer in C++.
**Example
**Input: 13
**Output:
The number of set bits in 13 (1101) is: 3
Counting Set Bits in an Integer
To count the set bits in a number in C++, we can directly use the __builtin_popcount() method provided by the GCC compiler in C++. This function returns the number of set bits in a given unsigned integer.
Syntax
****__builtin_popcount**(int x);
Here, x is the unsigned or positive number for which you want to determine the number of set bits.
C++ Program to Count Number of Set Bits in an Integer
The following program illustrates how we can count the set bits of a number using the __builtin_popcount() method in C++.
C++ `
// C++ Program to Count Set Bits
#include #include using namespace std;
// Function to count set bits in an integer and print the // result void countAndPrintSetBits(int num) { // Calculate number of set bits using GCC's built-in // function for counting set bits int count = __builtin_popcount(num);
// Print the number of set bits and binary
// representation of the number
cout << "Number of set bits in " << num << " ("
<< bitset<32>(num).to_string().substr(
bitset<32>(num).to_string().find('1'))
<< ") is: " << count << endl;
}
int main() { // Declare the numbers int num1 = 5; int num2 = 13;
// Count and print set bits for num1
countAndPrintSetBits(num1);
// Count and print set bits for num2
countAndPrintSetBits(num2);
return 0;
}
`
Output
Number of set bits in 5 (101) is: 2 Number of set bits in 13 (1101) is: 3
**Time Complexity: O(1)
**Auxiliary Space: O(1)
To learn about other methods for counting set bits in an integer you may refer to this article : Counting Set Bits in an integer
Similar Reads
- How to Find the Size of a Set in Bytes in C++? In C++, sets are associative containers that only store unique elements. The elements inside the set are sorted in a specific order. In this article, we will learn how we can find the size of a set in bytes in C++. Example Input: S = {1,2,3,4}Output: Size of the set in bytes is : 16Find the Size of 2 min read
- How to Set, Clear, and Toggle a Single Bit in C++? In bit manipulation, setting a bit, clearing a bit, and toggling a single bit are basic operations. We can easily carry out these operations using the bitwise operators in C++. In this article, we will see how to set, clear, and toggle operations on the Kth bit. Example Input: N = 15, K = 0Output: S 3 min read
- Count total set bits in all numbers from 1 to N | Set 3 Given a positive integer N, the task is to count the total number of set bits in binary representation of all the numbers from 1 to N. Examples: Input: N = 3 Output: 4 setBits(1) + setBits(2) + setBits(3) = 1 + 1 + 2 = 4 Input: N = 6 Output: 9 Approach: Solution to this problem has been published in 12 min read
- Count total set bits in all numbers from 1 to n | Set 2 Given a positive integer N, the task is to count the sum of the number of set bits in the binary representation of all the numbers from 1 to N.Examples: Input: N = 3 Output: 4 DecimalBinarySet Bit Count101121013112 1 + 1 + 2 = 4Input: N = 16 Output: 33 Recommended PracticeCount total set bitsTry It! 11 min read
- How to Create Stack of Bitset in C++? In C++, a stack is a container adapter that provides a last-in, first-out (LIFO) type of data structure with two major operations, namely push and pop while Bitset is a container that can store N bits and provides constant-time operations to manipulate individual bits. In this article, we will learn 2 min read
- Count integers in an Array which are multiples their bits counts Given an array arr[] of N elements, the task is to count all the elements which are a multiple of their set bits count.Examples: Input : arr[] = { 1, 2, 3, 4, 5, 6 } Output : 4 Explanation : There numbers which are multiple of their setbits count are { 1, 2, 4, 6 }. Input : arr[] = {10, 20, 30, 40} 9 min read
- bitset count() in C++ STL bitset::count() is an inbuilt STL in C++ which returns the number of set bits in the binary representation of a number. Syntax: int count() Parameter: The function accepts no parameter. Return Value: The function returns the number of set bits. It returns the total number of ones or the number of se 2 min read
- How to Find the Size of a Set in C++? In C++, sets are STL containers that store unique elements of the same type in a sorted manner. No duplicate elements are allowed in the sets, as the value of every element in a set is unique. In this article, we will learn how we can find the size of a set container in C++. Example: Input: mySet={1 2 min read
- How to Count the Number of Occurrences of a Value in an Array in C++? In C++, an array is a data structure that stores the collection of the same type of data in a contiguous memory location. In this article, we will learn how to count the number of occurrences of a value in an array in C++. Example: Input: arr= {2, 4, 5 ,2 ,4 , 5, 2 , 3 ,8}Target = 2Output: Number of 2 min read
- How to Find the Frequency of an Element in a Set in C++? C++ STL provides a set container that can be used to store unique elements in a sorted order. In this article, we will learn how to find the frequency of an element in a set in C++. Example: Input:sets ={10,20,30,40,50}Output:Frequency of 30 is 1Finding Frequency of an Element in a Set in 2 min read