public member function
<bitset>

std::bitset::bitset

default (1)
bitset();
integer value (2)
bitset (unsigned long val);
string (3)
template<class charT, class traits, class Alloc>  explicit bitset (const basic_string<charT,traits,Alloc>& str,    typename basic_string<charT,traits,Alloc>::size_type pos = 0,    typename basic_string<charT,traits,Alloc>::size_type n =      basic_string<charT,traits,Alloc>::npos);
default (1)
constexpr bitset() noexcept;
integer value (2)
constexpr bitset (unsigned long long val) noexcept;
string (3)
template <class charT, class traits, class Alloc>  explicit bitset (const basic_string<charT,traits,Alloc>& str,    typename basic_string<charT,traits,Alloc>::size_type pos = 0,    typename basic_string<charT,traits,Alloc>::size_type n =      basic_string<charT,traits,Alloc>::npos,    charT zero = charT('0'), charT one = charT('1'));
C-string (4)
template <class charT>  explicit bitset (const charT* str,    typename basic_string<charT>::size_type n = basic_string<charT>::npos,    charT zero = charT('0'), charT one = charT('1'));
Construct bitset
Constructs a bitset container object:
(1) default constructor
The object is initialized with zeros.
(2) initialization from integer value
Initializes the object with the bit values of val:
(3) initialization from string or (4) C-string
Uses the sequence of zeros and/or ones in str to initialize the first n bit positions of the constructed bitset object.

Note that bitset objects have a fixed size (determined by their class template argument) no matter the constructor used: Those bit positions not explicitly set by the constructor are initialized with a value of zero.

Parameters

val
Integral value whose bits are copied to the bitset positions.
- If the value representation of val is greater than the bitset size, only the least significant bits of val are taken into consideration.
- If the value representation of val is less than the bitset size, the remaining bit positions are initialized to zero.
str
A basic_string whose contents are used to initialize the bitset:
The constructor parses the string reading at most n characters beginning at pos, interpreting the character values '0' and '1' as zero and one, respectively.
Note that the least significant bit is represented by the last character read (not the first); Thus, the first bit position is read from the right-most character, and the following bits use the characters preceding this, from right to left.
If this sequence is shorter than the bitset size, the remaining bit positions are initialized to zero.
A basic_string or C-string whose contents are used to initialize the bitset:
The constructor parses the string reading at most n characters (beginning at pos for (3)), interpreting the character values specified as arguments zero and one as zeros and ones, respectively.
Note that the least significant bit is represented by the last character read (not the first); Thus, the first bit position is read from the right-most character, and the following bits use the characters preceding this, from right to left.
If this sequence is shorter than the bitset size, the remaining bit positions are initialized to zero.
pos
First character in the basic_string to be read and interpreted.
If this is greater than the length of str, an out_of_range exception is thrown.
n
Number of characters to read. Any value greater than the bitset size (including npos) is equivalent to specifying exactly the bitset size.
zero, one
Character values to represent zero and one.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// constructing bitsets
#include <iostream>       // std::cout
#include <string>         // std::string
#include <bitset>         // std::bitset

int main ()
{
  std::bitset<16> foo;
  std::bitset<16> bar (0xfa2);
  std::bitset<16> baz (std::string("0101111001"));

  std::cout << "foo: " << foo << '\n';
  std::cout << "bar: " << bar << '\n';
  std::cout << "baz: " << baz << '\n';

  return 0;
}

Output:

foo: 0000000000000000
bar: 0000111110100010
baz: 0000000101111001


Data races

Constructors (3) and (4) access the characters in str.

Exception safety

Neither the default constructor (1) nor the constructor from integer value (2) throw exceptions.
The other constructors cause no side effects in case an exception is thrown (strong guarantee).
Throws out_of_range if pos > str.size().

See also