The C++ bitset function allows for accessing the value at any index within a bitset.
Syntax
Example
boolean operator [] (int pos);
reference operator[] (int pos);
Parameter
The parameter index indicates the location where the value should be assigned.
Return value
The function retrieves the value corresponding to the bit located at the specified index.
Example 1
Example
#include <iostream>
#include <bitset>
using namespace std;
int main()
{
bitset<4> b;
b[1]=1;
b[2]=b[1];
cout<< "b :" << b;
return 0;
}
Output:
Output
b :0110
Example 2
Example
#include <iostream>
#include <bitset>
using namespace std;
int main()
{
bitset<4> b(string("1001"));
b[1]=1;
b[2]=b[1];
cout<< "b :" << b;
return 0;
}
Output:
Output
b :1111