Share via


bitset 類別

描述物件類型,其可儲存由固定位元數所組成的序列,以提供精簡的方式,來保留一組項目或條件的旗標。 類別 bitset 支援包含位集合之 bitset 型別物件的作業,並提供每個位的常數時間存取。

語法

template <size_t N>
class bitset

參數

N
指定 物件中 bitset 具有類型非零整數的位數,該整數 size_t 必須在編譯時期知道。

備註

不同于類似的 vector<bool> 類別 ,類別 bitset 沒有反覆運算器,也不是 C++ 標準程式庫容器。 vector<bool>它也不同于在編譯時期根據宣告 時樣板參數 Nbitset<N> 所指定的大小,在編譯時期固定的特定大小。

如果位元值為 1 則設定位元,如果其值為 0 則重設。 若要翻轉或反轉位元,方法是從 1 到 0 或從 0 到 1 變更其值。 N中的 bitset 位是由介於 0 到 N - 1 的整數值編制索引,其中 0 會編制第一個位位置的索引,而 N - 1 是最終位位置。

成員

建構函式

名稱 描述
bitset 建構 bitset<N> 類別的物件並將此位元初始化為零、某指定值或字串字元中取得的值。

Typedefs

名稱 描述
element_type 資料類型 bool 同義字的類型,可以用來參考 bitset 中的項目位元。

函式

名稱 描述
all 測試這個 bitset 中的所有位,以判斷它們是否全部設定為 true
any 此成員函式會測試該序列中的任何位元是否設為 1。
count 此成員函式會傳回位元序列中設定的位元數。
flip 反轉 bitset 中的所有位元值,或在指定位置反轉單一位元。
none 測試在 bitset 物件中是否沒有已設為 1 的位元。
reset bitset 中的所有位元重設為 0,或將指定位置的位元重設為 0。
set bitset 中的所有位元設為 1,或將指定位置的位元設為 1。
size 傳回 bitset 物件中的位元數。
test 測試位於 bitset 中指定位置的位元是否設為 1。
to_string bitset 物件轉換為字串表示。
to_ullong unsigned long long 傳回 bitset 中的位元值總和。
to_ulong bitset 物件轉換成 unsigned long,如果用來初始化 bitset,其可能產生所包含位元的序列。

類別

名稱 描述
reference Proxy 類別,提供參考給包含於 bitset 中的位元,而 bitset 是用來存取及管理做為類別 bitsetoperator[] 協助程式類別的個別位元。

操作員

名稱 描述
operator!= 測試目標 bitset 是否與指定的 bitset 不相等。
operator&= 執行位集與位 「and」 ( & ) 運算的位組合。
operator<< 以位置的指定數目將 bitset 中的位元移位至左邊,並傳回結果至新的 bitset
operator<<= 以位置的指定數目將 bitset 中的位元移位至左邊,並傳回結果至目標的 bitset
operator== 測試目標 bitset 是否與指定之 bitset 相等。
operator>> 以位置的指定數目將 bitset 中的位元移位至右邊,並傳回結果至新的 bitset
operator>>= 以位置的指定數目將 bitset 中的位元移位至右邊,並傳回結果至目標的 bitset
operator[] 如果 bitset 可修改,則傳回位於 bitset 中指定位置的位元參考;否則它會傳回該位置的位元值。
operator^= 執行位集與位 「xor」 ( ^ ) 運算的位組合。
operator|= 執行位集與位 「or」 ( | ) 運算的位組合。
operator~ 反轉目標 bitset 中的所有位元,並傳回結果。

結構

名稱 描述
hash

all

測試此 bitset 中的所有位,以判斷它們是否全部設定為 true。

bool all() const;

傳回值

如果此集合中的所有位為 true,則傳 true 回 。 如果一或多個位為 false,則傳 false 回 。

any

測試序列中是否有任何位元設為 1。

bool any() const;

傳回值

true 如果此 bitset 中的任何位設定為 1,則為 ; false 如果所有位都是 0,則為 。

範例

// bitset_any.cpp
// compile with: /EHsc
#include <bitset>
#include <iostream>

int main( )
{
   using namespace std;

   bitset<5> b1 ( 6 );
   bool b, rb;

   cout << "The original bitset b1( 6 ) is: ( "<< b1 << " )"
        << endl;

   b = b1.any ( );

   if ( b )
      cout << "At least one of the bits in bitset is set to 1."
           << endl;
   else
      cout << "None of the bits in bitset are set to 1."
           << endl;

   bitset<5> rb1;
   rb1 = b1.reset ( );

   cout << "The reset bitset is: ( "<< b1 << " )"
        << endl;

   rb = rb1.any ( );

   if ( rb )
      cout << "At least one of the bits in the reset bitset "
           << "are set to 1." << endl;
   else
      cout << "None of the bits in bitset b1 are set to 1."
           << endl;
}
The original bitset b1( 6 ) is: ( 00110 )
At least one of the bits in bitset is set to 1.
The reset bitset is: ( 00000 )
None of the bits in bitset b1 are set to 1.

bitset

