Bitset
Bitset
<bitset>
std:: bitset::bitset
C++98
C++11
bitset();
default (1)
bitset (unsigned long val);
integer value (2)
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,
string (3)
typename basic_string<charT,traits,Alloc>::size_type n =
basic_string<charT,traits,Alloc>::npos);
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 bitsetobject.
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
C++98
C++11
Example
1 // constructing bitsets Edit & Run
2 #include <iostream> // std::cout
3 #include <string> // std::string
4 #include <bitset> // std::bitset
5
6 int main ()
7 {
8 std::bitset<16> foo;
9 std::bitset<16> bar (0xfa2);
10 std::bitset<16> baz (std::string("0101111001"));
11
12 std::cout << "foo: " << foo << '\n';
13 std::cout << "bar: " << bar << '\n';
14 std::cout << "baz: " << baz << '\n';
15
16 return 0;
17 }
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().
Source is http://www.cplusplus.com/reference/bitset/bitset/bitset/