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:
#include <bits/stdc++.h>
using namespace std;
int main()
{
bitset<4> b1(string( "1100" ));
bitset<6> b2(string( "010010" ));
cout << b1 << " after applying flip() function returns " ;
cout << b1.flip() << endl;
cout << b2 << " after applying flip() function returns " ;
cout << b2.flip();
return 0;
}
|
Output:
0011 after applying flip() function returns 1100
101101 after applying flip() function returns 010010
Program 2:
#include <bits/stdc++.h>
using namespace std;
int main()
{
bitset<4> b1(string( "1100" ));
bitset<7> b2(string( "0100100" ));
cout << b1 << " after applying flip(3) function returns " ;
cout << b1.flip(3) << endl;
cout << b2 << " after applying flip(6) function returns " ;
cout << b2.flip(6);
return 0;
}
|
Output:
0100 after applying flip(3) function returns 1100
1100100 after applying flip(6) function returns 0100100
Last Updated :
06 Jun, 2020
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...