Partager via


Classe bitset

Décrit un type d'objet stockant une séquence composée d'un nombre fixe de bits qui offrent un moyen compact de conserver des indicateurs d'un ensemble d'éléments ou de conditions. La bitset classe prend en charge les opérations sur les objets de type bitset qui contiennent une collection de bits et fournissent un accès à temps constant à chaque bit.

Syntaxe

template <size_t N>
class bitset

Paramètres

N
Spécifie le nombre de bits dans l’objet avec un entier différent de zéro du type size_t qui doit être connu au moment de la bitset compilation.

Notes

Contrairement à la classe similairevector<bool>, la bitset classe n’a pas d’itérateurs et n’est pas un conteneur de bibliothèque standard C++. Il diffère également d’être vector<bool> d’une taille spécifique qui est fixe au moment de la compilation conformément à la taille spécifiée par le paramètre N de modèle lorsque l’objet bitset<N> est déclaré.

Un bit est défini si sa valeur est 1, et est réinitialisé si sa valeur est 0. Basculer ou inverser un bit consiste à passer sa valeur de 1 à 0 ou de 0 à 1. Les N bits d’un bitset sont indexés par des valeurs entières comprises entre 0 et N 1, où 0 indexe la première position de bits et N - 1 la position finale du bit.

Membres

Constructeurs

Nom Description
bitset Construit un objet de la classe bitset<N> et initialise les bits à zéro, à une valeur spécifiée ou à des valeurs obtenues auprès des caractères d'une chaîne.

Typedefs

Nom Description
element_type Type qui est un synonyme du type de données bool et qui peut être utilisé pour référencer les bits des éléments d'un bitset.

Functions

Nom Description
all Teste tous les bits de ce bitset qui suit pour déterminer s’ils sont tous définis truesur .
any La fonction membre teste si des bits de la séquence sont définis sur 1.
count La fonction membre retourne le nombre de bits définis dans la séquence de bits.
flip Inverse la valeur de tous les bits d’un bitset ou inverse un seul bit à une position spécifiée.
none Teste si aucun bit n'a été défini sur 1 dans un objet bitset.
reset Réinitialise tous les bits d'un bitset à 0 ou réinitialise à 0 un bit à une position spécifiée.
set Définit tous les bits d'un bitset sur 1 ou définit sur 1 un bit à une position spécifiée.
size Retourne le nombre de bits d'un objet bitset.
test Teste si le bit à une position spécifiée d'un bitset est défini sur 1.
to_string Convertit un objet bitset en une représentation sous forme de chaîne.
to_ullong Retourne la somme des valeurs des bits d'un bitset sous forme de unsigned long long.
to_ulong Convertit un objet bitset en un unsigned long qui générerait la séquence de bits contenue s'il était utilisé pour initialiser le bitset.

Classes

Nom Description
reference Classe proxy qui fournit des références aux bits contenus dans un bitset et qui est utilisée pour accéder aux bits individuels et pour s'en servir comme une classe d'assistance pour l'opérateur operator[] de la classe bitset.

Opérateurs

Nom Description
operator!= Teste l'inégalité d'un bitset cible avec un bitset spécifié.
operator&= Effectue une combinaison de bitsets au niveau du bit avec l’opération « and » (&) au niveau du bit.
operator<< Décale les bits d'un bitset vers la gauche d'un nombre spécifié de positions et retourne le résultat dans un nouveau bitset.
operator<<= Décale les bits d'un bitset vers la gauche d'un nombre spécifié de positions et retourne le résultat dans le bitset ciblé.
operator== Teste l'égalité d'un bitset cible avec un bitset spécifié.
operator>> Décale les bits d'un bitset vers la droite d'un nombre spécifié de positions et retourne le résultat dans un nouveau bitset.
operator>>= Décale les bits d'un bitset vers la droite d'un nombre spécifié de positions et retourne le résultat dans le bitset ciblé.
operator[] Retourne une référence à un bit à une position spécifiée d'un bitset si le bitset est modifiable ; sinon, retourne la valeur du bit à cette position.
operator^= Effectue une combinaison de bitsets au niveau du bit avec l’opération « xor » (^) au niveau du bit.
operator|= Effectue une combinaison de bitsets au niveau du bit avec l’opération « ou » (|) au niveau du bit.
operator~ Inverse tous les bits d’un bitset cible et retourne le résultat.

Structures

Nom Description
hash

all

