std::bitset::bitset(3)
π std::bitset::bitset construction station π
okay so welcome to the std::bitset::bitset(3) page from the C++ Standard Libary β yes bestie, that's how it's spelled here, don't come for me π
. This is the constructor page, and it's lowkey iconic because there are several ways to birth a bitset.
πΈ NAME πΈ
std::bitset::bitset - std::bitset::bitset
β¨ Synopsis β¨
here's the whole overload lineup, with all the version tags still attached because they matter, fr fr:
bitset(); (until
C++11)
constexpr bitset() noexcept; (since
C++11)
bitset( unsigned long val );
(until C++11)
constexpr bitset( unsigned long long val )
(since C++11)
noexcept;
template< class CharT, class Traits, class Alloc >
explicit bitset( const std::basic_string<CharT,
Traits, Alloc>& str,
typename std::basic_string<CharT,
Traits, Alloc>::size_type
pos = 0, (3)
(constexpr since
typename std::basic_string<CharT,
C++23)
Traits, Alloc>::size_type
n = std::basic_string<CharT,
Traits, Alloc>::npos,
CharT zero = CharT('0'),
(1)
CharT one = CharT('1') );
template< class CharT, class Traits > (2)
constexpr explicit bitset(
std::basic_string_view<CharT, Traits> str,
std::size_t pos = 0,
std::size_t n = (4)
(since C++26)
std::size_t(-1),
CharT zero = CharT('0'),
CharT one = CharT('1')
);
template< class CharT >
explicit bitset( const CharT* str,
(since C++11)
std::size_t n = std::size_t(-1), (5)
(constexpr since
CharT zero = CharT('0'),
C++23)
CharT one = CharT('1') );
okay so now the actual tea β β Constructs a new bitset from one of several optional data sources:
- Default constructor. Constructs a bitset with all bits set to zero.
- Constructs a bitset, initializing the first (rightmost, least significant)
Mbit positions to the corresponding bit values ofval, whereMis the smaller of:N, and- the number of bits in the value representation of
unsigned long(until C++11). For typical implementations, it is32or64. - the number of bits in the value representation of
unsigned long long(since C++11). For typical implementations, it is64.
If M is less than N, the remaining bit positions are initialized to zeroes.
- Constructs a bitset using the characters in the
std::basic_stringstr. An optional starting positionposand lengthncan be provided, as well as characters denoting alternate values for set (one) and unset (zero) bits.Traits::eq()is used to compare the character values.
The effective length of the initializing string is std::min(n, str.size() - pos).
If pos > str.size(), this constructor throws std::out_of_range. If any character examined in str is not zero or one, this constructor throws std::invalid_argument.
- Similar to (3), but uses a
std::basic_string_viewinstead of astd::basic_string. - Similar to (3), but uses a
const CharT*instead of astd::basic_string. Equivalent to:
bitset(n == std::size_t(-1)
? basic_string<CharT>(str)
: basic_string<CharT>(str, n), 0, n, zero, one)
Until C++26: this constructor may dynamically allocate memory, although implementations generally avoid dynamic allocation.
Since C++26: this constructor does not dynamically allocate memory, as if only std::basic_string_view is involved.
π«Ά Parameters π«Ά
- val β number used to initialize the bitset
- str β string used to initialize the bitset
- pos β a starting offset into str
- n β number of characters to use from str
- one β alternate character for set bits in str
- zero β alternate character for unset bits in str
π Exceptions π
3,4)std::out_of_rangeifpos > str.size(),std::invalid_argumentif any character is not one or zero.5)std::invalid_argumentif any character is not one or zero.
π§ Notes π§
feature-test macro check, iykyk:
- Feature-test macro:
__cpp_lib_constexpr_bitset; Value:202207L; Std:(C++23); Feature: A more constexprstd::bitset, overloads(3,5) - Feature-test macro:
__cpp_lib_bitset; Value:202306L; Std:(C++26); Feature: Interfacingstd::bitsetwithstd::string_view,(4)
π Example π
// Run this code
#include <bitset>
#include <climits>
#include <iostream>
#include <string>
int main()
{
// empty constructor (1)
std::bitset<8> b1; // [0,0,0,0,0,0,0,0]
// unsigned long long constructor (2)
std::bitset<8> b2(42); // [0,0,1,0,1,0,1,0]
std::bitset<70> bl(ULLONG_MAX); // [0,0,0,0,0,0,1,1,1,...,1,1,1] in
C++11
std::bitset<8> bs(0xfff0); // [1,1,1,1,0,0,0,0]
// string constructor (3)
std::string bit_string = "110010";
std::bitset<8> b3(bit_string); // [0,0,1,1,0,0,1,0]
std::bitset<8> b4(bit_string, 2); // [0,0,0,0,0,0,1,0]
std::bitset<8> b5(bit_string, 2, 3); // [0,0,0,0,0,0,0,1]
// string constructor using custom zero/one digits (3)
std::string alpha_bit_string = "aBaaBBaB";
std::bitset<8> b6(alpha_bit_string, 0, alpha_bit_string.size(),
'a', 'B'); // [0,1,0,0,1,1,0,1]
// char* constructor using custom digits (5)
std::bitset<8> b7("XXXXYYYY", 8, 'X', 'Y'); // [0,0,0,0,1,1,1,1]
std::cout << "b1: " << b1 << "\nb2: " << b2 << "\nbl: " << bl
<< "\nbs: " << bs << "\nb3: " << b3 << "\nb4: " << b4
<< "\nb5: " << b5 << "\nb6: " << b6 << "\nb7: " << b7 <<
'\n';
}
π Possible output π
b1: 00000000
b2: 00101010
bl:
0000001111111111111111111111111111111111111111111111111111111111111111
bs: 11110000
b3: 00110010
b4: 00000010
b5: 00000001
b6: 01001101
b7: 00001111
π₯ Defect reports π₯
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
LWG 396, applied toC++98β Behavior as published: the values of thezeroandonecharacters for overload(3)were0and1(which do not correspond to'0'and'1'). Correct behavior: added parameters to provide values for these characters.LWG 457, applied toC++98β Behavior as published:Mis the smaller ofNand the valueCHAR_BIT * sizeof(unsigned long)for overload(2), butunsigned longis not guaranteed to use all its bits to represent its value. Correct behavior: consider the number of bits of the value representation instead.
π See also π
setβ sets bits to true or given value (public member function)resetβ sets bits to false (public member function)
kisses bestie π this page came from http://cppreference.com, dated 2024.06.10, and it's all about std::bitset::bitset(3) β go build something iconic π«Άβ¨
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms