Last Updated : 23 Jul, 2025
In C++, the bitset is a container that represents a fixed-size sequence of bits. A bitset allows you to manipulate individual bits efficiently, making it useful in problems related to bitwise operations, such as checking flags, implementing binary representations.
Bitset is defined as the std::bitset class template inside the <bitset> header file.
Creating a BitsetThe syntax to create bitset is as follows:
C++
where, n is the number of bits to allocate, and name is the name assigned.
InitializingBy default, when a bitset of a given size is created, all of its bits are unset i.e. 0. We can initialize it to some value simply by passing it to its constructor.
C++
#include <bits/stdc++.h>
using namespace std;
int main() {
// Default Initialization
bitset<5> bnum(18);
cout << bnum;
return 0;
}
Here, the bitset bnum is of size 5 bits and stores the decimal value 18 in binary form.
Binary numbers are also represented as strings, so bitset also provide facility to initialize itself from the strings.
C++
#include <bits/stdc++.h>
using namespace std;
int main() {
// Initialize bitset with value
bitset<5> bs2("10010");
cout << bs2 << endl;
return 0;
}
The string should represent a valid binary number i.e. all characters should be 0 or 1. Otherwise, an error may occur.
Accessing Individual BitsWe access the bit at any position by using below methods:
Here, pos is the position or index of the bit starting from 0. It must be in the range (0 ≤ pos ≤ size – 1), otherwise, out of bound exception is thrown.
Example:
C++
#include <bits/stdc++.h>
using namespace std;
int main() {
// 18 = (10010)
bitset<5> bs(18);
// Check 3rd bit
cout << bs[2] << endl;
// Check 5th bit
cout << bs.test(4);
return 0;
}
Setting, Resetting and Flipping
Setting means making the bit at particular position 1 and resetting means making it 0. These operations can be done using set() and reset() function. The flip() function can be used to set the bit if it's not set and unset if it is set.
Example:
C++
#include <bits/stdc++.h>
using namespace std;
int main() {
bitset<5> bs(18);
// Set 1st bit
bs.set(0);
cout << bs << endl;
// Reset 2nd bit
bs.reset(1);
cout << bs << endl;
// Flip 5th bit
bs.flip(4);
cout << bs;
return 0;
}
Bitset Operators
Bitset objects can work with all the bitwise operators to provide seamless replacement integration in the code.
Operator
Operation
&
Bitwise AND|
Bitwise OR^
Bitwise XOR>>=
Binary Right shift and assign<<=
Binary Left shift and assign&=
Assign the value of bitwise AND to the first bitset.|=
Assign the value of bitwise OR to the first bitset.^=
Assign the value of bitwise XOR to the first bitset.~
Bitwise NOTExample:
C++
#include <bits/stdc++.h>
using namespace std;
int main() {
// 18 = (10010)
bitset<5> bs1(18);
// 5 = (00101)
bitset<5> bs2(5);
// AND Operator
cout << (bs1 & bs2) << endl;
// OR Operator
cout << (bs1 | bs2) << endl;
// XOR operator
cout << (bs1 ^ bs2);
return 0;
}
Explanation: In the above example, we perform AND, OR and XOR operator on bitsets:
Internal Working
In C++, bitset is implemented using an array or similar structure to store its bits. When you perform operations like set(), reset(), or flip(), they directly modify the bits in the internal array. The size of the bitset is fixed at compile-time, and it cannot be dynamically resized.
Why Bitset is PreferredBitsets are a preferred choice for bitwise operations and binary data handling due to the following reasons:
Vector of bool and array of bool can also be implemented to store a sequence of boolean values like bitset but there are some differences between each implementation:
Parameterbitset
vector of bool
array of bool
Definition A class template consisting of a sequence of bits stored such that each bit occupies 1 bit of memory. A variation of vectors of C++ STL in which each element is of size 1 bit and is of type bool A fixed size contiguous collection of bool data elements. Size Fixed Size. Dynamic Size. Fixed Size. Memory A single element occupies 1 bit of memory. A single element occupies 1 bit of memory. A single element occupies 1 byte of memory. Speed Same Same Faster All Member FunctionsHere’s the list of all member functions of std::bitset:
Function
Description
Set the bit value at the given index to 1. Set the bit value at a given index to 0. Flip the bit value at the given index. Count the number of set bits. Returns the boolean value at the given index. Checks if any bit is set. Checks if all bits are unset. Check if all bit is set. Returns the size of the bitset.to_string()
Converts bitset to string. Converts bitset to unsigned long. Converts bitset to unsigned long long.RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4