Udostępnij przez


<bitset>, operatory

operator &

Wykonuje bitowo AND między dwoma zestawami bitów.

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

Parametry

Lewej
Pierwszy z dwóch zestawów bitów, których odpowiednie elementy mają być łączone z bitowym AND.

Prawo
Druga z dwóch valarrays, których odpowiednie elementy mają być łączone z bitowym AND.

Wartość zwracana

Zestaw bitów, którego elementy są wynikiem wykonania AND operacji na odpowiednich elementach lewej i prawej.

Przykład

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

Wstawia tekstową reprezentację sekwencji bitów do strumienia wyjściowego.

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

Parametry

Prawo
Obiekt typu bitset<N> , który ma zostać wstawiony do strumienia wyjściowego jako ciąg.

Wartość zwracana

Tekstowa reprezentacja sekwencji bitów w pliku ostr.

Uwagi

Funkcja szablonu przeciąża operator<<funkcję , umożliwiając zapisanie zestawu bitów bez uprzedniego przekonwertowania jej na ciąg. Funkcja szablonu skutecznie wykonuje:

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

Przykład

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

Odczytuje ciąg znaków bitowych do zestawu bitów.

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

Parametry

i_str
Ciąg wprowadzony do strumienia wejściowego, który ma zostać wstawiony do zestawu bitów.

Prawo
Zestaw bitów odbierający bity ze strumienia wejściowego.

Wartość zwracana

Funkcja szablonu zwraca i_str ciągu.

Uwagi

Funkcja szablonu przeciąża przechowywanie w zestawie bitów prawej wartości , gdzie str jest obiektem typu basic_string< CharType, Traits, allocator< CharType > >& wyodrębnionym z i_str.bitset(str)operator>>

Funkcja szablonu wyodrębnia elementy z i_str i wstawia je do zestawu bitów do:

  • Wszystkie elementy bitowe zostały wyodrębnione ze strumienia wejściowego i przechowywane w zestawie bitów.

  • Bitset jest wypełniony bitami ze strumienia wejściowego.

  • Napotkano element wejściowy, który nie jest ani 0, ani 1.

Przykład

#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^

Wykonuje bitowo EXCLUSIVE-OR między dwoma zestawami bitów.

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

Parametry

Lewej
Pierwszy z dwóch zestawów bitów, których odpowiednie elementy mają być łączone z bitowym EXCLUSIVE-OR.

Prawo
Druga z dwóch valarrays, których odpowiednie elementy mają być łączone z bitowym EXCLUSIVE-OR.

Wartość zwracana

Zestaw bitów, którego elementy są wynikiem wykonania EXCLUSIVE-OR operacji na odpowiednich elementach lewej i prawej.

Przykład

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

Wykonuje bitowo OR między dwoma bitset obiektami.

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

Parametry

Lewej
Pierwszy z dwóch zestawów bitów, których odpowiednie elementy mają być łączone z bitowym OR.

Prawo
Druga z dwóch valarrays, których odpowiednie elementy mają być łączone z bitowym OR.

Wartość zwracana

Zestaw bitów, którego elementy są wynikiem wykonania OR operacji na odpowiednich elementach lewej i prawej.

Przykład

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