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";
}
}
運行結果: