Bitset Constructors

C++ Reference

Bitset Constructors
Syntax:
  #include <bitset>
  bitset();
  bitset( unsigned long val );

Bitsets can either be constructed with no arguments or with an unsigned long number val that will be converted into binary and inserted into the bitset. When creating bitsets, the number given in the place of the template determines how long the bitset is.

For example, the following code creates two bitsets and displays them:

 // create a bitset that is 8 bits long
 bitset<8> bs;
 // display that bitset
 for( int i = (int) bs.size()-1; i >= 0; i-- ) {
   cout << bs[i] << " ";
 }
 cout << endl;
 // create a bitset out of a number
 bitset<8> bs2( (long) 131 );
 // display that bitset, too
 for( int i = (int) bs2.size()-1; i >= 0; i-- ) {
   cout << bs2[i] << " ";
 }
 cout << endl;