Files
2025-10-25 03:02:53 +03:00

64 lines
5.1 KiB
Markdown
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

[template.bitset.general]
# 22 General utilities library [[utilities]](./#utilities)
## 22.9 Bitsets [[bitset]](bitset#template.bitset.general)
### 22.9.2 Class template bitset [[template.bitset]](template.bitset#general)
#### 22.9.2.1 General [template.bitset.general]
[🔗](#lib:bitset)
namespace std {template<size_t N> class bitset {public:// bit referenceclass reference {public:constexpr reference(const reference&) = default; constexpr ~reference(); constexpr reference& operator=(bool x) noexcept; // for b[i] = x;constexpr reference& operator=(const reference&) noexcept; // for b[i] = b[j];constexpr bool operator~() const noexcept; // flips the bitconstexpr operator bool() const noexcept; // for x = b[i];constexpr reference& flip() noexcept; // for b[i].flip();}; // [[bitset.cons]](bitset.cons "22.9.2.2Constructors"), constructorsconstexpr bitset() noexcept; constexpr bitset(unsigned long long val) noexcept; template<class charT, class traits, class Allocator>constexpr explicit bitset(const basic_string<charT, traits, Allocator>& str, typename basic_string<charT, traits, Allocator>::size_type pos = 0, typename basic_string<charT, traits, Allocator>::size_type n = basic_string<charT, traits, Allocator>::npos,
charT zero = charT('0'),
charT one = charT('1')); template<class charT, class traits>constexpr explicit bitset( basic_string_view<charT, traits> str, typename basic_string_view<charT, traits>::size_type pos = 0, typename basic_string_view<charT, traits>::size_type n = basic_string_view<charT, traits>::npos,
charT zero = charT('0'),
charT one = charT('1')); template<class charT>constexpr explicit bitset(const charT* str, typename basic_string_view<charT>::size_type n = basic_string_view<charT>::npos,
charT zero = charT('0'),
charT one = charT('1')); // [[bitset.members]](bitset.members "22.9.2.3Members"), bitset operationsconstexpr bitset& operator&=(const bitset& rhs) noexcept; constexpr bitset& operator|=(const bitset& rhs) noexcept; constexpr bitset& operator^=(const bitset& rhs) noexcept; constexpr bitset& operator<<=(size_t pos) noexcept; constexpr bitset& operator>>=(size_t pos) noexcept; constexpr bitset operator<<(size_t pos) const noexcept; constexpr bitset operator>>(size_t pos) const noexcept; constexpr bitset& set() noexcept; constexpr bitset& set(size_t pos, bool val = true); constexpr bitset& reset() noexcept; constexpr bitset& reset(size_t pos); constexpr bitset operator~() const noexcept; constexpr bitset& flip() noexcept; constexpr bitset& flip(size_t pos); // element accessconstexpr bool operator[](size_t pos) const; constexpr reference operator[](size_t pos); constexpr unsigned long to_ulong() const; constexpr unsigned long long to_ullong() const; template<class charT = char, class traits = char_traits<charT>, class Allocator = allocator<charT>>constexpr basic_string<charT, traits, Allocator> to_string(charT zero = charT('0'), charT one = charT('1')) const; // observersconstexpr size_t count() const noexcept; constexpr size_t size() const noexcept; constexpr bool operator==(const bitset& rhs) const noexcept; constexpr bool test(size_t pos) const; constexpr bool all() const noexcept; constexpr bool any() const noexcept; constexpr bool none() const noexcept; }; // [[bitset.hash]](bitset.hash "22.9.3bitset hash support"), hash supporttemplate<class T> struct hash; template<size_t N> struct hash<bitset<N>>;}
[1](#1)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L10478)
The class templatebitset<N> describes an object that can store a sequence consisting of a fixed number of
bits, N[.](#1.sentence-1)
[2](#2)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L10484)
Each bit represents either the value zero (reset) or one (set)[.](#2.sentence-1)
To[*toggle*](#def:toggle) a bit is to change the value zero to one, or the value one to
zero[.](#2.sentence-2)
Each bit has a non-negative position pos[.](#2.sentence-3)
When converting
between an object of classbitset<N> and a value of some
integral type, bit position pos corresponds to the[*bit value*](#def:bit_value)1 << pos[.](#2.sentence-4)
The integral value corresponding to two
or more bits is the sum of their bit values[.](#2.sentence-5)
[3](#3)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L10501)
The functions described in [[template.bitset]](template.bitset "22.9.2Class template bitset") can report three kinds of
errors, each associated with a distinct exception:
- [(3.1)](#3.1)
an[*invalid-argument*](#def:invalid-argument) error is associated with exceptions of typeinvalid_argument ([[invalid.argument]](invalid.argument "19.2.5Class invalid_­argument"));
- [(3.2)](#3.2)
an[*out-of-range*](#def:out-of-range) error is associated with exceptions of typeout_of_range ([[out.of.range]](out.of.range "19.2.7Class out_­of_­range"));
- [(3.3)](#3.3)
an[*overflow*](#def:overflow) error is associated with exceptions of typeoverflow_error ([[overflow.error]](overflow.error "19.2.10Class overflow_­error"))[.](#3.sentence-1)