bitset 클래스

일련의 항목 또는 조건에 대한 플래그를 유지하는 간단한 방법을 제공하는 고정 비트 수로 구성된 시퀀스를 저장하는 개체의 유형에 대해 설명합니다. 클래스는 bitset 비트 컬렉션을 포함하고 각 비트에 대한 상수 시간 액세스를 제공하는 비트 집합 형식의 개체에 대한 작업을 지원합니다.

구문

template <size_t N>
class bitset

매개 변수

N
컴파일 시간에 알려야 하는 형식 size_t 의 0이 아닌 정수를 사용하여 개체의 비트 bitset 수를 지정합니다.

설명

비슷한 vector<bool> 클래스와 달리 클래스에는 bitset 반복기가 없으며 C++ 표준 라이브러리 컨테이너가 아닙니다. 또한 템플릿 매개 변수 N 가 선언될 때 bitset<N> 지정된 크기에 따라 컴파일 시간에 고정되는 특정 크기와 다릅니다vector<bool>.

비트는 값이 1이면 설정되고, 값이 0이면 재설정 합니다. 비트를 대칭 이동하거나 반전한다는 것은 값을 1에서 0 또는 0에서 1로 변경하는 것입니다. a bitset 의 비트는 N 0에서 1까지의 정수 값으로 N 인덱싱됩니다. 여기서 0은 첫 번째 비트 위치를 인덱싱하고 N 마지막 비트 위치는 1입니다.

멤버

생성자

이름 설명
bitset 클래스 bitset<N>의 개체를 생성하고 비트를 0, 지정된 일부 값 또는 문자열의 문자에서 얻은 값으로 초기화합니다.

Typedef

이름 설명
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 bitset에 있는 비트 값의 합을 unsigned long long로 반환합니다.
to_ulong bitset 개체를 bitset을 초기화하는 데 사용되는 경우 포함된 비트 시퀀스를 생성할 unsigned long로 변환합니다.

클래스

이름 설명
reference bitsetoperator[]에 대한 도우미 클래스로서 개별 비트에 액세스하고 조작하는 데 사용되는 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;

Return Value

이 집합의 모든 비트가 true이면 반환 true 합니다. 하나 이상의 비트가 false이면 반환 false 합니다.

any

시퀀스의 모든 비트가 1로 설정되어 있는지를 테스트합니다.

bool any() const;

Return Value

true 이 안에 있는 비트가 bitset 1로 설정되어 있으면 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>의 개체를 생성하고 비트를 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
생성 중인 비트를 bitset 초기화하는 데 기본 두 표현을 사용하는 부호 없는 정수입니다.

str
비트 값을 초기화하는 데 사용되는 0과 0의 bitset 문자열입니다.

pos
문자열의 문자 위치로, 왼쪽에서 오른쪽으로 계산하고 0으로 시작하여 첫 번째 비트를 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의 개체를 throw합니다. 지정된 위치(pos)가 문자열 길이를 초과하면 생성자는 클래스 out_of_range의 개체를 throw합니다. 생성자는 위치에 있는 문자열의 문자가 1인 위치 jbitset 의 비트 pos + j 만 설정합니다. 기본적으로 pos는 0입니다.

5) 초기화할 4) 비트 수를 지정하는 다른 매개 변수 count와 비슷하지만 포함됩니다. 두 개의 선택적 매개 변수 _Zero 가 있으며 _One, 각각 0비트와 1비트를 의미하도록 해석해야 하는 문자를 str 나타냅니다.

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;

Return Value

비트 시퀀스에 설정된 비트 수입니다.

예시

// 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
값이 반전될 비트의 위치입니다.

Return Value

멤버 함수가 호출된 수정 bitset 된 복사본입니다.

설명

매개 변수로 지정된 위치가 비트가 반전된 크기 Nbitset<N> 보다 큰 경우 두 번째 멤버 함수는 예외를 throw 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;

Return Value

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 않음을 위해 대상 비트 세트와 비교할 것입니다.

Return Value

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 대상 비트 집합과 비트로 결합되는 값입니다.

Return Value

매개 변수로 지정된 비트 "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 가 이동될 왼쪽 위치의 수입니다.

Return Value

필요한 위치 수만큼 왼쪽으로 이동한 비트가 있는 수정된 bitset입니다.

설명

멤버 연산자 함수는 지정된 수의 위치를 왼쪽으로 이동하는 bitset 위치를 <<= 반환 bitset(*this) <<= pos 하고 결과를 대상 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 가 이동할 위치 수입니다.

Return Value

비트가 필요한 위치 수를 왼쪽으로 이동하도록 수정된 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 같음을 위해 대상 비트 세트와 비교할 것입니다.

Return Value

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 가 이동할 위치의 수입니다.

Return Value

비트가 대상을 기준으로 필요한 위치 수를 오른쪽으로 이동한 새 비트 세트입니다 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 가 이동할 위치의 수입니다.

