public member function
<bitset>

std::bitset::flip

all bits (1)
bitset& flip();
single bit (2)
bitset& flip (size_t pos);
all bits (1)
bitset& flip() noexcept;
single bit (2)
bitset& flip (size_t pos);
Flip bits
Flips bit values converting zeros into ones and ones into zeros:
(1) all bits
Flips all bits in the bitset.
(2) single bit
Flips the bit at position pos.

Parameters

pos
Order position of the bit whose value is flipped.
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.

Return value

*this

Example

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

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

  std::cout << foo.flip(2) << '\n';     // 0101
  std::cout << foo.flip() << '\n';      // 1010

  return 0;
}

Output:

0101
1010


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