建構 bitset<N> 類別的物件,並將此位元初始化為零、某指定值或字串字元中取得的值。

1) constexpr bitset();
2) bitset(unsigned long val);
3) constexpr bitset(unsigned long long val);
4) template <class CharType, class Traits, class Allocator>
    explicit bitset(
        const basic_string<CharType, Traits, Allocator>& str,
        typename basic_string<CharType, Traits, Allocator>::size_type pos = 0);
    
5) template <class CharType, class Traits, class Allocator>
    explicit bitset(
        const basic_string<CharType, Traits, Allocator>& str,
        typename basic_string<CharType, Traits, Allocator>::size_type pos,
        typename basic_string<CharType, Traits, Allocator>::size_type count,
        CharType Zero = CharType ('0'),
        CharType One = CharType ('1'));
    
6) template<class CharType>
    explicit bitset(
        const CharType* str,
        typename basic_string<CharType>::size_type
        n = basic_string<CharType>::npos,
        CharType zero = CharType('0'),
        CharType one = CharType('1'));

參數

val
不帶正負號的整數,其基底兩個標記法用來初始化所建構中的 bitset 位。

str
用來初始化 bitset 位值的零和一個字串。

pos
字串中字元的位置,從左至右計算,並以零開始,用來初始化 中的 bitset 第一個位。

count
字串中用來提供 中位 bitset 的初始值字元數。

Zero
用來表示零的字元。 預設值為 '0'。

One
用來表示一的字元。 預設值為 '1'。

備註

1) 建構 類別 bitset<N> 的物件,並將所有 N 位初始化為預設值為零。

2-3) 建構 類別 bitset<N> 的物件,並從 參數初始化位 val

4) 建構 類別 bitset<N> 的物件,並從零和一個字串中提供的字元初始化位。 如果字串的任何字元都非 0 或 1,建構函式會擲回 類別 invalid argument 的物件。 如果指定的位置 ( pos ) 超出字串的長度,則建構函式會擲回 類別 out_of_range 的 物件。 建構函式只會在 位置 j 的位置 bitsetpos + j 設定字元為 1 的位。 pos 預設為 0。

5) 類似于 4) ,但包含另一個 參數, count 指定要初始化的位數。 它有兩個選擇性參數和 _Zero_One ,表示 中 str 應分別解譯為 0 位和 1 位的字元。

6) 建構 類別 bitset<N> 的物件,將 N 位初始化為對應至零和一個 C 樣式字元字串中提供的字元的值。 您可以呼叫建構函式,而不將字串轉換成字串類型,例如: bitset<5> b5("01011");

範例

// bitset_bitset.cpp
// compile with: /EHsc
#include <bitset>
#include <iostream>

int main( )
{
   // Using the default constructor
   using namespace std;
   bitset<2> b0;
   cout << "The set of bits in bitset<2> b0 is: ( "
        << b0 << " )." << endl;

   // Using the second member function
   bitset<5> b1 ( 6 );
   cout << "The set of bits in bitset<5> b1( 6 ) is: ( "
        << b1 << " )." << endl;

   // The template parameter N can be an expression
   bitset< 2 * sizeof ( int ) > b2;
   cout << "The set of bits in bitset< 2 * sizeof ( int ) > b2 is: ( "
        << b2 << " )." << endl;

   // The base two representation will be truncated
   // if its length exceeds the size of the bitset
   bitset<3> b3 ( 6 );
   cout << "The set of bits in bitset<3> b3( 6 ) is ( "
        << b3 << " )." << endl;

   // Using a c-style string to initialize the bitset
    bitset<7> b3andahalf ( "1001001" );
    cout << "The set of bits in bitset<7> b3andahalf ( \"1001001\" )"
         << " is ( " << b3andahalf << " )." << endl;

   // Using the fifth member function with the first parameter
   string bitval4 ( "10011" );
   bitset<5> b4 ( bitval4 );
   cout << "The set of bits in bitset<5> b4( bitval4 ) is ( "
        << b4 << " )." << endl;

   // Only part of the string may be used for initialization

   // Starting at position 3 for a length of 6 (100110)
   string bitval5 ("11110011011");
   bitset<6> b5 ( bitval5, 3, 6 );
   cout << "The set of bits in bitset<11> b5( bitval, 3, 6 ) is ( "
        << b5 << " )." << endl;

   // The bits not initialized with part of the string
   // will default to zero
   bitset<11> b6 ( bitval5, 3, 5 );
   cout << "The set of bits in bitset<11> b6( bitval5, 3, 5 ) is ( "
        << b6 << " )." << endl;

   // Starting at position 2 and continue to the end of the string
   bitset<9> b7 ( bitval5, 2 );
   cout << "The set of bits in bitset<9> b7( bitval, 2 ) is ( "
        << b7 << " )." << endl;
}
The set of bits in bitset<2> b0 is: ( 00 ).
The set of bits in bitset<5> b1( 6 ) is: ( 00110 ).
The set of bits in bitset<2 * sizeof ( int ) > b2 is: ( 00000000 ).
The set of bits in bitset<3> b3( 6 ) is ( 110 ).
The set of bits in bitset<5> b4( bitval4 ) is ( 10011 ).
The set of bits in bitset<11> b5( bitval, 3, 6 ) is ( 100110 ).
The set of bits in bitset<11> b6( bitval5, 3, 5 ) is ( 00000010011 ).
The set of bits in bitset<9> b7( bitval, 2 ) is ( 110011011 ).

