CARVIEW |
Select Language
HTTP/1.1 301 Moved Permanently
Server: nginx
Date: Thu, 24 Jul 2025 16:49:14 GMT
Content-Type: text/html; charset=UTF-8
Transfer-Encoding: chunked
Connection: keep-alive
Location: /reference/bitset/bitset/bitset/
HTTP/1.1 200 OK
Server: nginx
Date: Thu, 24 Jul 2025 16:49:14 GMT
Content-Type: text/html; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
ETag: W/"7f10-u8FUkbhcLL7u503VMluEP/GgdYY"
Content-Encoding: gzip
Constructs a bitset container 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.
Output:
The other constructors cause no side effects in case an exception is thrown (strong guarantee).
Throws out_of_range if
Reference
C library:
- <cassert> (assert.h)
- <cctype> (ctype.h)
- <cerrno> (errno.h)
-
<cfenv> (fenv.h)C++11
- <cfloat> (float.h)
-
<cinttypes> (inttypes.h)C++11
- <ciso646> (iso646.h)
- <climits> (limits.h)
- <clocale> (locale.h)
- <cmath> (math.h)
- <csetjmp> (setjmp.h)
- <csignal> (signal.h)
- <cstdarg> (stdarg.h)
-
<cstdbool> (stdbool.h)C++11
- <cstddef> (stddef.h)
-
<cstdint> (stdint.h)C++11
- <cstdio> (stdio.h)
- <cstdlib> (stdlib.h)
- <cstring> (string.h)
-
<ctgmath> (tgmath.h)C++11
- <ctime> (time.h)
-
<cuchar> (uchar.h)C++11
- <cwchar> (wchar.h)
- <cwctype> (wctype.h)
Containers:
-
<array>C++11
- <deque>
-
<forward_list>C++11
- <list>
- <map>
- <queue>
- <set>
- <stack>
-
<unordered_map>C++11
-
<unordered_set>C++11
- <vector>
-
Input/Output:
Multi-threading:
-
<atomic>C++11
-
<condition_variable>C++11
-
<future>C++11
-
<mutex>C++11
-
<thread>C++11
-
Other:
- <algorithm>
- <bitset>
-
<chrono>C++11
-
<codecvt>C++11
- <complex>
- <exception>
- <functional>
-
<initializer_list>C++11
- <iterator>
- <limits>
- <locale>
- <memory>
- <new>
- <numeric>
-
<random>C++11
-
<ratio>C++11
-
<regex>C++11
- <stdexcept>
- <string>
-
<system_error>C++11
-
<tuple>C++11
-
<type_traits>C++11
-
<typeindex>C++11
- <typeinfo>
- <utility>
- <valarray>
bitset
- bitset::bitset
- bitset operators
member classes
member functions
non-member specializations
-
hash<bitset>C++11
-
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
- (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
|
|
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
- bitset::set
- Set bits (public member function)
- bitset::reset
- Reset bits (public member function)
- bitset::operator[]
- Access bit (public member function)
- bitset operators
- Bitset operators (function)
Home page | Privacy policy
© cplusplus.com, 2000-2025 - All rights reserved - v3.3.4s
Spotted an error? contact us
© cplusplus.com, 2000-2025 - All rights reserved - v3.3.4s
Spotted an error? contact us