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.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- std::bitset::to_ullong and std::bitset::to_ulong in C++ STL
- bitset any() in C++ STL
- bitset none() in C++ STL
- bitset operator in C++ STL
- bitset test() in C++ STL
- bitset set() function in C++ STL
- bitset count() in C++ STL
- bitset all() function in C++ STL
- bitset::flip() in C++ STL
- bitset size() in C++ STL
- Subset sum queries using bitset
- bitset reset() function in C++ STL
- C++ bitset interesting facts
- _Find_first() function in C++ bitset with Examples
- _Find_next() function in C++ bitset with Examples
- Subset Sum Queries in a Range using Bitset
- Count number of set bits in a range using bitset
- Count number of common elements between two arrays by using Bitset and Bitwise operation
- unordered_multimap and its application
- fesetround() and fegetround() in C++ and their application
Improved By : yekeparampir