Share via


Classe bitset

Descrive un tipo di oggetto che archivia una sequenza formata da un numero fisso di bit che forniscono un modo compatto di mantenere i flag per un set di elementi o condizioni. La bitset classe supporta operazioni su oggetti di tipo bitset che contengono una raccolta di bit e forniscono l'accesso in tempo costante a ogni bit.

Sintassi

template <size_t N>
class bitset

Parametri

N
Specifica il numero di bit nell'oggetto bitset con un numero intero diverso da zero di tipo size_t che deve essere noto in fase di compilazione.

Osservazioni:

A differenza della classe similevector<bool>, la bitset classe non ha iteratori e non è un contenitore della libreria standard C++. Differisce anche dal vector<bool> fatto di essere di alcune dimensioni specifiche fisse in fase di compilazione in base alle dimensioni specificate dal parametro N di modello quando bitset<N> viene dichiarato .

Un bit viene impostato se il valore è 1 e reimpostato se il valore è 0. Capovolgere o invertire un bit significa modificarne il valore da 1 a 0 o da 0 a 1. I N bit in un bitset vengono indicizzati per valori interi da 0 a N - 1, dove 0 indicizza la prima posizione del bit e N - 1 la posizione finale del bit.

Membri

Costruttori

Nome Descrizione
bitset Costruisce un oggetto di classe bitset<N> e inizializza i bit a zero, a un valore specificato o a valori ottenuti dai caratteri in una stringa.

Typedef

Nome Descrizione
element_type Tipo sinonimo del tipo di dati bool che può essere usato per fare riferimento ai bit dell'elemento in bitset.

Funzioni

Nome Descrizione
all Verifica tutti i bit in questo bitset modo per determinare se sono tutti impostati su true.
any La funzione membro verifica se nella sequenza sono inclusi bit impostati su 1.
count La funzione membro restituisce il numero di bit impostati nella sequenza di bit.
flip Inverte il valore di tutti i bit in bitset o un singolo bit in una posizione specificata.
none Verifica che nessun bit sia stato impostato su 1 in un oggetto bitset.
reset Reimposta tutti i bit in bitset su 0 o un bit in una posizione specificata su 0.
set Imposta tutti i bit in bitset su 1 o un bit in una posizione specificata su 1.
size Restituisce il numero di bit in un oggetto bitset.
test Verifica se il bit in una posizione specificata in bitset è impostato su 1.
to_string Converte un oggetto bitset in una rappresentazione di stringa.
to_ullong Restituisce la somma dei valori di bit in bitset come unsigned long long.
to_ulong Converte un oggetto bitset in unsigned long che genera la sequenza dei bit contenuti se usati per inizializzare bitset.

Classi

Nome Descrizione
reference Classe proxy che fornisce i riferimenti ai bit contenuti in un bitset usata per accedere e modificare i singoli bit come classe helper per operator[] della classe bitset.

Operatori

Nome Descrizione
operator!= Verifica un bitset di destinazione per la disuguaglianza con un bitset specificato.
operator&= Esegue una combinazione bit per bit di bitset con l'operazione bit per bit "and" (&).
operator<< Sposta i bit in un bitset a sinistra di un numero di posizioni specificato e restituisce il risultato in un nuovo bitset.
operator<<= Sposta i bit in un bitset a sinistra di un numero di posizioni specificato e restituisce il risultato nel bitset di destinazione.
operator== Verifica un bitset di destinazione per l'uguaglianza con un bitset specificato.
operator>> Sposta i bit in un bitset a destra di un numero di posizioni specificato e restituisce il risultato in un nuovo bitset.
operator>>= Sposta i bit in un bitset a destra di un numero di posizioni specificato e restituisce il risultato nel bitset di destinazione.
operator[] Restituisce un riferimento a un bit in una posizione specificata in un bitset se il bitset è modificabile. In caso contrario, restituisce il valore del bit in tale posizione.
operator^= Esegue una combinazione bit per bit di bitset con l'operazione "xor" bit per bit (^).
operator|= Esegue una combinazione bit per bit di bitset con l'operazione bit per bit "o" (|).
operator~ Inverte tutti i bit in un bitset di destinazione e restituisce il risultato.

Strutture

Nome Descrizione
hash

all

Verifica tutti i bit in questo bitset per determinare se sono tutti impostati su true.

bool all() const;

Valore restituito

Restituisce true se tutti i bit in questo set sono true. Restituisce false se uno o più bit sono false.

any

Verifica se nella sequenza sono inclusi bit impostati su 1.

bool any() const;

Valore restituito

true se qualsiasi bit in questo bitset oggetto è impostato su 1; false se tutti i bit sono 0.

