bitset::flip() in C++ STL

The bitset flip() method is an inbuilt method of C++ STL( Standard Template Library). It flips the bits of the calling bitset. This method flips all 0’s to 1’s and all 1’s to 0’s, which means it reverse each and every bit of the calling bitset when no parameter is passed.

If a parameter is passed the flip method will flip only the nth bit for the integer n passed. For example, if 5 is passed then the flip method will flip 5th bit of of the calling bitset.

Syntax

bitset_name.flip(int pos)

Sample

Initial bitset: 011001

After applying the bits flip function with no parameter: 100110

After applying the Beta function with 6: 011000

Example

#include <bits/stdc++.h>
using namespace std;
int main() {
bitset<6> bit1(string("100110"));
bitset<10> bit2(string("0100001111"));
cout << bit1 << " after applying flip() function with nothing passed as parameter returns "
<< bit1.flip() << endl;
cout << bit2 << " after applying flip() function with 7 passed as parameter returns "
<< bit2.flip(7);
return 0;
}

Output

100110 after applying flip() function with nothing passed as parameter returns 011001
0100001111 after applying flip() function with 7 passed as parameter returns 0110001111
Published on 09-Aug-2019 12:18:27