Condividi tramite


Operatori <bitset>

operator&

Esegue un'operazione AND bit per bit tra due bitset.

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

Parametri

left
Il primo dei due bitset i cui rispettivi elementi devono essere combinati con l'operazione AND bit per bit.

right
La seconda delle due matrici di valori i cui rispettivi elementi devono essere combinati con l'operazione AND bit per bit.

Valore restituito

Bitset i cui elementi sono il risultato dell'esecuzione dell'operazione AND sugli elementi corrispondenti di sinistra e destra.

Esempio

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

Inserisce una rappresentazione di testo della sequenza di bit nel flusso di output.

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

Parametri

right
Oggetto di tipo bitset<N> che deve essere inserito nel flusso di output come stringa.

Valore restituito

Rappresentazione testuale della sequenza di bit in ostr.

Osservazioni:

La funzione modello esegue l'overload operator<<di , consentendo la scrittura di un bitset senza prima convertirlo in una stringa. La funzione di modello consente di gestire in modo efficace quanto segue:

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

Esempio

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

Legge una stringa di caratteri di bit in un bitset.

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

Parametri

i_str
La immessa nel flusso di input da inserire nel bitset.

right
Il bitset che riceve i bit dal flusso di input.

Valore restituito

La funzione modello restituisce la stringa i_str.

Osservazioni:

Overload della funzione operator>> modello da archiviare nel bitset a destra del valore bitset(str), dove str è un oggetto di tipo basic_string< CharType, Traits, allocator< CharType > >& estratto da i_str.

La funzione modello estrae gli elementi da i_str e li inserisce nel bitset fino a quando:

  • Tutti gli elementi di bit sono stati estratti dal flusso di input e memorizzati nel bitset.

  • Il bitset viene riempito con i bit provenienti dal flusso di input.

  • Viene rilevato un elemento input diverso da 0 o 1.

Esempio

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

Esegue un'operazione EXCLUSIVE-OR bit per bit tra due bitset.

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

Parametri

left
Il primo dei due bitset i cui rispettivi elementi devono essere combinati con l'operazione EXCLUSIVE-OR bit per bit.

right
La seconda delle due matrici di valori i cui rispettivi elementi devono essere combinati con l'operazione EXCLUSIVE-OR bit per bit.

Valore restituito

Bitset i cui elementi sono il risultato dell'esecuzione dell'operazione EXCLUSIVE-OR sugli elementi corrispondenti di sinistra e destra.

Esempio

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

Esegue un OR bit per bit tra due bitset oggetti.

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

Parametri

left
Il primo dei due bitset i cui rispettivi elementi devono essere combinati con l'operazione OR bit per bit.

right
La seconda delle due matrici di valori i cui rispettivi elementi devono essere combinati con l'operazione OR bit per bit.

Valore restituito

Bitset i cui elementi sono il risultato dell'esecuzione dell'operazione OR sugli elementi corrispondenti di sinistra e destra.

Esempio

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