count

傳回位元序列中設定的位元數。

size_t count() const;

傳回值

位元序列中設定的位元數。

範例

// bitset_count.cpp
// compile with: /EHsc
#include <bitset>
#include <iostream>

int main( )
{
    using namespace std;

    bitset<5> b1(4);

    cout << "The collection of bits in the original bitset is: ( "
         << b1 << " )" << endl;

    size_t i;
    i = b1.count();
    cout << "The number of bits in the bitset set to 1 is: "
         << i << "." << endl;

    bitset<5> fb1;
    fb1 = b1.flip();

    cout << "The collection of flipped bits in the modified bitset "
         << "is: ( " << b1 << " )" << endl;

    size_t ii;
    ii = fb1.count();
    cout << "The number of bits in the bitset set to 1 is: "
         << ii << "." << endl;
}
The collection of bits in the original bitset is: ( 00100 )
The number of bits in the bitset set to 1 is: 1.
The collection of flipped bits in the modified bitset is: ( 11011 )
The number of bits in the bitset set to 1 is: 4.

element_type

資料類型 bool 同義字的類型,可以用來參考 bitset 中的項目位元。

typedef bool element_type;

範例

// bitset_elem_type.cpp
// compile with: /EHsc
#include <bitset>
#include <iostream>

int main( )
{
   using namespace std;

   bitset<3> b1 ( 2 );
   cout << "Original bitset b1(6) is: ( "<< b1 << " )"
        << endl;

   //Compare two ways to reference bits in a bitset
   bool b;
   bitset<5>::element_type e;

   b = b1.test ( 2 );
   if ( b )
      cout << "The bit at position 2 of bitset b1"
           << "has a value of 1." << endl;
   else
      cout << "The bit at position 2 of bitset b1"
           << "has a value of 0." << endl;
   b1[2] = 1;
   cout << "Bitset b1 modified by b1[2] = 1 is: ( "<< b1 << " )"
        << endl;

   e = b1.test ( 2 );
   if ( e )
      cout << "The bit at position 2 of bitset b1"
           << "has a value of 1." << endl;
   else
      cout << "The bit at position 2 of bitset b1"
           << "has a value of 0." << endl;
}
Original bitset b1(6) is: ( 010 )
The bit at position 2 of bitset b1has a value of 0.
Bitset b1 modified by b1[2] = 1 is: ( 110 )
The bit at position 2 of bitset b1has a value of 1.

flip

反轉 bitset 中的所有位元值,或在指定位置反轉單一位元。

bitset<N>& flip();
bitset<N>& flip(size_t pos);

參數

pos
要反轉其值之位元的位置。

傳回值

已為其叫用成員函式的複 bitset 本。

備註

如果指定為參數的位置大於 N 位反轉的大小 bitset<N> ,則第二個成員函式會擲回 out_of_range 例外狀況。

範例

// 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 ( 3 );

   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";
   }
}
The collection of bits in the original bitset is: ( 00110 )
After flipping all the bits, the bitset becomes: ( 11001 )
After flipping the fourth bit, the bitset becomes: ( 10001 )

00001  The bit flipped is in position 0.
00011  The bit flipped is in position 1.
00111  The bit flipped is in position 2.
01111  The bit flipped is in position 3.
11111  The bit flipped is in position 4.

hash

template <class T> struct hash;
template <size_t N> struct hash<bitset<N>>;

none

測試在 bitset 物件中是否沒有已設為 1 的位元。

bool none() const;

傳回值

true 如果 中 bitset 沒有任何位設定為 1,則為 , false 如果至少有一個位設定為 1,則為 。

範例

// bitset_none.cpp
// compile with: /EHsc
#include <bitset>
#include <iostream>

int main( )
{
   using namespace std;

   bitset<5> b1 ( 6 );
   bool b, rb;

   cout << "Original bitset b1(6) is: ( " << b1 << " )"
        << endl;

   b = b1.none ( );

   if ( b )
      cout << "None of the bits in bitset b1 are set to 1."
           << endl;
   else
      cout << "At least one of the bits in bitset b1 is set to 1."
           << endl;

   bitset<5> rb1;
   rb1 = b1.reset ( );
   rb = rb1.none ( );
   if ( rb )
      cout << "None of the bits in bitset b1 are set to 1."
           << endl;
   else
      cout << "At least one of the bits in bitset b1 is set to 1."
           << endl;
}
Original bitset b1(6) is: ( 00110 )
At least one of the bits in bitset b1 is set to 1.
None of the bits in bitset b1 are set to 1.

operator!=

測試目標 bitset 是否與指定的 bitset 不相等。

bool operator!=(const bitset<N>& right) const;

參數

