bitset count() in C++ STL Last Updated : 18 Jun, 2018 Comments Improve Suggest changes Like Article Like Report 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 set bits in the binary representation of the number if the passed number is an integer. Below programs illustrates the bitset::count() function. Program 1: CPP // CPP program to illustrate the // bitset::count() function #include <bits/stdc++.h> using namespace std; int main() { // Initialisation of a bitset bitset<4> b1(string("1100")); bitset<6> b2(string("001000")); // Function to count the // number of set bits in b1 int result1 = b1.count(); cout << b1 << " has " << result1 << " set bit\n"; // Function to count the // number of set bits in b2 int result2 = b2.count(); cout << b2 << " has " << result2 << " set bit"; return 0; } Output: 1100 has 2 set bit 001000 has 1 set bit Program 2: CPP // CPP program to illustrate the // bitset::count() function // when the input is an integer #include <bits/stdc++.h> using namespace std; int main() { // Initialisation of a bitset bitset<4> b1(16); bitset<4> b2(18); // Function to count the // number of set bits in b1 int result1 = b1.count(); cout << b1 << " has " << result1 << " set bit\n"; // Function to count the // number of set bits in b2 int result2 = b2.count(); cout << b2 << " has " << result2 << " set bit"; return 0; } Output: 0000 has 0 set bit 0010 has 1 set bit Comment More infoAdvertise with us Next Article bitset count() in C++ STL G gopaldave Follow Improve Article Tags : Misc C++ Programs C++ STL CPP-Functions CPP-bitset +2 More Practice Tags : CPPMiscSTL Similar Reads count_if() in C++ STL count_if() function returns the number of elements in a range that satisfy the condition. Syntax: template <class InputT, class UnaryPredicate> typename iterator_traits <InputT> :: difference_type count_if(InputT first, InputT last, UnaryPredicate p); Examples: Input: 0 1 2 3 4 5 6 7 8 9 2 min read bitset test() in C++ STL bitset::test() is an inbuilt function in C++ STL which tests whether the bit at a given index is set or not. Syntax: bitset_name.test(index) Parameters: The function accepts only a single mandatory parameter index which specifies the index at which the bit is set or not. Return Value: The function r 2 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 How to Count Set Bits in an Integer in C++? 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++.ExampleInput: 13Output:The number of set bits in 13 (1101) is: 3Counting Set Bits in an IntegerTo count the set bi 2 min read C++ __builtin_popcount() Function __builtin_popcount() is a built-in function of GCC compiler. This function is used to count the number of set bits in an unsigned integer. In other words, it counts the number of 1's in the binary form of a positive integer. Syntax: __builtin_popcount(int number); Parameter: This function only takes 2 min read Like