C++ Bitset Library - flip() Function



Description

The C++ function std::bitset::flip() toggles single bit from bitset.

Declaration

Following is the declaration for std::bitset::flip() function form std::bitset header.

C++98

bitset& flip (size_t pos);

C++11

bitset& flip (size_t pos);

Parameters

pos − Position of the bit whose value is flipped.

Return value

Returns this pointer.

Exceptions

Throws out_of_range exception if pos is greater than or equal to bitset size.

Example

The following example shows the usage of std::bitset::flip() function.

#include <iostream>
#include <bitset>

using namespace std;

int main(void) {

   bitset<4> b("1110");

   cout << "Before flip operation b = " << b << endl;

   b.flip(1);

   cout << "After flip operation b = " << b << endl;

   return 0;
}

Let us compile and run the above program, this will produce the following result −

Before flip operation b = 1110
After flip operation b = 1100
bitset.htm
Advertisements