Sdílet prostřednictvím


<bitset> – operátory

operátor &.

Provádí bitové operace AND mezi dvěma bitovými sadami.

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

Parametry

Vlevo
První ze dvou bitových sad, jejichž odpovídající prvky mají být kombinovány s bitovou bitové AND.

Vpravo
Druhá z těchto dvou valarray, jejichž příslušné prvky mají být kombinovány s bitovou bitové AND.

Návratová hodnota

Bitset, jehož prvky jsou výsledkem provádění AND operace s odpovídajícími prvky zleva a doprava.

Příklad

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

Vloží textovou reprezentaci bitové sekvence do výstupního datového proudu.

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

Parametry

Vpravo
Objekt typu bitset<N> , který se má vložit do výstupního datového proudu jako řetězec.

Návratová hodnota

Textové znázornění bitové sekvence v ostr.

Poznámky

Funkce šablony přetěžuje operator<<, což umožňuje, aby byla bitset zapsána bez prvního převodu na řetězec. Funkce šablony efektivně provede:

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

Příklad

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

Načte řetězec bitových znaků do bitové sady.

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
Řetězec zadaný do vstupního datového proudu, který se má vložit do bitsetu.

Vpravo
Bitová sada, která přijímá bity ze vstupního datového proudu.

Návratová hodnota

Funkce šablony vrátí řetězec i_str.

Poznámky

Funkce šablony přetíží operator>> uložení v bitové sadě vpravo hodnotu bitset(str), kde str je objekt typu basic_string< CharType, Traits, allocator< CharType > >& extrahován z i_str.

Funkce šablony extrahuje prvky z i_str a vloží je do bitsetu, dokud:

  • Všechny bitové prvky byly extrahovány ze vstupního datového proudu a uloženy v bitové sadě.

  • Bitset je naplněn bity ze vstupního datového proudu.

  • Je zjištěn vstupní prvek, který není 0 ani 1.

Příklad

#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;
}

operátor^

Provádí bitové operace EXCLUSIVE-OR mezi dvěma bitovými sadami.

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

Parametry

Vlevo
První ze dvou bitových sad, jejichž odpovídající prvky mají být kombinovány s bitovou bitové EXCLUSIVE-OR.

Vpravo
Druhá z těchto dvou valarray, jejichž příslušné prvky mají být kombinovány s bitovou bitové EXCLUSIVE-OR.

Návratová hodnota

Bitset, jehož prvky jsou výsledkem provádění EXCLUSIVE-OR operace s odpovídajícími prvky zleva a doprava.

Příklad

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

Provede bitové or mezi dvěma bitset objekty.

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

Parametry

Vlevo
První ze dvou bitových sad, jejichž odpovídající prvky mají být kombinovány s bitovou bitové OR.

Vpravo
Druhá z těchto dvou valarray, jejichž příslušné prvky mají být kombinovány s bitovou bitové OR.

Návratová hodnota

Bitset, jehož prvky jsou výsledkem provádění OR operace s odpovídajícími prvky zleva a doprava.

Příklad

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