public member function
<bitset>

std::bitset::set

all bits (1)
bitset& set();
single bit (2)
bitset& set (size_t pos, bool val = true);
all bits (1)
bitset& set() noexcept;
single bit (2)
bitset& set (size_t pos, bool val = true);
Set bits
Sets bits:
(1) all bits
Sets (to one) all bits in the bitset.
(2) single bit
Sets val as the value for the bit at position pos.

Parameters

pos
Order position of the bit whose value is modified.
Order positions are counted from the rightmost bit, which is order position 0.
If pos is equal or greater than the bitset size, an out_of_range exception is thrown.
size_t is an unsigned integral type.
val
Value to store in the bit (either true for one or false for zero).

Return value

*this

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// bitset::set
#include <iostream>       // std::cout
#include <bitset>         // std::bitset

int main ()
{
  std::bitset<4> foo;

  std::cout << foo.set() << '\n';       // 1111
  std::cout << foo.set(2,0) << '\n';    // 1011
  std::cout << foo.set(2) << '\n';      // 1111

  return 0;
}

Output:

1111
1011
1111


Data races

Both the bitset and its bits are modified.

Exception safety

For (1): it never throws exceptions (no-throw guarantee).
For (2): in case of exception, the object is in a valid state (basic guarantee).
If pos is equal or greater than the bitset size, the function throws an out_of_range exception.

See also