[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 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.2 Constructors"), constructorsconstexpr bitset() noexcept; constexpr bitset(unsigned long long val) noexcept; templateconstexpr explicit bitset(const basic_string& str, typename basic_string::size_type pos = 0, typename basic_string::size_type n = basic_string::npos, charT zero = charT('0'), charT one = charT('1')); templateconstexpr explicit bitset( basic_string_view str, typename basic_string_view::size_type pos = 0, typename basic_string_view::size_type n = basic_string_view::npos, charT zero = charT('0'), charT one = charT('1')); templateconstexpr explicit bitset(const charT* str, typename basic_string_view::size_type n = basic_string_view::npos, charT zero = charT('0'), charT one = charT('1')); // [[bitset.members]](bitset.members "22.9.2.3 Members"), 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 Allocator = allocator>constexpr basic_string 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.3 bitset hash support"), hash supporttemplate struct hash; template struct hash>;} [1](#1) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L10478) The class templatebitset 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 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.2 Class 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.5 Class 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.7 Class 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.10 Class overflow_­error"))[.](#3.sentence-1)