Teste tous les bits de ce bitset pour déterminer s’ils sont tous définis sur true.

bool all() const;

Valeur de retour

Retourne true si tous les bits de cet ensemble sont vrais. Retourne false si un ou plusieurs bits sont faux.

any

Vérifie si des bits de la séquence sont définis sur 1.

bool any() const;

Valeur de retour

true si un bit est bitset défini sur 1 ; false si tous les bits sont 0.

Exemple

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

Construit un objet de la classe bitset<N> et initialise les bits avec la valeur zéro, une valeur spécifiée ou des valeurs obtenues à partir des caractères d’une chaîne.

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

Paramètres

val
Entier non signé dont la représentation de base deux est utilisée pour initialiser les bits dans la bitset construction en cours.

str
Chaîne de zéros et celles utilisées pour initialiser les bitset valeurs de bits.

pos
Position du caractère dans la chaîne, en comptant de gauche à droite et en commençant par zéro, utilisée pour initialiser le premier bit dans le bitset.

count
Nombre de caractères dans la chaîne utilisée pour fournir des valeurs initiales pour les bits dans le bitset.

Zero
Caractère utilisé pour représenter la valeur zéro. La valeur par défaut est « 0 ».

One
Caractère utilisé pour représenter la valeur un. La valeur par défaut est « 1 ».

Notes

1) Construit un objet de classe bitset<N> et initialise tous les bits N à une valeur par défaut de zéro.

2-3) Construit un objet de classe bitset<N> et initialise les bits du val paramètre.

4) Construit un objet de classe bitset<N> et initialise les bits des caractères fournis dans une chaîne de zéros et un. Si des caractères de la chaîne sont autres que 0 ou 1, le constructeur lève un objet de classe invalid argument. Si la position spécifiée (pos) dépasse la longueur de la chaîne, le constructeur lève un objet de classe out_of_range. Le constructeur définit uniquement les bits à la position j dans laquelle bitset le caractère de la chaîne à la position pos + j est 1. Par défaut, pos est 0.

5) Similaire à 4) mais inclut un autre paramètre, countqui spécifie le nombre de bits à initialiser. Il a deux paramètres facultatifs, _Zero et _One, qui indiquent le caractère dans str lequel il doit être interprété pour signifier un 0 bits et un 1 bits, respectivement.

6) Construit un objet de classe bitset<N>, initialisant les bits N en valeurs qui correspondent aux caractères fournis dans une chaîne de caractères de style C de zéros et une. Vous appelez le constructeur sans transformer la chaîne en un type de chaîne, par exemple : bitset<5> b5("01011");

Exemple

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

Retourne le nombre de bits définis dans la séquence de bits.

size_t count() const;

Valeur de retour

Nombre de bits définis dans la séquence de bits.

Exemple

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

Type qui est un synonyme du type de données bool et qui peut être utilisé pour référencer les bits des éléments d'un bitset.

typedef bool element_type;

Exemple

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

Inverse la valeur de tous les bits d’un bitset ou inverse un seul bit à une position spécifiée.

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

Paramètres

pos
Position du bit dont la valeur doit être inversée.

Valeur de retour

Copie de la fonction membre pour bitset laquelle la fonction membre a été appelée.

Notes

La deuxième fonction membre lève une out_of_range exception si la position spécifiée en tant que paramètre est supérieure à la taille N du bit dont le bitset<N> bit a été inversé.

Exemple

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

aucune

Teste si aucun bit n'a été défini sur 1 dans un objet bitset.

bool none() const;

Valeur de retour

true si aucun bit n’a bitset été défini sur 1 ; false si au moins un bit a été défini sur 1.

Exemple

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

Vérifie si un bitset cible n’est pas égal à un bitset spécifié.

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

Paramètres

right
Qui bitset doit être comparé au bitset cible pour l’inégalité.

Valeur de retour

true si les bitsets sont différents ; false s’ils sont les mêmes.

Notes

Les bitsets doivent être de la même taille.

Exemple

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

Effectue une combinaison de bitsets au niveau du bit avec l’opération « and » (&) au niveau du bit.

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

Paramètres

right
Qui bitset doit être combiné au niveau du bit avec le bitset cible.

Valeur de retour

Bitset cible modifié qui résulte de l’opération « et » au& niveau du bit avec le bitset paramètre spécifié.

Notes

Deux bits combinés par l’opérateur AND retournent true si chaque bit est vrai ; sinon, leur combinaison retourne false.