right
bitset要與不相等目標位集比較的 。

傳回值

true 如果位集不同,則為 ; false 如果它們相同,則為 。

備註

位集的大小必須相同。

範例

// bitset_op_NE.cpp
// compile with: /EHsc
#include <bitset>
#include <iostream>

int main( )
{
   using namespace std;

   bitset<5> b1 ( 7 );
   bitset<5> b2 ( 7 );
   bitset<5> b3 ( 2 );
   bitset<4> b4 ( 7 );

   if ( b1 != b2 )
      cout << "Bitset b1 is different from bitset b2." << endl;
   else
      cout << "Bitset b1 is the same as bitset b2." << endl;

   if ( b1 != b3 )
      cout << "Bitset b1 is different from bitset b3." << endl;
   else
      cout << "Bitset b1 is the same as bitset b3." << endl;

   // This would cause an error because bitsets must have the
   // same size to be tested
   // if ( b1 != b4 )
   //   cout << "Bitset b1 is different from bitset b4." << endl;
   // else
   //   cout << "Bitset b1 is the same as bitset b4." << endl;
}
Bitset b1 is the same as bitset b2.
Bitset b1 is different from bitset b3.

operator&=

執行位集與位 「and」 ( & ) 運算的位組合。

bitset<N>& operator&=(const bitset<N>& right);

參數

right
bitset要與目標位集結合的 。

傳回值

由位 「and」 ( & ) 作業產生的修改目標位集,指定 bitset 為 參數。

備註

如果每個位為 true,則由 AND 運算子結合的兩個位會傳回 true ;否則,其組合會傳 false 回 。

這兩個位集的大小必須相同。

範例

// bitset_op_bitwise.cpp
// compile with: /EHsc
#include <bitset>
#include <iostream>

int main( )
{
   using namespace std;

   bitset<5> b1 ( 7 );
   bitset<5> b2 ( 11 );
   bitset<4> b3 ( 7 );

   cout << "The target bitset b1 is:    ( "<< b1 << " )." << endl;
   cout << "The parameter bitset b2 is: ( "<< b2 << " )." << endl;
   cout << endl;

   b1 &= b2;
   cout << "After bitwise AND combination,\n"
        << "the target bitset b1 becomes:   ( "<< b1 << " )."
        << endl;

   // Note that the parameter-specified bitset is unchanged
   cout << "The parameter bitset b2 remains: ( "<< b2 << " )."
        << endl;

   // The following would cause an error because the bisets
   // must be of the same size to be combined
   // b1 &= b3;
}
The target bitset b1 is:    ( 00111 ).
The parameter bitset b2 is: ( 01011 ).

After bitwise AND combination,
the target bitset b1 becomes:   ( 00011 ).
The parameter bitset b2 remains: ( 01011 ).

operator<<

以位置的指定數目將 bitset 中的位元移位至左邊,並傳回結果至新的 bitset

bitset<N> operator<<(size_t pos) const;

參數

pos
中位 bitset 要移位左邊的位置數目。

傳回值

已修改的 bitset,其中的位元會向左移位必要的位置數值。

備註

成員運算子函式會 bitset(*this) <<= pos 傳回 ,其中 <<= 會將 中的 bitset 位移向左邊指定的位置數目,並將結果傳回至目標 bitset

範例

// bitset_op_LS.cpp
// compile with: /EHsc
#include <bitset>
#include <iostream>

int main( )
{
   using namespace std;

   bitset<5> b1 ( 7 );

   cout << "The bitset b1 is: ( "<< b1 << " )." << endl;

   bitset<5> b2;
   b2 = b1 << 2;

   cout << "After shifting the bits 2 positions to the left,\n"
        << " the bitset b2 is: ( "<< b2 << " )."
        << endl;

   bitset<5> b3 = b2 >> 1;

   cout << "After shifting the bits 1 position to the right,\n"
        << " the bitset b3 is: ( " << b3 << " )."
        << endl;
}

operator<<=

以位置的指定數目將 bitset 中的位元移位至左邊,並傳回結果至目標的 bitset

bitset<N>& operator<<=(size_t pos);

參數

pos
中位 bitset 要移位左邊的位置數目。

傳回值

已修改目標 bitset ,讓位已移轉至左邊所需的位置數目。

備註

如果沒有要移位至該位置的元素,此函式會將位元值清除成 0。

範例

// bitset_op_LSE.cpp
// compile with: /EHsc
#include <bitset>
#include <iostream>

int main( )
{
   using namespace std;
   bitset<5> b1 ( 7 );
   cout << "The target bitset b1 is: ( "<< b1 << " )." << endl;
   b1 <<= 2;
   cout << "After shifting the bits 2 positions to the left,\n"
        << "the target bitset b1 becomes: ( "<< b1 << " )."
        << endl;
}
The target bitset b1 is: ( 00111 ).
After shifting the bits 2 positions to the left,
the target bitset b1 becomes: ( 11100 ).

operator==

測試目標 bitset 是否與指定的 bitset 相等。

bool operator==(const bitset<N>& right) const;

參數

