A bitset is an array of bool but each Boolean value is not stored separately instead bitset optimizes the space such that each bool takes 1 bit space only, so space taken by bitset bs is less than that of bool bs[N] and vector bs(N). However, a limitation of bitset is, N must be known at compile time, i.e., a constant (this limitation is not there with vector and dynamic array)
As bitset stores the same information in compressed manner the operation on bitset are faster than that of array and vector. We can access each bit of bitset individually with help of array indexing operator  that is bs shows bit at index 3 of bitset bs just like a simple array. Remember bitset starts its indexing backward that is for 10110, 0 are at 0th and 3rd indices whereas 1 are at 1st 2nd and 4th indices.
We can construct a bitset using integer number as well as binary string via constructors which is shown in below code. The size of bitset is fixed at compile time that is, it can’t be changed at runtime.
The main function defined for bitset class are operator , count, size, set, reset and many more they are explained in below code –
00000000000000000000000000000000 00000000000000000000000000010100 00000000000000000000000000001100 00010010 00010010 has 2 ones and 6 zeros bool representation of 00010010 : 0 1 0 0 1 0 0 0 bset1 has no bit set. 11111111 11101111 11111111 11111011 00000000 00000100 11111011 Decimal number: 100 Binary equivalent: 01100100
For bitset set, reset and flip function are defined. Set function sets (1) all bits of bitset if no argument is provided otherwise it sets the bit whose position is given as argument. In same way reset and flip also work if they are called with no argument they perform their operation on whole bitset and if some position is provided as argument then they perform operation at that position only.
For bitset all bitwise operator are overloaded that is they can be applied to bitset directly without any casting or conversion, main overloaded operator are &, |, ==, != and shifting operator <> which makes operation on bitset easy.
Use of above operator is shown in below code.
0 1 1010 0010 0011 1100 0110 1100 0010 0111 0101
This article is contributed by Utkarsh Trivedi. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- std::bitset::to_ullong and std::bitset::to_ulong in C++ STL
- unordered_multimap and its application
- is_permutation() in C++ and its application for anagram search
- isspace() in C/C++ and its application to count whitespace characters
- iscntrl() in C++ and its application to find control characters
- Count number of common elements between two arrays by using Bitset and Bitwise operation
- Subset sum queries using bitset
- C++ bitset interesting facts
- bitset size() in C++ STL
- bitset::flip() in C++ STL
- bitset none() in C++ STL
- bitset count() in C++ STL
- bitset any() in C++ STL
- bitset all() function in C++ STL
- bitset set() function in C++ STL
- bitset operator in C++ STL
- bitset reset() function in C++ STL
- bitset test() in C++ STL
- Subset Sum Queries in a Range using Bitset
- Count number of set bits in a range using bitset
Improved By : yekeparampir