Les deux bitsets doivent être de la même taille.

Exemple

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

Décale les bits d'un bitset vers la gauche d'un nombre spécifié de positions et retourne le résultat dans un nouveau bitset.

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

Paramètres

pos
Nombre de positions à gauche que les bits dans le bitset fichier doivent être décalés.

Valeur de retour

Bitset modifié avec les bits décalés vers la gauche du nombre nécessaire de positions.

Notes

La fonction d’opérateur membre retourne bitset(*this) <<= pos<<= déplace les bits d’un bitset point vers la gauche un nombre spécifié de positions et retourne le résultat à l’objet ciblé bitset.

Exemple

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

Décale les bits d'un bitset vers la gauche d'un nombre spécifié de positions et retourne le résultat dans le bitset ciblé.

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

Paramètres

pos
Nombre de positions à gauche dans les bitset bits dans devront être décalés.

Valeur de retour

Modification ciblée bitset afin que les bits aient été déplacés vers la gauche le nombre requis de positions.

Notes

Si aucun élément n’existe pour décaler à la position, la fonction efface le bit en le définissant sur 0.

Exemple

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

Vérifie si un bitset cible est égal à un bitset spécifié.

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

Paramètres

right
Qui bitset doit être comparé au bitset cible pour l’égalité.

Valeur de retour

true si les bitsets sont identiques ; false s’ils sont différents.

Notes

Les bitsets doivent être de la même taille.

Exemple

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

Déplace les bits d’un bitset point à droite, un nombre spécifié de positions et retourne le résultat à un nouveau bitset.

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

Paramètres

pos
Nombre de positions à droite des bits dans le bitset champ à déplacer.

Valeur de retour

Un nouveau bitset où les bits ont été déplacés vers la droite le nombre requis de positions par rapport à la cible bitset.

Exemple

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

Décale les bits d'un bitset vers la droite d'un nombre spécifié de positions et retourne le résultat dans le bitset ciblé.

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

Paramètres

pos
Nombre de positions à droite des bits dans le bitset champ à déplacer.

Valeur de retour

Modification ciblée bitset afin que les bits aient été déplacés vers la droite le nombre requis de positions.

Notes

Si aucun élément n’existe pour décaler à la position, la fonction efface le bit en le définissant sur 0.

Exemple

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

Retourne une référence à un bit à une position spécifiée d'un bitset si le bitset est modifiable ; sinon, retourne la valeur du bit à cette position.

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

Paramètres

pos
Position de localisation du bit dans le bitset.

Notes

Lorsque vous définissez _ITERATOR_DEBUG_LEVEL comme 1 ou 2 dans votre build, une erreur d’exécution se produit dans votre exécutable si vous tentez d’accéder à un élément en dehors des limites du bitsetfichier . Pour plus d'informations, consultez Checked Iterators.

Exemple

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

Effectue une combinaison de bitsets au niveau du bit avec l’opération « xor » (^) au niveau du bit.

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

Paramètres

right
Qui bitset doit être combiné au niveau du bit avec le bitset cible.

Valeur de retour

Bitset cible modifié qui résulte de l’opération « xor » au^ niveau du bit avec le bitset paramètre spécifié.

Notes

Deux bits combinés par l’opérateur « xor » au niveau du bit (^) retournent true si au moins un, mais pas les deux, des bits sont true; sinon, leur combinaison renvoie false.

Les bitsets doivent être de la même taille.

Exemple

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

Combine deux bitsets à l’aide de l’opération « ou » au| niveau du bit.

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

Paramètres

right
Qui bitset doit être combiné au niveau du bit avec la cible bitset.

Valeur de retour

Bitset cible modifié qui résulte de l’opération « ou » au| niveau du bit avec le bitset paramètre spécifié.

Notes

Deux bits combinés par l’opérateur inclusif OR retournent true si au moins un des bits est true; si les deux bits sont false, leur combinaison renvoie false.

Les bitsets doivent être de la même taille.

Exemple

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

Inverse tous les bits d’un bitset cible et retourne le résultat.

bitset<N> operator~() const;

Valeur de retour

Avec bitset tous ses bits inversés par rapport à la cible bitset.

Exemple

// 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 qui fournit des références aux bits contenus dans un bitset et qui est utilisée pour accéder aux bits individuels et pour s'en servir comme une classe d'assistance pour l'opérateur operator[] de la 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();
};

Paramètres