right
bitset要比較目標位集是否相等的 。

傳回值

true 如果位集相同,則為 ; false 如果他們不同的話。

備註

位集的大小必須相同。

範例

// bitset_op_EQ.cpp
// compile with: /EHsc
#include <bitset>
#include <iostream>

int main( )
{
   using namespace std;
   bitset<5> b1 ( 7 );
   bitset<5> b2 ( 7 );
   bitset<5> b3 ( 2 );
   bitset<4> b4 ( 7 );

   if ( b1 == b2 )
      cout << "Bitset b1 is the same as bitset b2." << endl;
   else
      cout << "Bitset b1 is different from bitset b2." << endl;

   if ( b1 == b3 )
      cout << "Bitset b1 is the same as bitset b3." << endl;
   else
      cout << "Bitset b1 is different from bitset b3." << endl;

   // This would cause an error because bitsets must have the
   // same size to be tested
   // if ( b1 == b4 )
   //   cout << "Bitset b1 is the same as bitset b4." << endl;
   // else
   //   cout << "Bitset b1 is different from bitset b4." << endl;
}
Bitset b1 is the same as bitset b2.
Bitset b1 is different from bitset b3.

operator>>

將 中的 bitset 位移向右邊指定的位置數目,並將結果傳回至新的位集。

bitset<N> operator>>(size_t pos) const;

參數

pos
中位 bitset 要移位右邊的位置數目。

傳回值

新的位集,其中位已向右移位相對於目標 bitset 的必要位置數目。

範例

// bitset_op_RS.cpp
// compile with: /EHsc
#include <bitset>
#include <iostream>

int main( )
{
   using namespace std;
   bitset<5> b1 ( 7 );
   cout << "The bitset b1 is: ( "<< b1 << " )." << endl;

   bitset<5> b2;
   b2 = b1 << 2;

   cout << "After shifting the bits 2 positions to the left,\n"
        << "the bitset b2 is: ( "<< b2 << " )."
        << endl;
   bitset<5> b3 = b2 >> 1;

   cout << "After shifting the bits 1 position to the right,\n"
        << "the bitset b3 is: ( " << b3 << " )."
        << endl;
}
The bitset b1 is: ( 00111 ).
After shifting the bits 2 positions to the left,
the bitset b2 is: ( 11100 ).
After shifting the bits 1 position to the right,
the bitset b3 is: ( 01110 ).

operator>>=

以位置的指定數目將 bitset 中的位元移位至右邊,並傳回結果至目標的 bitset

bitset<N>& operator>>=(size_t pos);

參數

pos
中位 bitset 要移位右邊的位置數目。

傳回值

已修改目標 bitset ,讓位已向右移位所需的位置數目。

備註

如果沒有要移位至該位置的元素,此函式會將位元值清除成 0。

範例

// bitset_op_RSE.cpp
// compile with: /EHsc
#include <bitset>
#include <iostream>

int main( )
{
   using namespace std;
   bitset<5> b1 ( 28 );
   cout << "The target bitset b1 is: ( "<< b1 << " )." << endl;

   b1 >>= 2;
   cout << "After shifting the bits 2 positions to the right,\n"
        << "the target bitset b1 becomes: ( "<< b1 << " )."
        << endl;
}
The target bitset b1 is: ( 11100 ).
After shifting the bits 2 positions to the right,
the target bitset b1 becomes: ( 00111 ).

operator[]

如果 bitset 可修改,則傳回位於 bitset 中指定位置的位元參考;否則它會傳回該位置的位元值。

bool operator[](size_t pos) const;
reference operator[](size_t pos);

參數

pos
在 中 bitset 尋找位的位置。

備註

當您在組建中定義為 _ITERATOR_DEBUG_LEVEL 1 或 2 時,如果您嘗試存取 超出 界限的專案 bitset ,則可執行檔中會發生執行階段錯誤。 如需詳細資訊,請參閱 Checked Iterators

範例

// bitset_op_REF.cpp
// compile with: /EHsc
#include <bitset>
#include <iostream>

int main( )
{
   using namespace std;
   bool b;
   bitset<5> b1 ( 6 );
   cout << "The initialized bitset<5> b1( 2 ) is: ( "<< b1 << " )."
        << endl;

   int i;
   for ( i = 0 ; i <= 4 ; i++ )
   {
      b = b1[ i ];
      cout << "  The bit in position "
           << i << " is " << b << ".\n";
   }
}

operator^=

執行位集與位 「xor」 ( ^ ) 運算的位組合。

bitset<N>& operator^=(const bitset<N>& right);

參數

right
bitset要與目標位集結合的 。

傳回值

由位 「xor」 ( ^ ) 運算產生的修改目標位集,指定 bitset 為 參數。

備註

如果位至少有一個,但不是兩個位,則由位 「xor」 運算子 ^true 結合的兩個位 true 會傳回 ;否則,其組合會傳 false 回 。

位集的大小必須相同。

範例

// bitset_op_bitwiseOR.cpp
// compile with: /EHsc
#include <bitset>
#include <iostream>

