0% found this document useful (0 votes)
45 views3 pages

Bitset

This document summarizes the constructors for the std::bitset class in C++. It describes 4 constructors: (1) a default constructor that initializes all bits to 0, (2) a constructor that initializes the bits from an integer value, (3) a constructor that initializes the bits from a string, reading characters as 0s and 1s, and (4) a constructor that initializes from a C-string. It provides details on how each constructor initializes the bits and handles values that are larger or smaller than the bitset size.

Uploaded by

Lucas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views3 pages

Bitset

This document summarizes the constructors for the std::bitset class in C++. It describes 4 constructors: (1) a default constructor that initializes all bits to 0, (2) a constructor that initializes the bits from an integer value, (3) a constructor that initializes the bits from a string, reading characters as 0s and 1s, and (4) a constructor that initializes from a C-string. It provides details on how each constructor initializes the bits and handles values that are larger or smaller than the bitset size.

Uploaded by

Lucas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

public member function

<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

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

You might also like