Esempio

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

Costruisce un oggetto di classe bitset<N> e inizializza i bit su zero, su un valore specificato o su valori ottenuti dai caratteri in una stringa.

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'));

Parametri

val
Intero senza segno la cui rappresentazione di base due viene utilizzata per inizializzare i bit nell'oggetto bitset costruito.

str
Stringa di zeri e valori usati per inizializzare i valori di bitset bit.

pos
Posizione del carattere nella stringa, contando da sinistra a destra e a partire da zero, usata per inizializzare il primo bit in bitset.

count
Numero di caratteri nella stringa utilizzata per fornire valori iniziali per i bit nell'oggetto bitset.

Zero
Il carattere usato per rappresentare uno zero. Il valore predefinito è "0".

One
Il carattere usato per rappresentare un uno. Il valore predefinito è "1".

Osservazioni:

1) Costruisce un oggetto della classe bitset<N> e inizializza tutti i bit N in un valore predefinito pari a zero.

2-3) Costruisce un oggetto della classe bitset<N> e inizializza i bit dal val parametro .

4) Costruisce un oggetto della classe bitset<N> e inizializza i bit dai caratteri forniti in una stringa di zeri e uno. Se uno dei caratteri della stringa è diverso da 0 o 1, il costruttore genera un oggetto della classe invalid argument. Se la posizione specificata (pos) supera la lunghezza della stringa, il costruttore genera un oggetto della classe out_of_range. Il costruttore imposta solo i bit in corrispondenza della bitset posizione j in per cui il carattere nella stringa in corrispondenza della posizione pos + j è 1. Il valore predefinito di pos è 0.

5) Analogamente a 4) ma include un altro parametro, count, che specifica il numero di bit da inizializzare. Ha due parametri facoltativi, _Zero e _One, che indicano il carattere in str deve essere interpretato rispettivamente per indicare un bit 0 e un bit 1.

6) Costruisce un oggetto della classe bitset<N>, inizializzando i bit N ai valori che corrispondono ai caratteri forniti in una stringa di caratteri in stile C di zeri e uno. Chiamare il costruttore senza eseguire il cast della stringa in un tipo stringa, ad esempio: bitset<5> b5("01011");

Esempio

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

Restituisce il numero di bit impostati nella sequenza di bit.

size_t count() const;

Valore restituito

Numero di bit impostati nella sequenza di bit.

Esempio

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

Tipo sinonimo del tipo di dati bool che può essere usato per fare riferimento ai bit dell'elemento in bitset.

typedef bool element_type;

Esempio

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

Inverte il valore di tutti i bit in bitset o un singolo bit in una posizione specificata.

bitset<N>& flip();
bitset<N>& flip(size_t pos);

Parametri

pos
La posizione del bit il cui valore viene invertito.

Valore restituito

Copia dell'oggetto modificato bitset per il quale è stata richiamata la funzione membro.

Osservazioni:

La seconda funzione membro genera un'eccezione out_of_range se la posizione specificata come parametro è maggiore della dimensione N del bitset<N> bit invertito.

Esempio

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

Nessuno

Verifica che nessun bit sia stato impostato su 1 in un oggetto bitset.

bool none() const;

Valore restituito

true se nessun bit in bitset è stato impostato su 1; false se almeno un bit è stato impostato su 1.

Esempio

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

Verifica un bitset di destinazione per la disuguaglianza con un bitset specificato.

bool operator!=(const bitset<N>& right) const;

Parametri

right
Oggetto bitset da confrontare con il bitset di destinazione per la disuguaglianza.

Valore restituito

true se i bitset sono diversi; false se sono uguali.

Osservazioni:

I bitset devono avere le stesse dimensioni.

Esempio

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

Esegue una combinazione bit per bit di bitset con l'operazione bit per bit "and" (&).

bitset<N>& operator&=(const bitset<N>& right);

Parametri

right
Oggetto bitset da combinare bit per bit con il bitset di destinazione.

Valore restituito

Bitset di destinazione modificato risultante dall'operazione bit per bit "and" (&) con l'oggetto bitset specificato come parametro.

Osservazioni:

Due bit combinati dall'operatore AND restituiscono true se ogni bit è true; in caso contrario, la combinazione restituisce false.

I due bitset devono avere le stesse dimensioni.

Esempio

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

Sposta i bit in un bitset a sinistra di un numero di posizioni specificato e restituisce il risultato in un nuovo bitset.

bitset<N> operator<<(size_t pos) const;

Parametri

pos
Numero di posizioni a sinistra che devono essere spostati i bit in bitset .

Valore restituito