int main( )
{
   using namespace std;
   bitset<5> b1 ( 7 );
   bitset<5> b2 ( 11 );
   bitset<4> b3 ( 7 );

   cout << "The target bitset b1 is:    ( "<< b1 << " )." << endl;
   cout << "The parameter bitset b2 is: ( "<< b2 << " )." << endl;
   cout << endl;

   b1 ^= b2;
   cout << "After bitwise exclusive OR combination,\n"
        << "the target bitset b1 becomes:   ( "<< b1 << " )."
        << endl;

   // Note that the parameter-specified bitset in unchanged
   cout << "The parameter bitset b2 remains: ( "<< b2 << " )."
        << endl;

   // The following would cause an error because the bitsets
   // must be of the same size to be combined
   // b1 |= b3;
}
The target bitset b1 is:    ( 00111 ).
The parameter bitset b2 is: ( 01011 ).

After bitwise exclusive OR combination,
the target bitset b1 becomes:   ( 01100 ).
The parameter bitset b2 remains: ( 01011 ).

operator|=

使用位 「or」 ( | ) 運算結合兩個位集。

bitset<N>& operator|=(const bitset<N>& right);

參數

right
bitset要以位方式與目標 bitset 結合的 。

傳回值

由位 「or」 ( | ) 作業產生的修改目標位集,其 bitset 指定為 參數。

備註

如果至少有一個位為 true ,則由內含 OR 運算子結合的兩個位會傳回 true ;如果兩個位都是 false ,則其組合會傳 false 回 。

位集的大小必須相同。

範例

// bitset_op_BIO.cpp
// compile with: /EHsc
#include <bitset>
#include <iostream>

int main( )
{
   using namespace std;

   bitset<5> b1 ( 7 );
   bitset<5> b2 ( 11 );
   bitset<4> b3 ( 7 );

   cout << "The target bitset b1 is:    ( "<< b1 << " )." << endl;
   cout << "The parameter bitset b2 is: ( "<< b2 << " )." << endl;
   cout << endl;

   b1 |= b2;
   cout << "After bitwise inclusive OR combination,\n"
        << "the target bitset b1 becomes:   ( "<< b1 << " )."
        << endl;

   // Note that the parameter-specified bitset in unchanged
   cout << "The parameter bitset b2 remains: ( "<< b2 << " )."
        << endl;

   // The following would cause an error because the bisets
   // must be of the same size to be combined
   // b1 |= b3;
}
The target bitset b1 is:    ( 00111 ).
The parameter bitset b2 is: ( 01011 ).

After bitwise inclusive OR combination,
the target bitset b1 becomes:   ( 01111 ).
The parameter bitset b2 remains: ( 01011 ).

operator~

反轉目標 bitset 中的所有位元,並傳回結果。

bitset<N> operator~() const;

傳回值

bitset,其所有位都會反轉為目標 bitset

範例

// bitset_op_invert.cpp
// compile with: /EHsc
#include <iostream>
#include <string>
#include <bitset>

int main( )
{
   using namespace std;

   bitset<5> b1 ( 7 );
   bitset<5> b2;
   b2 = ~b1;

   cout << "Bitset b1 is: ( "<< b1 << " )." << endl;
   cout << "Bitset b2 = ~b1 is: ( "<< b2 << " )." << endl;

   // These bits could also be flipped using the flip member function
   bitset<5> b3;
   b3 = b1.flip( );
   cout << "Bitset b3 = b1.flip( ) is: ( "<< b2 << " )." << endl;
}
Bitset b1 is: ( 00111 ).
Bitset b2 = ~b1 is: ( 11000 ).
Bitset b3 = b1.flip( ) is: ( 11000 ).

reference

Proxy 類別,提供參考給包含於 bitset 中的位元,而 bitset 是用來存取及管理做為類別 bitsetoperator[] 協助程式類別的個別位元。

class reference {
   friend class bitset<N>;
public:
   reference& operator=(bool val);
   reference& operator=(const reference& bitref);
   bool operator~() const;
   operator bool() const;
   reference& flip();
};

參數

val
要指派給 中 bitset 位之 型 bool 別之 物件的值。

bitref
位於 中位置 ibitsetx 之位之表單 x [ i ] 的參考。

傳回值

類別參考之第一、第二和第五個成員函式之 引數位置所指定之 位 bitset 的參考,以及 truefalse ,以反映 類別參考之第三個和第四個成員函式中 bitset 修改位的值。

備註

reference類別只作為 的 bitsetoperator[] 協助程式類別存在。 成員類別描述可存取 內 bitset 個別位的物件。 讓我們 b 成為 型 bool 別的物件, x 以及 ybitset<N> 別 的物件,以及 ij 這類物件內的有效位置。 標記法 x [i] 會參考位集 x 中位置 i 的位。 reference 類別的成員函式會依序提供下列作業:

作業 定義
x[i] = b boolb 儲存在 bitset x 中的位位置 i
x[i] = y[j] 將位 y [ ] 的值儲存在 bitset x 中的位位置 ij
b = ~ x[i] 將位 x [ ] 的翻轉值儲存在 中 boolbi
b = x[i] 將位 x [ ] 的值儲存在 中 bbooli
x[i]. flip( ) 將位 [ i ] 的翻轉值儲存在 中的 xx 位置 i