val
Valeur de l’objet de type bool à affecter à un bit dans un bitset.

bitref
Référence du formulaire x [ i ] au bit à la position i dans bitsetx.

Valeur de retour

Référence au bit bitset spécifié par la position d’argument pour les premières, deuxième et cinquième fonctions membres de référence de classe, et true ou false, pour refléter la valeur du bit modifié dans la bitset troisième et quatrième fonctions membres de référence de classe.

Notes

La classe reference n’existe qu’en tant que classe d’assistance pour le bitsetoperator[]. La classe membre décrit un objet qui peut accéder à un bit individuel au sein d’un bitset. Supposons qu’il s’agit b d’un objet de type bool, x d’objets y de type bitset<N>et ij de positions valides au sein d’un tel objet. La notation x [i] fait référence au bit à la position i dans le bitset x. Les fonctions membres de la classe reference fournissent, dans l’ordre, les opérations suivantes :

Opération Définition
x[i] = b Stocke la bool valeur b à la position i du bit dans le bitset x.
x[i] = y[j] Stocke la valeur du bit y[ j] à la position i du bit dans le bitset x.
b = ~ x[i] Stocke la valeur retournée du bit x[ i] in boolb.
b = x[i] Stocke la valeur du bit x[ i] in boolb.
x[i]. flip( ) Stocke la valeur retournée du bit x[ i] à la position i du bit en x.

Exemple

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

Réinitialise tous les bits d'un bitset à 0 ou réinitialise à 0 un bit à une position spécifiée.

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

Paramètres

pos
Position du bit dans la bitset valeur 0.

Valeur de retour

Copie de la bitset fonction membre pour laquelle la fonction membre a été appelée.

Notes

La deuxième fonction membre lève une out_of_range exception si la position spécifiée est supérieure à la taille du bitset.

Exemple

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

Définit tous les bits d'un bitset sur 1 ou définit sur 1 un bit à une position spécifiée.

bitset<N>& set();

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

Paramètres

pos
Position du bit dans la bitset valeur affectée.

val
Valeur à affecter au bit à la position spécifiée.

Valeur de retour

Copie de la bitset fonction membre pour laquelle la fonction membre a été appelée.

Notes

La deuxième fonction membre lève une out_of_range exception si la position spécifiée est supérieure à la taille du bitset.

Exemple

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

Retourne le nombre de bits d'un objet bitset.

size_t size() const;

Valeur de retour

Nombre de bits, Ndans un bitset<N>.

Exemple

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

Teste si le bit à une position spécifiée d'un bitset est défini sur 1.

bool test(size_t pos) const;

Paramètres

pos
Position du bit à bitset tester pour sa valeur.

Valeur de retour

true si le bit spécifié par la position de l’argument a la valeur 1 ; sinon, false.

Notes

La fonction membre lève une out_of_range

to_string

Convertit un objet bitset en une représentation sous forme de chaîne.

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;

Valeur retournée

Objet de chaîne de classe basic_string, où chaque bit défini dans le fichier bitset a un caractère correspondant de 1 et un caractère de 0 si le bit n’est pas défini.

Exemple

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

Retourne une unsigned long long valeur qui contient les mêmes bits définis que le contenu de l’objet bitset .

unsigned long long to_ullong() const;

Valeur retournée

Retourne la somme des valeurs de bits qui se trouvent dans la séquence de bits en tant que unsigned long long. Cette unsigned long long valeur recréerait les mêmes bits de jeu s’il est utilisé pour initialiser un bitset.

Exceptions

Lève un objet si un overflow_error bit dans la séquence de bits a une valeur de bit qui ne peut pas être représentée comme valeur de type unsigned long long.

Notes

Retourne la somme des valeurs de bits qui se trouvent dans la séquence de bits en tant que unsigned long long.

to_ulong

Convertit un bitset objet en entier qui générerait la séquence de bits contenue si elle était utilisée pour initialiser le bitset.

unsigned long to_ulong( ) const;

Valeur retournée

Entier qui générerait les bits dans un bitset si utilisé dans l’initialisation du bitset.

Notes

L’application de la fonction membre renvoie l’entier qui a la même séquence de 1 et 0 chiffres que celui trouvé dans la séquence de bits contenus dans le bitset.

La fonction membre lève un overflow_error objet si un bit dans la séquence de bits a une valeur de bit qui ne peut pas être représentée comme valeur de type unsigned long.

Exemple

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