Il bitset modificato con i bit spostati a sinistra del numero di posizioni richiesto.

Osservazioni:

La funzione dell'operatore membro restituisce bitset(*this) <<= pos dove <<= sposta i bit in un bitset oggetto a sinistra di un numero specificato di posizioni e restituisce il risultato all'oggetto di destinazione bitset.

Esempio

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

Sposta i bit in un bitset a sinistra di un numero di posizioni specificato e restituisce il risultato nel bitset di destinazione.

bitset<N>& operator<<=(size_t pos);

Parametri

pos
Il numero di posizioni a sinistra dei bit in bitset deve essere spostato.

Valore restituito

Modifica di destinazione bitset in modo che i bit siano stati spostati a sinistra del numero di posizioni richiesto.

Osservazioni:

Se non esiste un elemento da spostare nella posizione, la funzione cancella il bit impostandolo su 0.

Esempio

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

Verifica un bitset di destinazione per l'uguaglianza con un bitset specificato.

bool operator==(const bitset<N>& right) const;

Parametri

right
Oggetto bitset da confrontare con il bitset di destinazione per verificarne l'uguaglianza.

Valore restituito

true se i bitset sono uguali; false se sono diversi.

Osservazioni:

I bitset devono avere le stesse dimensioni.

Esempio

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

Sposta i bit in un bitset oggetto a destra di un numero specificato di posizioni e restituisce il risultato a un nuovo bitset.

bitset<N> operator>>(size_t pos) const;

Parametri

pos
Il numero di posizioni a destra dei bit in bitset deve essere spostato.

Valore restituito

Nuovo bitset in cui i bit sono stati spostati a destra il numero di posizioni richiesto rispetto all'oggetto di destinazione bitset.

Esempio

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

Sposta i bit in un bitset a destra di un numero di posizioni specificato e restituisce il risultato nel bitset di destinazione.

bitset<N>& operator>>=(size_t pos);

Parametri

pos
Il numero di posizioni a destra dei bit in bitset deve essere spostato.

Valore restituito

Modifica di destinazione bitset in modo che i bit siano stati spostati a destra il numero di posizioni richiesto.

Osservazioni:

Se non esiste un elemento da spostare nella posizione, la funzione cancella il bit impostandolo su 0.

Esempio

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

Restituisce un riferimento a un bit in una posizione specificata in un bitset se il bitset è modificabile. In caso contrario, restituisce il valore del bit in tale posizione.

bool operator[](size_t pos) const;
reference operator[](size_t pos);

Parametri

pos
Posizione che individua il bit all'interno di bitset.

Osservazioni:

Quando si definisce _ITERATOR_DEBUG_LEVEL come 1 o 2 nella compilazione, si verificherà un errore di runtime nel file eseguibile se si tenta di accedere a un elemento al di fuori dei limiti di bitset. Per altre informazioni, vedere Checked Iterators.

Esempio

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

Esegue una combinazione bit per bit di bitset con l'operazione "xor" bit per bit (^).

bitset<N>& operator^=(const bitset<N>& right);

Parametri

right
Oggetto bitset da combinare bit per bit con il bitset di destinazione.

Valore restituito

Bitset di destinazione modificato risultante dall'operazione "xor" bit per bit con^ l'oggetto bitset specificato come parametro.

Osservazioni:

Due bit combinati dall'operatore "xor" bit per bit (^) restituiscono true se almeno uno, ma non entrambi, dei bit sono true; in caso contrario, la combinazione restituisce false.

I bitset devono avere le stesse dimensioni.

Esempio

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

Combina due bitset usando l'operazione bit per bit "o" (|).

bitset<N>& operator|=(const bitset<N>& right);

Parametri

right
Oggetto bitset che deve essere combinato bit per bit con la destinazione bitset.

Valore restituito

Bitset di destinazione modificato risultante dall'operazione bit per bit "o" (|) con l'oggetto bitset specificato come parametro.

Osservazioni:

Due bit combinati dall'operatore inclusivo OR restituiscono true se almeno uno dei bit è true; se entrambi i bit sono false, la combinazione restituisce false.

I bitset devono avere le stesse dimensioni.

Esempio

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

Inverte tutti i bit in un bitset di destinazione e restituisce il risultato.

bitset<N> operator~() const;

Valore restituito

Oggetto bitset con tutti i suoi bit invertiti rispetto all'oggetto di destinazione bitset.

Esempio

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

Classe proxy che fornisce i riferimenti ai bit contenuti in un bitset usata per accedere e modificare i singoli bit come classe helper per operator[] della classe 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();
};

Parametri

val
Valore dell'oggetto di tipo bool da assegnare a un bit in un oggetto bitset.

