Freigeben über


bitset::bitset

Constructs an object of class bitset<N> and initializes the bits to zero, or to some specified value, or to values obtained from characters in a string.

bitset( );
bitset(
   unsigned long _Val
);
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);
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,
                typename basic_string<CharType, Traits, Allocator>::size_type _Count);

Parameters

  • _Val
    The positive integer whose base two representation is used to initialize the bits in the bitset being constructed.

  • _Str
    The string of zeros and ones used to initialize the bitset bit values.

  • _Pos
    The position of the character in the string, counting from left to right and beginning with zero, used to initialize the first bit in the bitset.

  • _Count
    The number of characters in the string used to provide initial values for the bits in the bitset.

Remarks

Three member functions can be used to construct objects of class bitset<N>:

  • The first member function constructs an object of class bitset<N> and initializes all N bits to a default value of zero.

  • The second member function constructs an object of class bitset<N>, initializing the N bits to values that correspond to a base two representation of the positive integer specified in _Val. If the length of the base two integer exceeds the number of bits N in the bitset, then the first N places of the base two integer are used to initialize the bitset bit values. If the number of bits in the bitset exceeds the length of the base two number, then the additional bits are initialized to the default value of zero.

  • The third member function constructs an object of class bitset<N> and initializes bits from the characters provided in a string of zeroes and ones. If any characters of the string are other than 0 or 1, the constructor throws an object of class invalid argument. If the position specified is beyond the length of the string, then the constructor throws an object of class out_of_range, which is available when the <stdexcept> header is included. The constructor sets only those bits at position j in the bitset for which the character in the string at position pos** +** j is 1.

Example

// 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 expresssion
   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 the third 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 ).

Requirements

Header: <bitset>

Namespace: std

See Also

Concepts

bitset Class

bitset Members