範例

// bitset_reference.cpp
// compile with: /EHsc
#include <bitset>
#include <iostream>

int main( )
{
   using namespace std;

   bitset<5> b1 ( 2 );
   bitset<5> b2 ( 6 );
   cout << "The initialized bitset<5> b1( 2 ) is: ( "<< b1 << " )."
        << endl;
   cout << "The initialized bitset<5> b2( 6 ) is: ( "<< b2 << " )."
        << endl;

   // Example of x [i] = b storing bool b at bit position i
   // in bitset x
   b1[ 0 ] = true;
   cout << "The bitset<5> b1 with the bit at position 0 set to 1"
        << "is: ( "<< b1 << " )" << endl;

   // Example of x [i] = y [j] storing the bool value of the
   // bit at position j in bitset y at bit position i in bitset x
   b2 [4] = b1 [0];      // b1 [0] = true
   cout << "The bitset<5> b2 with the bit at position 4 set to the "
        << "value\nof the bit at position 0 of the bit in "
        << "bitset<5> b1 is: ( "<<  b2  << " )" << endl;

   // Example of b = ~x [i] flipping the value of the bit at
   // position i of bitset x and storing the value in an
   // object b of type bool
   bool b = ~b2 [4];      // b2 [4] = false
   if ( b )
      cout << "The value of the object b = ~b2 [4] "
           << "of type bool is true." << endl;
   else
      cout << "The value of the object b = ~b2 [4] "
           << "of type bool is false." << endl;

   // Example of b = x [i] storing the value of the bit at
   // position i of bitset x in the object b of type bool
   b = b2 [4];
   if ( b )
      cout << "The value of the object b = b2 [4] "
           << "of type bool is true." << endl;
   else
      cout << "The value of the object b = b2 [4] "
           << "of type bool is false." << endl;

   // Example of x [i] . flip ( ) toggling the value of the bit at
   // position i of bitset x
   cout << "Before flipping the value of the bit at position 4 in "
        << "bitset b2,\nit is ( "<<  b2  << " )." << endl;
   b2 [4].flip( );
   cout << "After flipping the value of the bit at position 4 in "
        << "bitset b2,\nit becomes ( "<<  b2  << " )." << endl;
   bool c;
   c = b2 [4].flip( );
   cout << "After a second flip, the value of the position 4 "
        << "bit in b2 is now: " << c << ".";
}
The initialized bitset<5> b1( 2 ) is: ( 00010 ).
The initialized bitset<5> b2( 6 ) is: ( 00110 ).
The bitset<5> b1 with the bit at position 0 set to 1 is: ( 00011 )
The bitset<5> b2 with the bit at position 4 set to the value
of the bit at position 0 of the bit in bitset<5> b1 is: ( 10110 )
The value of the object b = ~b2 [4] of type bool is false.
The value of the object b = b2 [4] of type bool is true.
Before flipping the value of the bit at position 4 in bitset b2,
it is ( 10110 ).
After flipping the value of the bit at position 4 in bitset b2,
it becomes ( 00110 ).
After a second flip, the value of the position 4 bit in b2 is now: 1.

reset

bitset 中的所有位元重設為 0,或將指定位置的位元重設為 0。

bitset<N>& reset();
bitset<N>& reset(size_t pos);

參數

pos
要重設為 0 之 中 bitset 位的位置。

傳回值

叫用成員函式的 bitset 複本。

備註

如果指定的位置大於 的大小,則第二個 out_of_range 成員函式會擲回例外狀況 bitset

範例

// bitset_reset.cpp
// compile with: /EHsc
#include <bitset>
#include <iostream>

int main( )
{
   using namespace std;

   bitset<5> b1 ( 13 );
   cout << "The set of bits in bitset<5> b1(13) is: ( "<< b1 << " )"
        << endl;

   bitset<5> b1r3;
   b1r3 = b1.reset( 2 );
   cout << "The collection of bits obtained from resetting the\n"
        << "third bit of bitset b1 is: ( "<< b1r3 << " )"
        << endl;

   bitset<5> b1r;
   b1r = b1.reset( );
   cout << "The collecion of bits obtained from resetting all\n"
        << "the elements of the bitset b1 is: ( "<< b1r << " )"
        << endl;
}
The set of bits in bitset<5> b1(13) is: ( 01101 )
The collecion of bits obtained from resetting the
third bit of bitset b1 is: ( 01001 )
The collecion of bits obtained from resetting all
the elements of the bitset b1 is: ( 00000 )

set

bitset 中的所有位元設為 1,或將指定位置的位元設為 1。

bitset<N>& set();

bitset<N>& set(
    size_t pos,
    bool val = true);

參數

pos
要設定指派值的 中 bitset 位位置。

val
要指派給位於指定位置之位的值。

傳回值

叫用成員函式的 bitset 複本。

備註

