bitset::operator[] is a built-in function in C++ STL which is used to assign value to any index of a bitset.
Syntax:
bitset_name[index] = value
Parameter: The parameter index specifies the position at which the value is to be assigned.
Return Value: The function returns the value to the bit at the index.
Below programs illustrate the bitset::operator[] function.
Program 1:
// CPP program to illustrate the // bitset::operator[] #include <bits/stdc++.h> using namespace std; int main() { // Initialization of bitset // by zeros of size 6 bitset<6> b; // initialises second index as 1 b[2] = 1; // initialises fifth index as 1 b[5] = 1; // prints the bitset cout << "The bitset after assigning values is: " << b; return 0; } |
The bitset after assigning values is: 100100
Program 2:
// CPP program to illustrate the // bitset::operator[] // assign value from a index #include <bits/stdc++.h> using namespace std; int main() { // Initialization of bitset // by zeros of size 6 bitset<6> b; // initialises second index as 1 b[2] = 1; // initialises fifth index as the same as b[2] b[3] = b[2]; b[0] = b[5]; // prints the bitset cout << "The bitset after assigning values is: " << b; return 0; } |
The bitset after assigning values is: 001100
Program 3:
// CPP program to illustrate the // bitset::operator[] // prints the value of bit at index #include <bits/stdc++.h> using namespace std; int main() { // Initialization of bitset // by zeros of size 6 bitset<6> b; // initialises second index as 1 b[2] = 1; // initialises fifth index as the same as b[2] b[3] = b[2]; b[0] = b[5]; cout << "The bit value at index 3 is " << b[3]; return 0; } |
The bit value at index 3 is 1
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.
Recommended Posts:
- std::bitset::to_ullong and std::bitset::to_ulong in C++ STL
- Why overriding both the global new operator and the class-specific operator is not ambiguous?
- Operator Overloading '<<' and '>>' operator in a linked list class
- bitset any() in C++ STL
- bitset none() in C++ STL
- bitset size() in C++ STL
- bitset test() in C++ STL
- C++ bitset and its application
- bitset count() in C++ STL
- bitset::flip() in C++ STL
- bitset set() function in C++ STL
- bitset all() function in C++ STL
- C++ bitset interesting facts
- bitset reset() function in C++ STL
- Subset sum queries using bitset
- _Find_first() function in C++ bitset with Examples
- _Find_next() function in C++ bitset with Examples
- Count number of set bits in a range using bitset
- Count number of common elements between two arrays by using Bitset and Bitwise operation
- dot (.) operator in C/C++
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

