Share via


<bitset> 연산자

operator&

두 bitset 간에 비트 AND을 수행합니다.

template <size_t size>
bitset<size>
operator&(
    const bitset<size>& left,
    const bitset<size>& right);

매개 변수

left
각각의 요소를 비트 AND로 결합할 두 bitset 중 첫 번째입니다.

right
각각의 요소를 비트 AND로 결합할 두 valarray 중 두 번째입니다.

Return Value

요소가 왼쪽과 오른쪽의 해당 요소에 대한 작업을 수행한 AND 결과인 비트 세트입니다.

예시

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

using namespace std;

int main()
{
   bitset<4> b1 ( string("0101") );
   bitset<4> b2 ( string("0011") );
   bitset<4> b3 = b1 & b2;
   cout << "bitset 1: " << b1 << endl;
   cout << "bitset 2: " << b2 << endl;
   cout << "bitset 3: " << b3 << endl;
}
bitset 1: 0101
bitset 2: 0011
bitset 3: 0001

operator<<

비트 시퀀스의 텍스트 표현을 출력 스트림에 삽입합니다.

template <class CharType, class Traits, size_t N>
basic_ostream<CharType, Traits>& operator<<(
    basic_ostream<CharType, Traits>& ostr,
    const bitset<N>& right);

매개 변수

right
출력 스트림에 문자열로 삽입할 비트 집합<N> 형식의 개체입니다.

Return Value

에 있는 ostr비트 시퀀스의 텍스트 표현입니다.

설명

템플릿 함수가 오버로드 operator<<되어 먼저 문자열로 변환하지 않고 비트 세트를 쓸 수 있습니다. 템플릿 함수는 다음을 효과적으로 실행합니다.

ostr << right.to_string<CharType, Traits, allocator<CharType>>()

예시

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

int main( )
{
   using namespace std;

   bitset<5> b1 ( 9 );

   // bitset inserted into output stream directly
   cout << "The ordered set of bits in the bitset<5> b1(9)"
        << "\n can be output with the overloaded << as: ( "
        << b1 << " )" << endl;

   // Compare converting bitset to a string before
   // inserting it into the output stream
   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;
}

operator>>

bitset에 대한 비트 문자의 문자열을 읽습니다.

template <class CharType, class Traits, size_t Bits>
basic_istream<CharType, Traits>& operator>> (
    basic_istream<CharType, Traits>& i_str,
    bitset<N>& right);

매개 변수

i_str
bitset에 삽입할 입력 스트림에 입력되는 문자열입니다.

right
입력 스트림에서 비트를 수신하는 bitset입니다.

Return Value

템플릿 함수는 문자열 i_str 반환합니다.

설명

템플릿 함수는 비트 세트 bitset(str) 에 저장하기 위해 오버로드 operator>> 됩니다. 여기서 str 는 i_str 추출된 basic_string< CharType, Traits, allocator< CharType > >& 형식의 개체입니다.

템플릿 함수는 i_str 요소를 추출하고 다음까지 비트 세트에 삽입합니다.

  • 모든 비트 요소가 입력 스트림에서 추출되어 bitset에 저장될 때까지

  • bitset가 입력 스트림의 비트로 채워질 때까지

  • 0 또는 1이 아닌 입력 요소가 발견될 때까지

예시

#include <bitset>
#include <iostream>
#include <string>

using namespace std;
int main()
{
   bitset<5> b1;
   cout << "Enter string of (0 or 1) bits for input into bitset<5>.\n"
        << "Try bit string of length less than or equal to 5,\n"
        << " (for example: 10110): ";
   cin >>  b1;

   cout << "The ordered set of bits entered from the "
        << "keyboard\n has been input into bitset<5> b1 as: ( "
        << b1 << " )" << endl;

   // Truncation due to longer string of bits than length of bitset
   bitset<2> b3;
   cout << "Enter string of bits (0 or 1) for input into bitset<2>.\n"
        << " Try bit string of length greater than 2,\n"
        << " (for example: 1011): ";
   cin >>  b3;

   cout << "The ordered set of bits entered from the "
        << "keyboard\n has been input into bitset<2> b3 as: ( "
        << b3 << " )" << endl;

   // Flushing the input stream
   char buf[100];
   cin.getline(&buf[0], 99);

   // Truncation with non-bit value
   bitset<5> b2;
   cout << "Enter a string for input into  bitset<5>.\n"
        << " that contains a character than is NOT a 0 or a 1,\n "
        << " (for example: 10k01): ";
   cin >>  b2;

   cout << "The string entered from the keyboard\n"
        << " has been input into bitset<5> b2 as: ( "
        << b2 << " )" << endl;
}

operator^

두 bitset 간에 비트 EXCLUSIVE-OR을 수행합니다.

template <size_t size>
bitset<size>
operator^(
    const bitset<size>& left,
    const bitset<size>& right);

매개 변수

left
각각의 요소를 비트 EXCLUSIVE-OR로 결합할 두 bitset 중 첫 번째입니다.

right
각각의 요소를 비트 EXCLUSIVE-OR로 결합할 두 valarray 중 두 번째입니다.

Return Value

요소가 왼쪽과 오른쪽의 해당 요소에 대한 작업을 수행한 EXCLUSIVE-OR 결과인 비트 세트입니다.

예시

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

using namespace std;

int main()
{
   bitset<4> b1 ( string("0101") );
   bitset<4> b2 ( string("0011") );
   bitset<4> b3 = b1 ^ b2;
   cout << "bitset 1: " << b1 << endl;
   cout << "bitset 2: " << b2 << endl;
   cout << "bitset 3: " << b3 << endl;
}
bitset 1: 0101
bitset 2: 0011
bitset 3: 0110

operator|

bitset 개체 간에 비트 OR을 수행합니다.

template <size_t size>
bitset<size>
operator|(
    const bitset<size>& left,
    const bitset<size>& right);

매개 변수

left
각각의 요소를 비트 OR로 결합할 두 bitset 중 첫 번째입니다.

right
각각의 요소를 비트 OR로 결합할 두 valarray 중 두 번째입니다.

Return Value

요소가 왼쪽과 오른쪽의 해당 요소에 대한 작업을 수행한 OR 결과인 비트 세트입니다.

예제

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

using namespace std;

int main()
{
   bitset<4> b1 ( string("0101") );
   bitset<4> b2 ( string("0011") );
   bitset<4> b3 = b1 | b2;
   cout << "bitset 1: " << b1 << endl;
   cout << "bitset 2: " << b2 << endl;
   cout << "bitset 3: " << b3 << endl;
}
bitset 1: 0101
bitset 2: 0011
bitset 3: 0111