Return Value

비트가 필요한 위치 수만큼 오른쪽으로 이동되도록 대상 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.

설명

빌드에서 1 또는 2로 정의 _ITERATOR_DEBUG_LEVEL 하면 실행 파일에서 범위 외부의 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 대상 비트 집합과 비트로 결합되는 값입니다.

Return Value

매개 변수로 지정된 비트 "xor"(^) 작업 bitset 에서 발생하는 수정된 대상 비트 집합입니다.

설명

비트 "xor" 연산자()가 결합한 두 비트는^ 비트 중 하나 이상(둘 다 아님)이면 반환 true 되고, 그렇지 않으면 조합이 반환됩니다truefalse.

비트 세트의 크기는 같아야 합니다.

예시

// 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과 비트 결합할 값입니다.

Return Value

매개 변수로 지정된 비트 "or" (|) 작업 bitset 에서 발생하는 수정된 대상 비트 집합입니다.

설명

비트 중 하나 이상이면 포괄 OR 연산자가 결합한 두 비트가 반환 true 되고true, 두 비트가 모두 있으면 조합이 반환됩니다falsefalse.

비트 세트의 크기는 같아야 합니다.

예시

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

Return Value

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

bitsetoperator[]에 대한 도우미 클래스로서 개별 비트에 액세스하고 조작하는 데 사용되는 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
비트에 할당할 형식 bool 의 개체 값입니다 bitset.

bitref
위치의 비트에 대한 폼 x [ i ] 의 참조입니다bitsetx.i

Return Value

클래스 참조의 첫 번째, 두 번째 및 다섯 번째 멤버 함수에 대한 인수 위치로 지정된 비트 bitset 에 대한 참조이거나 truefalse, 클래스 참조의 세 번째 및 네 번째 멤버 함수에 bitset 대해 수정된 비트의 값을 반영합니다.

설명

클래스 reference 는 .에 대한 도우미 클래스로만 존재합니다 bitsetoperator[]. 멤버 클래스는 내의 개별 비트에 액세스할 수 있는 개체를 설명합니다 bitset. 형식의 개체와 y 형식 boolxbitset<N>의 개체 및 ji 이러한 개체 내의 유효한 위치가 되도록 b 합니다. 표기법 x [i] 은 비트 집합의 위치에 i 있는 비트를 참조합니다 x. reference 클래스의 멤버 함수는 다음 연산을 차례로 제공합니다.

연산 정의
x[i] = b 비트 집합의 비트 위치에 i 값을 b 저장 bool 합니다x.
x[i] = y[j] 비트 [] 값을 비트 yj집합의 비트 위치에 i 저장합니다x.
b = ~ x[i] 비트 x[]의 대칭 이동 값을 저장합니다boolb. i
b = x[i] 비트 x[] i의 값을 저장합니다 boolb.
x[i]. flip( ) 비트 x[] i의 대칭 이동 값을 비트 위치에 i 다시 저장합니다 x.

예시

// 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 의 위치입니다.

Return Value

멤버 함수가 bitset 호출된 복사본입니다.

설명

지정된 위치가 out_of_range .의 크기보다 크면 두 번째 멤버 함수가 예외를 bitsetthrow합니다.

예시

// 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
지정된 위치에 있는 비트에 할당할 값입니다.

Return Value

멤버 함수가 bitset 호출된 복사본입니다.

설명

지정된 위치가 out_of_range .의 크기보다 크면 두 번째 멤버 함수가 예외를 bitsetthrow합니다.

예시

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

Return Value

의 비트 N수입니다 bitset<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 대해 테스트할 비트의 위치입니다.

Return Value

true인수 위치에 지정된 비트가 1로 설정된 경우 그렇지 않으면 . false

설명

멤버 함수가 을 throw합니다. 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로 반환합니다. 이 unsigned long long 값은 초기화하는 데 사용되는 경우 동일한 집합 비트를 다시 만듭니다 bitset.

예외

overflow_error 비트 시퀀스의 비트에 형식unsigned long long의 값으로 나타낼 수 없는 비트 값이 있는 경우 개체를 throw합니다.

설명

비트 시퀀스에 있는 비트 값의 합계를 unsigned long long로 반환합니다.

to_ulong

개체를 bitset 초기화하는 bitset데 사용되는 경우 포함된 비트 시퀀스를 생성하는 정수로 변환합니다.

unsigned long to_ulong( ) const;

반환 값

초기화에 사용되는 경우 비트 bitset 가 생성되는 정수 bitset입니다.

설명

멤버 함수를 적용하면 1과 0 숫자의 시퀀스가 같은 정수가 반환됩니다 bitset.

비트 시퀀스의 overflow_error 비트에 형식 unsigned long의 값으로 나타낼 수 없는 비트 값이 있는 경우 멤버 함수는 개체를 throw합니다.

예제

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