bitset::flip:反转所有位,或者指定的位。
Toggles the value of all the bits in a bitset or toggles a single bit at a specified position.
反转:原来是1,反转后就是0;如果原来是0,toggle后就是1.
不带参数调用,就是反转所有位。
带参数,即是从右边数0开始数,反转第几位。(注意:两点 1是从右边数,2是从0开始)
// bitset_flip.cpp
// compile with: /EHsc
#include <bitset>
#include <iostream>
int main( )
{
using namespace std;
bitset<5> b1 ( 6 );
cout << "The collection of bits in the original bitset is: ( "
<< b1 << " )" << endl;
bitset<5> fb1;
fb1 = b1.flip ( );
cout << "After flipping all the bits, the bitset becomes: ( "
<< fb1 << " )" << endl;
bitset<5> f3b1;
f3b1 = b1.flip ( 0 );
cout << "After flipping the fourth bit, the bitset becomes: ( "
<< f3b1 << " )" << endl << endl;
bitset<5> b2;
int i;
for ( i = 0 ; i <= 4 ; i++ )
{
b2.flip(i);
cout << b2 << " The bit flipped is in position "
<< i << ".\n";
}
}
运行结果: