In C++, a bitset is a container that holds a fixed-size sequence of bits (0s and 1s). It enables us to manage and manipulate a fixed-size sequence of bits efficiently. It is useful for low-level operations, memory-efficient storage, and bitwise logic. It is implemented as a class template and defined in the <bitset> header file within the std namespace.
It has the following syntax:
In this syntax, N is the number of bits that we want to store, and b is the name that is assigned.
In C++, we can initialize the bitset function in several ways based on their use cases. A bitset is a fixed-size sequence of N bits (such as 0's and 1's), where we can manipulate each bit individually. When we create a bitset, all bits are unset (0) by default. However, we may initialize it with definite values during its declaration. Several initializations of bitset in C++ are as follows:
Here, the bitset is of size 7 bits and
We can also initialize a bitset with an unsigned long value. In the unsigned long, the binary representation of the number is stored from right to left.
The string must have the same or fewer characters than the size of the bitset. Extra bits are truncated from the left.
This example demonstrates different ways to initialize a bitset using default values, integers, strings, and string objects.
Output:
b1: 00000000 b2: 00001111 b3: 00001111 b4: 10101010
Explanation
In this example, we demonstrate how to initialize std::bitset<8> in different ways. First, b1 is default-initialized (all bits 0). b2 employs the integer 15, which is represented in binary as 00001111. b3 employs a binary string "00001111". b4 is utilizing a std::string with bits "10101010".
In C++, the std::bitset container allows efficient access and manipulation of individual bits at any position. There are two main methods to access bits.
The pos represents the index or position of the bit from 0. It should be in the range (0 ≤ pos ≤ size - 1); otherwise, a bound exception is raised.
Let us take an example to illustrate how to access bits in C++.
Output:
Bitset: 11001001 Bit at position 0: 1 Bit at position 5: 0 Bit at position 4 is not set (0). Modified Bitset: 11000001
Explanation
In this example, we have taken a bitset of 8 bits that is initialized with the binary value "11001001". After that, we use the [] operator and the test() function. These functions check specific bit positions and print their values, while the test() function is used to check if a bit is set.
In C++, the std::bitset offers many useful functions to work individual or all bits. We can use the set() function to set the bits to 1, and use the reset() function to clear bits by setting the bit to 0. After that, the flip() function can also be utilized to toggles the bit's value. If the bit is 1, it becomes 0, and if it is 0, it becomes 1.
Let us take an example to illustrate how we can set, reset, and flip bits in C++.
Output:
Original: 00001100 After set(0): 00001101 After set(): 11111111 After reset(3):11110111 After reset(): 00000000 After flip(1): 00000010 After flip(): 11111101
Explanation
In this example, we have taken the set(), reset(), and flip() functions that work on individual bits and the entire bitset. These operations allow us to turn bits on and off or toggle them efficiently in a fixed-size binary sequence.
There are several functions of Bitset in C++. Some of them are as follows:
| Function | Description |
|---|---|
| all() | It is used to test and check whether all the bits are set. |
| any() | It is used to check if any bit is set. |
| count() | It is used to count the number of set bits. |
| flip() | It is used to flip the bit value at the provided index. |
| none() | It is used to check if all bits are unset. |
| operator[] | It is used to enable access to individual bits by index. |
| reset() | It is used to set the value of bit at a given index to 0. |
| set() | It is used to set the value of bit at a given index to 1. |
| size() | It is used to return the size of bitset. |
| test() | It is used to return the Boolean value at a given index. |
| to_string() | It is used to convert the bitset to string. |
| to_ullong() | It is used to convert the bitset to unsigned long long. |
| to_ulong()() | It is used to convert the bitset to unsigned long. |
Now, we will discuss these functions one by one.
In C++ bitsets, this operation is utilized for testing and checking whether all the bits are set appropriately.
It has the following syntax:
It returns true if every bit in the bitset is 1; otherwise false.
Let us take an example to illustrate the bitset::all() function in C++.
Output:
b1.all(): 1 b2.all(): 0
Explanation
In this example, we demonstrate the .all() member function in std::bitset. It consists of the appropriate headers and declares two 8-bit bitsets. The initial bitset b1 has all its bits set to 1 (11111111). The second bitset, b2, has only its first four bits set (11110000). The b1.all() function returns true since all bits are set, but b2.all() returns false because not all bits are set.
In C++, the .any() method of std::bitset returns whether at least one bit is equal to 1.
Let us take an example to illustrate the bitset::any() function in C++.
Output:
b1.any(): 0 b2.any(): 1
Explanation
In this example, we have taken two 8-bit bitsets, b1 with all the bits as 0 and b2 with a single bit set as 1 at position 4 from the right side. After that, the any() method is invoked on both the bitsets. For b1, any() function gives false (0) since all bits are 0. For b2, any() gives true (1) because it has at least one bit as 1.
Let us take an example to illustrate the count() and none() functions in C++.
Output:
b1: 00000000 b1.count(): 0 b1.none(): 1 b2: 10101010 b2.count(): 4 b2.none(): 0
Explanation
In this example, the expression b1.count() gives the number of 1s set in b1, which is 0. b1.none() returns true as there are no bits set to 1 in b1. For b2, count() gives 4, as there are four 1s in the bitset. After that, b2.none() returns false as not all bits are set to 0. Each result is displayed to the console for both bitsets. The program terminates by returning 0.
Let us take an example to illustrate the operator[], size(), and test function in C++.
Output:
Bitset: 10110010 Size: 8 b[2]: 0 b.test(2): 0
Explanation
In this example, we start by initializing an 8-bit bitset b with the binary value "10110010". After that, the bitset is displayed to reveal its contents. The size() member function is invoked to retrieve the number of bits in the bitset, which is 8. Subsequently, b[2] is employed to refer to the bit at position 2 (from the right), and it returns 0. After that, b.test(2) is employed to check the value of the same bit safely, and it returns 0.
Let us take an example to illustrate the to_string, to_ullong, and to_ulong function in C++.
Output:
Bitset: 10101010 to_string(): 10101010 to_ulong(): 170 to_ullong(): 170
Explanation
In this example, an 8-bit bitset b is created with the binary value 10101010. The to_string() function is used to convert the bitset to a string of characters '1' and '0', which is stored in the variable s. After that, the to_ulong() operation converts the bitset to an unsigned long integer, and to_ullong() converts it to an unsigned long long integer. As 10101010 in binary is the same as 170 in decimal, both numeric conversions yield the value 170.
In C++, bitsets are a class of choice to perform bitwise operations and manipulate binary data because of the following:
We request you to subscribe our newsletter for upcoming updates.

We deliver comprehensive tutorials, interview question-answers, MCQs, study materials on leading programming languages and web technologies like Data Science, MEAN/MERN full stack development, Python, Java, C++, C, HTML, React, Angular, PHP and much more to support your learning and career growth.
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India