bitset operator[] in C++ STL
Last Updated :
12 Nov, 2020
bitset::operator[] is a built-in function in C++ STL which is used to assign value to any index of a bitset.
Syntax:
bitset_operator[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:
C++
#include <bits/stdc++.h>
using namespace std;
int main()
{
bitset<6> b;
b[2] = 1;
b[5] = 1;
cout << "The bitset after assigning values is: " << b;
return 0;
}
|
Output:
The bitset after assigning values is: 100100
Program 2:
C++
#include <bits/stdc++.h>
using namespace std;
int main()
{
bitset<6> b;
b[2] = 1;
b[3] = b[2];
b[0] = b[5];
cout << "The bitset after assigning values is: " << b;
return 0;
}
|
Output:
The bitset after assigning values is: 001100
Program 3:
C++
#include <bits/stdc++.h>
using namespace std;
int main()
{
bitset<6> b;
b[2] = 1;
b[3] = b[2];
b[0] = b[5];
cout << "The bit value at index 3 is " << b[3];
return 0;
}
|
Output:
The bit value at index 3 is 1
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...