bitref
Riferimento del form x [ i ] al bit in corrispondenza della posizione i in bitsetx.

Valore restituito

Riferimento al bit nell'oggetto bitset specificato dalla posizione dell'argomento per la prima, la seconda e la quinta funzione membro del riferimento di classe e true o false, per riflettere il valore del bit modificato in bitset per la terza e quarta funzione membro del riferimento di classe.

Osservazioni:

La classe reference esiste solo come classe helper per .bitsetoperator[] La classe membro descrive un oggetto che può accedere a un singolo bit all'interno di un oggetto bitset. Si supponga di b essere un oggetto di tipo e y di oggetti di tipo boolbitset<N>x e i di j posizioni valide all'interno di tale oggetto. La notazione x [i] fa riferimento al bit in corrispondenza della posizione i in bitset x. Le funzioni membro della classe reference specificano, in ordine, le operazioni seguenti:

Operazione Definizione
x[i] = b Archivia bool il valore b in corrispondenza della posizione i di bit in bitset x.
x[i] = y[j] Archivia il valore del bit y[ j] in posizione i di bit nel bitset x.
b = ~ x[i] Archivia il valore capovolto del bit x[ i] in boolb.
b = x[i] Archivia il valore del bit x[ i] in boolb.
x[i]. flip( ) Archivia il valore capovolto del bit x[ i] in corrispondenza della posizione i di bit in x.

Esempio

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

Reimposta tutti i bit in bitset su 0 o un bit in una posizione specificata su 0.

bitset<N>& reset();
bitset<N>& reset(size_t pos);

Parametri

pos
Posizione del bit in bitset da reimpostare su 0.

Valore restituito

Copia dell'oggetto bitset per il quale è stata richiamata la funzione membro.

Osservazioni:

La seconda funzione membro genera un'eccezione out_of_range se la posizione specificata è maggiore delle dimensioni dell'oggetto bitset.

Esempio

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

Imposta tutti i bit in bitset su 1 o un bit in una posizione specificata su 1.

bitset<N>& set();

bitset<N>& set(
    size_t pos,
    bool val = true);

Parametri

pos
Posizione del bit in bitset da impostare il valore assegnato.

val
Valore da assegnare al bit nella posizione specificata.

Valore restituito

Copia dell'oggetto bitset per il quale è stata richiamata la funzione membro.

Osservazioni:

La seconda funzione membro genera un'eccezione out_of_range se la posizione specificata è maggiore delle dimensioni dell'oggetto bitset.

Esempio

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

Restituisce il numero di bit in un oggetto bitset.

size_t size() const;

Valore restituito

Numero di bit, , Nin un oggetto bitset<N>.

Esempio

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

Verifica se il bit in una posizione specificata in bitset è impostato su 1.

bool test(size_t pos) const;

Parametri

pos
Posizione del bit in bitset da testare per il relativo valore.

Valore restituito

true se il bit specificato dalla posizione dell'argomento è impostato su 1; in caso contrario, false.

Osservazioni:

La funzione membro genera un'eccezione out_of_range

to_string

Converte un oggetto bitset in una rappresentazione di stringa.

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;

Valore restituito

Oggetto stringa della classe basic_string, dove ogni bit impostato in bitset ha un carattere corrispondente pari a 1 e un carattere pari a 0 se il bit non è impostato.

Esempio

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

Restituisce un unsigned long long valore che contiene gli stessi bit impostati come contenuto dell'oggetto bitset .

unsigned long long to_ullong() const;

Valore restituito

Restituisce la somma dei valori di bit presenti nella sequenza di bit come unsigned long long. Questo unsigned long long valore crea nuovamente gli stessi bit di set se viene usato per inizializzare un oggetto bitset.

Eccezioni

Genera un overflow_error oggetto se un bit nella sequenza di bit ha un valore di bit che non può essere rappresentato come valore di tipo unsigned long long.

Osservazioni:

Restituisce la somma dei valori di bit presenti nella sequenza di bit come unsigned long long.

to_ulong

Converte un bitset oggetto nell'intero che genererebbe la sequenza di bit contenuti se utilizzata per inizializzare .bitset

unsigned long to_ulong( ) const;

Valore restituito

Intero che genera i bit in un bitset oggetto se utilizzato nell'inizializzazione dell'oggetto bitset.

Osservazioni:

L'applicazione della funzione membro restituirà l'intero con la stessa sequenza di 1 e 0 cifre trovate nella sequenza di bit contenuti in bitset.

La funzione membro genera un overflow_error oggetto se un bit nella sequenza di bit ha un valore di bit che non può essere rappresentato come valore di tipo unsigned long.

Esempio

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