bitset クラス

固定数のビットで構成されるシーケンスを格納するオブジェクト型を記述します。これらのビットによって、一連の項目または条件のフラグを保持するためのコンパクトな方法が提供されます。 bitset クラスでは、ビットのコレクションを含み、各ビットへの一定時間のアクセスを提供する bitset 型のオブジェクトの操作がサポートされます。

構文

template <size_t N>
class bitset

パラメーター

N
size_t 型の 0 以外の整数で bitset オブジェクト内のビット数を指定します。この値はコンパイル時に認識される必要があります。

解説

よく似ている vector<bool> クラスとは異なり、bitset クラスには反復子がなく、C++ 標準ライブラリのコンテナーでもありません。 また、bitset<N> が宣言されている場合、テンプレート パラメーター N で指定したサイズに従い、コンパイル時のサイズが特定の値に固定されている点でも vector<bool> とは異なっています。

ビットは、その値が 1 の場合は設定され、0 の場合にはリセットされます。 ビットの反転や切り替えとは、その値を 1 から 0 へ、あるいは 0 から 1 へ変更することです。 bitset 内の N ビットには、0 から N - 1 の整数値でインデックスが付けられます。0 は最初のビット位置、N - 1 は最後のビット位置のインデックスを表します。

メンバー

コンストラクター

名前 説明
bitset クラス bitset<N> のオブジェクトを構築し、ビットを 0 か、指定した値、または文字列の文字から取得した値に初期化します。

Typedefs

名前 説明
element_type データ型 bool の同意語で、bitset 内の要素のビットを参照するために使用できる型。

関数

名前 説明
all この bitset のビットがすべて true に設定されているかどうかをテストします。
any このメンバー関数は、シーケンス内のいずれかのビットが 1 に設定されているかどうかをテストします。
count このメンバー関数は、ビット シーケンスで設定されているビットの数を返します。
flip bitset 内のすべてのビットの値を反転させるか、指定した位置の単一のビットを反転させます。
none bitset オブジェクト内のどのビットも 1 に設定されていないかどうかをテストします。
reset bitset 内のすべてのビットを 0 にリセットするか、指定した位置の 1 つのビットを 0 にリセットします。
set bitset 内のすべてのビットを 1 に設定するか、指定した位置の 1 つのビットを 1 に設定します。
size bitset オブジェクト内のビット数を返します。
test bitset 内の指定した位置のビットが 1 に設定されているかどうかをテストします。
to_string bitset オブジェクトを文字列形式に変換します。
to_ullong unsigned long long として bitset のビット値の合計を返します。
to_ulong bitset オブジェクトを unsigned long に変換します。これにより、bitset の初期化に使用する場合に含まれるビットのシーケンスが生成されます。

クラス

名前 説明
reference bitset クラスの operator[] 用ヘルパー クラスとして、個々のビットへのアクセスと操作に使用される bitset に含まれるビットへの参照を提供するプロキシ クラス。

演算子

名前 説明
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

このビットセット内のすべてのビットをテストして、それらがすべて true に設定されているかどうかを判別します。

bool all() const;

戻り値

このセット内のすべてのビットが true の場合、true を返します。 1 つ以上のビットが false の場合、false を返します。

any

シーケンス内のいずれかのビットが 1 に設定されているかどうかをテストします。

bool any() const;

戻り値

この bitset 内のいずれかのビットが 1 に設定されている場合は true。すべてのビットが 0 の場合は false

// 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> のオブジェクトを構築し、ビットを 0 か、指定した値、または文字列の文字から取得した値に初期化します。

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
符号なし整数。この基数 2 の表現を使用して、構築対象の bitset 内のビットが初期化されます。

str
bitset のビット値の初期化に使用する 0 と 1 の文字列。

pos
文字列内の文字の位置。左から右に数え、ゼロから始まります。bitset 内の最初のビットの初期化に使用されます。

count
bitset 内のビットの初期値を提供するために使用する文字列内の文字数。

Zero
0 を表すために使用される文字。 既定値は '0' です。

One
1 を表すために使用される文字。 既定値は '1' です。

解説

1) クラス bitset<N> のオブジェクトを構築し、すべての N ビットを既定値の 0 に初期化します。

2-3) クラス bitset<N> のオブジェクトを構築し、val パラメーターからビットを初期化します。

4) クラス bitset<N> のオブジェクトを構築し、0 と 1 の文字列で与えられた文字からビットを初期化します。 文字列の文字に 0 か 1 以外の文字がある場合、このコンストラクターはクラス invalid argument のオブジェクトをスローします。 指定位置 (pos) が文字列の長さを超えている場合、このコンストラクターはクラス out_of_range のオブジェクトをスローします。 このコンストラクターは、位置 pos + j で文字列の文字が 1 のビットのみを bitset 内の位置 j に配置します。 既定では、pos は 0 です。

5) 初期化するビット数を 4) 指定する別のパラメーターが含まれていますが、 count同様です。 任意のパラメーターが 2 つあります。_Zero_One です。それぞれ、str のどの文字を 0 ビットまたは 1 ビットとして解釈する必要があるかを示します。

6) はクラス bitset<N> のオブジェクトを構築し、0 と 1 の C スタイル文字列で与えられた文字に対応する値に N ビットを初期化します。 文字列を文字列型に型変換せずにコンストラクターを呼び出します。例: 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 のコピー。

解説

