bitset::flip() in C++ STL
bitset::flip() is a built-in STL in C++ which flips the bits. If no parameter is passed in the function, then it flips all the bit values converting zeros to ones and ones to zeros. If a parameter position is passed, it flips the bit at the position only.
Syntax:
bitset_name.flip(int pos)
Parameter: The function accepts a parameter pos which is not mandatory. If a parameter pos is passed, it flips the bit at the index pos only(the index pos is calculated starting from the right). In case no parameter is passed, it flips all the bit values converting zeros to ones and ones to zeros.
Return Value: The function flips all the bit values according to the parameter passed or not and returns the new binary representation of the number.
Below programs illustrates the bitset::flip() function.
Program 1:
// CPP program to illustrate the // bitset::flip() function // when no parameter is passed #include <bits/stdc++.h> using namespace std; int main() { // Initialization of bitset bitset<4> b1(string("1100")); bitset<6> b2(string("010010")); // Printing the bitset after flipping the bits cout << b1 << " after applying flip() function returns " << b1.flip() << endl; cout << b2 << " after applying flip() function returns " << b2.flip(); return 0; } |
0011 after applying flip() function returns 0011 101101 after applying flip() function returns 101101
Program 2:
// CPP program to illustrate the // bitset::flip() function // when parameter is passed #include <bits/stdc++.h> using namespace std; int main() { // Initialization of bitset bitset<4> b1(string("1100")); bitset<7> b2(string("0100100")); // Printing the bitset after flipping the bits cout << b1 << " after applying flip(3) function returns " << b1.flip(3) << endl; cout << b2 << " after applying flip(6) function returns " << b2.flip(6); return 0; } |
0100 after applying flip(3) function returns 0100 1100100 after applying flip(6) function returns 1100100
Recommended Posts:
- fill in C++ STL
- Conditional or Ternary Operator (?:) in C/C++
- forward_list insert_after() function in C++ STL
- Count of distinct remainders when N is divided by all the numbers from the range [1, N]
- C++ | asm declaration
- Pointers and References in C++
- Strings in C++ and How to Create them?
- Enum Classes in C++ and Their Advantage over Enum DataType
- Deque vs Vector in C++ STL
- C++ Programming Basics
- Introduction to C++ Programming Language
- ios manipulators noshowpos() function in C++
- ios rdstate() function in C++ with Examples
- ios operator !() function in C++ with Examples
- ios operator() function in C++ with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