如果指定的位置大於 的大小,則第二個 out_of_range 成員函式會擲回例外狀況 bitset

範例

// bitset_set.cpp
// compile with: /EHsc
#include <bitset>
#include <iostream>

int main( )
{
   using namespace std;

   bitset<5> b1 ( 6 );
   cout << "The set of bits in bitset<5> b1(6) is: ( "<< b1 << " )"
        << endl;

   bitset<5> b1s0;
   b1s0 = b1.set( 0 );
   cout << "The collecion of bits obtained from setting the\n"
        << "zeroth bit of bitset b1 is: ( "<< b1s0 << " )"
        << endl;

   bitset<5> bs1;
   bs1 = b1.set( );
   cout << "The collecion of bits obtained from setting all the\n"
        << "elements of the bitset b1 is: ( "<< bs1 << " )"
        << endl;
}
The set of bits in bitset<5> b1(6) is: ( 00110 )
The collecion of bits obtained from setting the
zeroth bit of bitset b1 is: ( 00111 )
The collecion of bits obtained from setting all the
elements of the bitset b1 is: ( 11111 )

size

傳回 bitset 物件中的位元數。

size_t size() const;

傳回值

中的 bitset<N>N 數。

範例

// bitset_size.cpp
// compile with: /EHsc
#include <bitset>
#include <iostream>

int main()
{
    using namespace std;

    bitset<5> b1(6);
    size_t i;

    cout << "The set of bits in bitset<5> b1( 6 ) is: ( "<< b1 << " )"
         << endl;

    i = b1.size();

    cout << "The number of bits in bitset b1 is: " << i << "."
         << endl;
}
The set of bits in bitset<5> b1( 6 ) is: ( 00110 )
The number of bits in bitset b1 is: 5.

test

測試位於 bitset 中指定位置的位元是否設為 1。

bool test(size_t pos) const;

參數

pos
要測試其值的 中 bitset 位位置。

傳回值

true 如果引數位置所指定的位設定為 1,則為 ;否則為 false

備註

成員函式會擲回 out_of_range

to_string

bitset 物件轉換為字串表示。

template <class charT = char, class traits = char_traits<charT>, class Allocator = allocator<charT> >
   basic_string<charT, traits, Allocator> to_string(charT zero = charT('0'), charT one = charT('1')) const;

傳回值

類別 basic_string 的字串物件,其中 中 bitset 設定的每個位都有對應的字元 1,如果位未設定,則為 0 的字元。

範例

// bitset_to_string.cpp
// compile with: /EHsc
#include <bitset>
#include <iostream>
#include <string>

int main( )
{
   using namespace std;

   bitset<5> b1 ( 7 );

   cout << "The ordered set of bits in the bitset<5> b1( 7 )"
        << "\n  that was generated by the number 7 is: ( "
        << b1 << " )" << endl;

   string s1;
   s1 =  b1.template to_string<char,
   char_traits<char>, allocator<char> >( );
   cout << "The string returned from the bitset b1"
        << "\n  by the member function to_string( ) is: "
        << s1 << "." << endl;
}
The ordered set of bits in the bitset<5> b1( 7 )
  that was generated by the number 7 is: ( 00111 )
The string returned from the bitset b1
  by the member function to_string( ) is: 00111.

to_ullong

unsigned long long 回值,其中包含與 物件內容 bitset 相同的位。

unsigned long long to_ullong() const;

傳回值

傳回位序列中位值的總和,做為 unsigned long long 。 如果用來初始化 bitset ,這個 unsigned long long 值會重新建立相同的設定位。

例外狀況

overflow_error如果位序列中的任何位具有無法表示為 類型的 unsigned long long 值,則會擲回 物件。

備註

傳回位序列中位值的總和,做為 unsigned long long

to_ulong

bitset將 物件轉換成整數,如果用來初始化 bitset ,則會產生包含的位序列。

unsigned long to_ulong( ) const;

傳回值

如果在 初始化 中使用 ,則會產生 中的 bitset 位的 bitset 整數。

備註

套用成員函式會傳回的整數,其序列為 1 和 0 位數,如 中包含的 bitset 位序列所找到。

如果位序列中的任何位具有無法表示為 類型的 unsigned long 值,則成員函式會擲回 overflow_error 物件。

範例

// bitset_to_ulong.cpp
// compile with: /EHsc
#include <bitset>
#include <iostream>

int main( )
{
   using namespace std;

   bitset<5> b1 ( 7 );

   cout << "The ordered set of bits in the bitset<5> b1( 7 )"
        << "\n  that was generated by the number 7 is: ( "
        << b1 << " )" << endl;

   unsigned long int i;
   i = b1.to_ulong( );
   cout << "The integer returned from the bitset b1,"
        << "\n  by the member function to_long( ), that"
        << "\n  generated the bits as a base two number is: "
        << i << "." << endl;
}
The ordered set of bits in the bitset<5> b1( 7 )
  that was generated by the number 7 is: ( 00111 )
The integer returned from the bitset b1,
  by the member function to_long( ), that
  generated the bits as a base two number is: 7.