2 番目のメンバー関数は、パラメーターとして指定された位置が、ビットが反転された bitset<N> のサイズ 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>>;

なし

bitset オブジェクト内のどのビットも 1 に設定されていないかどうかをテストします。

bool none() const;

戻り値

bitset に 1 に設定されているビットがない場合は true、少なくとも 1 つのビットが 1 に設定されている場合は false

// 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!=

指定したビットセットとターゲット ビットセットが等しくないことをテストします。

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

パラメーター

right
ターゲット ビットセットと比較し、等しくないかどうかを判定する bitset

戻り値

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

戻り値

パラメーターとして指定された bitset とビット単位で "and" (&) 演算された結果の変更後ターゲット ビットセット。

解説

AND 演算子で結合された 2 つのビットは、各ビットが true の場合に true を返します。それ以外の場合、この結合は false を返します。

2 つのビットセットは同じサイズである必要があります。

// 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(*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==

指定したビットセットとターゲット ビットセットが等しいことをテストします。

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

パラメーター

right
ターゲット ビットセットと比較し、等しいかどうかを判定する bitset

戻り値

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 の境界の外にある要素にアクセスしようとすると、実行可能ファイルでランタイム エラーが発生します。 詳細については、「チェックを行う反復子」を参照してください。

// 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

戻り値

パラメーターとして指定された bitset とビット単位で "xor" (^) 演算された結果の変更後ターゲット ビットセット。

解説

ビットの少なくとも 1 つが両方ではない場合、ビットごとの "xor" 演算子 (^) によって結合された 2 つのビットがtruetrueされます。それ以外の場合は、それらの組み合わせが返されます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" (|) 演算を使用して 2 つのビットセットを結合します。

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

パラメーター

right
ビット単位でターゲット bitset と結合する bitset

戻り値

パラメーターとして指定された bitset とビット単位で "or" (|) 演算された結果の変更後ターゲット ビットセット。

解説

両立的 OR 演算子で結合された 2 つのビットは、少なくとも 1 つのビットが true の場合に 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<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

bitset クラスの operator[] 用ヘルパー クラスとして、個々のビットへのアクセスと操作に使用される bitset に含まれるビットへの参照を提供するプロキシ クラス。

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
bitsetx 内の位置 i のビットに対する x [ i ] 形式の参照。

戻り値

クラス参照の第 1、第 2、第 5 メンバー関数の引数位置により指定される bitset 内のビットの参照と、true または false。これは、クラス参照の第 3 および第 4 メンバー関数の、bitset 内の変更後ビットの値を反映します。

解説

クラス reference は、bitsetoperator[] のヘルパー クラスとしてのみ存在します。 このメンバー クラスは、bitset 内の個別ビットにアクセスできるオブジェクトを表します。 bbool 型のオブジェクト、xybitset<N> 型のオブジェクト、ij をそのようなオブジェクト内で有効な位置とします。 この表記は x [i] 、ビットセット内の位置 i にあるビットを参照します x。 クラス reference のメンバー関数は、次の演算を順に与えます。

操作 定義
x[i] = b ビットセットx のビット位置 iboolb を保存します。
x[i] = y[j] ビットセット内のビット位置にビット y[ j] の値を格納しますxi
b = ~ x[i] boolb にビットx [i] の反転値を保存します。
b = x[i] boolb にビット x [i] の値を保存します。
x[i]. flip( ) ビット [ i] の反転値をビットx位置ix格納します。

// 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 にリセットするか、指定した位置の 1 つのビットを 0 にリセットします。

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

パラメーター

pos
0 にリセットする、bitset 内のビットの位置。

戻り値

メンバー関数が呼び出された bitset のコピー。

解説

2 番目のメンバー関数は、指定された位置が bitset のサイズより大きい場合、out_of_range 例外をスローします。

// 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 つのビットを 1 に設定します。

bitset<N>& set();

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

パラメーター

pos
bitset 内で、値を割り当てるように設定するビットの位置。

val
指定した位置のビットに割り当てる値。

戻り値

メンバー関数が呼び出された bitset のコピー。

解説

2 番目のメンバー関数は、指定された位置が bitset のサイズより大きい場合、out_of_range 例外をスローします。

// 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 内で、その値をテストするビットの位置。

戻り値

引数位置により指定されたビットが 1 に設定されている場合は true、それ以外の場合は 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

bitset オブジェクトの内容と同じビットのセットを含む unsigned long long 値を返します。

unsigned long long to_ullong() const;

戻り値

ビット シーケンスにあるビット値の合計を unsigned long long として返します。 この unsigned long long 値を使用して bitset を初期化すると、同じ設定ビットが再作成されます。

例外

ビット シーケンス内の overflow_error ビットに、型の値として表現できないビット値がある場合にオブジェクトをスローします unsigned long long

解説

ビット シーケンスにあるビット値の合計を unsigned long long として返します。

to_ulong

bitset オブジェクトを整数に変換します。この整数を bitset の初期化で使用すると、ビット シーケンスが生成されます。

unsigned long to_ulong( ) const;

戻り値

bitset の初期化で使用すると、bitset 内のビットを生成する整数。

解説

メンバー関数を適用すると、整数が返されます。この整数の数字 1 と 0 のシーケンスは、bitset に含まれるビットのシーケンスと同じです。

ビット シーケンス内の overflow_error ビットに型の値として表現できないビット値がある場合、メンバー関数はオブジェクトをスローします unsigned long

// 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.