[bitset.members] # 22 General utilities library [[utilities]](./#utilities) ## 22.9 Bitsets [[bitset]](bitset#members) ### 22.9.2 Class template bitset [[template.bitset]](template.bitset#bitset.members) #### 22.9.2.3 Members [bitset.members] [🔗](#lib:operator&=,bitset) `constexpr bitset& operator&=(const bitset& rhs) noexcept; ` [1](#1) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L10643) *Effects*: Clears each bit in*this for which the corresponding bit in rhs is clear, and leaves all other bits unchanged[.](#1.sentence-1) [2](#2) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L10649) *Returns*: *this[.](#2.sentence-1) [🔗](#lib:operator%7c=,bitset) `constexpr bitset& operator|=(const bitset& rhs) noexcept; ` [3](#3) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L10660) *Effects*: Sets each bit in*this for which the corresponding bit in rhs is set, and leaves all other bits unchanged[.](#3.sentence-1) [4](#4) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L10666) *Returns*: *this[.](#4.sentence-1) [🔗](#lib:operator%5e=,bitset) `constexpr bitset& operator^=(const bitset& rhs) noexcept; ` [5](#5) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L10677) *Effects*: Toggles each bit in*this for which the corresponding bit in rhs is set, and leaves all other bits unchanged[.](#5.sentence-1) [6](#6) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L10683) *Returns*: *this[.](#6.sentence-1) [🔗](#lib:operator%3c%3c=,bitset) `constexpr bitset& operator<<=(size_t pos) noexcept; ` [7](#7) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L10694) *Effects*: Replaces each bit at position I in*this with a value determined as follows: - [(7.1)](#7.1) If I < pos, the new value is zero; - [(7.2)](#7.2) If I >= pos, the new value is the previous value of the bit at position I - pos[.](#7.sentence-1) [8](#8) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L10707) *Returns*: *this[.](#8.sentence-1) [🔗](#lib:operator%3e%3e=,bitset) `constexpr bitset& operator>>=(size_t pos) noexcept; ` [9](#9) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L10718) *Effects*: Replaces each bit at position I in*this with a value determined as follows: - [(9.1)](#9.1) If pos >= N - I, the new value is zero; - [(9.2)](#9.2) If pos < N - I, the new value is the previous value of the bit at position I + pos[.](#9.sentence-1) [10](#10) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L10730) *Returns*: *this[.](#10.sentence-1) [🔗](#lib:operator%3c%3c,bitset) `constexpr bitset operator<<(size_t pos) const noexcept; ` [11](#11) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L10741) *Returns*: bitset(*this) <<= pos[.](#11.sentence-1) [🔗](#lib:operator%3e%3e,bitset) `constexpr bitset operator>>(size_t pos) const noexcept; ` [12](#12) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L10752) *Returns*: bitset(*this) >>= pos[.](#12.sentence-1) [🔗](#lib:set_(member),bitset) `constexpr bitset& set() noexcept; ` [13](#13) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L10765) *Effects*: Sets all bits in*this[.](#13.sentence-1) [14](#14) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L10770) *Returns*: *this[.](#14.sentence-1) [🔗](#lib:set_(member),bitset_) `constexpr bitset& set(size_t pos, bool val = true); ` [15](#15) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L10783) *Effects*: Stores a new value in the bit at position pos in*this[.](#15.sentence-1) If val is true, the stored value is one, otherwise it is zero[.](#15.sentence-2) [16](#16) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L10789) *Returns*: *this[.](#16.sentence-1) [17](#17) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L10793) *Throws*: out_of_range if pos does not correspond to a valid bit position[.](#17.sentence-1) [🔗](#lib:reset,bitset) `constexpr bitset& reset() noexcept; ` [18](#18) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L10805) *Effects*: Resets all bits in*this[.](#18.sentence-1) [19](#19) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L10810) *Returns*: *this[.](#19.sentence-1) [🔗](#lib:reset,bitset_) `constexpr bitset& reset(size_t pos); ` [20](#20) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L10821) *Effects*: Resets the bit at position pos in*this[.](#20.sentence-1) [21](#21) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L10826) *Returns*: *this[.](#21.sentence-1) [22](#22) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L10830) *Throws*: out_of_range if pos does not correspond to a valid bit position[.](#22.sentence-1) [🔗](#lib:operator~,bitset) `constexpr bitset operator~() const noexcept; ` [23](#23) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L10842) *Effects*: Constructs an object x of classbitset and initializes it with*this[.](#23.sentence-1) [24](#24) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L10849) *Returns*: x.flip()[.](#24.sentence-1) [🔗](#lib:flip,bitset) `constexpr bitset& flip() noexcept; ` [25](#25) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L10860) *Effects*: Toggles all bits in*this[.](#25.sentence-1) [26](#26) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L10865) *Returns*: *this[.](#26.sentence-1) [🔗](#lib:flip,bitset_) `constexpr bitset& flip(size_t pos); ` [27](#27) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L10876) *Effects*: Toggles the bit at position pos in*this[.](#27.sentence-1) [28](#28) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L10881) *Returns*: *this[.](#28.sentence-1) [29](#29) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L10885) *Throws*: out_of_range if pos does not correspond to a valid bit position[.](#29.sentence-1) [🔗](#lib:operator%5b%5d,bitset) `constexpr bool operator[](size_t pos) const; ` [30](#30) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L10897) *Hardened preconditions*: pos < size() is true[.](#30.sentence-1) [31](#31) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L10901) *Returns*: true if the bit at position pos in *this has the value one, otherwise false[.](#31.sentence-1) [32](#32) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L10906) *Throws*: Nothing[.](#32.sentence-1) [🔗](#lib:operator%5b%5d,bitset_) `constexpr bitset::reference operator[](size_t pos); ` [33](#33) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L10917) *Hardened preconditions*: pos < size() is true[.](#33.sentence-1) [34](#34) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L10921) *Returns*: An object of typebitset​::​reference such that(*this)[pos] == this->test(pos), and such that(*this)[pos] = val is equivalent tothis->set(pos, val)[.](#34.sentence-1) [35](#35) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L10932) *Throws*: Nothing[.](#35.sentence-1) [36](#36) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L10936) *Remarks*: For the purpose of determining the presence of a data race ([[intro.multithread]](intro.multithread "6.10.2 Multi-threaded executions and data races")), any access or update through the resulting reference potentially accesses or modifies, respectively, the entire underlying bitset[.](#36.sentence-1) [🔗](#lib:to_ulong,bitset) `constexpr unsigned long to_ulong() const; ` [37](#37) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L10950) *Returns*: x[.](#37.sentence-1) [38](#38) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L10954) *Throws*: overflow_error if the integral value x corresponding to the bits in *this cannot be represented as type unsigned long[.](#38.sentence-1) [🔗](#lib:to_ullong,bitset) `constexpr unsigned long long to_ullong() const; ` [39](#39) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L10968) *Returns*: x[.](#39.sentence-1) [40](#40) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L10972) *Throws*: overflow_error if the integral value x corresponding to the bits in *this cannot be represented as type unsigned long long[.](#40.sentence-1) [🔗](#lib:to_string,bitset) `template, class Allocator = allocator> constexpr basic_string to_string(charT zero = charT('0'), charT one = charT('1')) const; ` [41](#41) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L10990) *Effects*: Constructs a string object of the appropriate type and initializes it to a string of length N characters[.](#41.sentence-1) Each character is determined by the value of its corresponding bit position in*this[.](#41.sentence-2) Character position N - 1 corresponds to bit position zero[.](#41.sentence-3) Subsequent decreasing character positions correspond to increasing bit positions[.](#41.sentence-4) Bit value zero becomes the character zero, bit value one becomes the characterone[.](#41.sentence-5) [42](#42) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L11003) *Returns*: The created object[.](#42.sentence-1) [🔗](#lib:count,bitset) `constexpr size_t count() const noexcept; ` [43](#43) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L11014) *Returns*: A count of the number of bits set in*this[.](#43.sentence-1) [🔗](#lib:size,bitset) `constexpr size_t size() const noexcept; ` [44](#44) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L11026) *Returns*: N[.](#44.sentence-1) [🔗](#lib:operator==,bitset) `constexpr bool operator==(const bitset& rhs) const noexcept; ` [45](#45) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L11037) *Returns*: true if the value of each bit in*this equals the value of the corresponding bit in rhs[.](#45.sentence-1) [🔗](#lib:test,bitset) `constexpr bool test(size_t pos) const; ` [46](#46) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L11050) *Returns*: true if the bit at position pos in*this has the value one[.](#46.sentence-1) [47](#47) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L11058) *Throws*: out_of_range if pos does not correspond to a valid bit position[.](#47.sentence-1) [🔗](#lib:all,bitset) `constexpr bool all() const noexcept; ` [48](#48) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L11070) *Returns*: count() == size()[.](#48.sentence-1) [🔗](#lib:any_(member),bitset) `constexpr bool any() const noexcept; ` [49](#49) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L11083) *Returns*: count() != 0[.](#49.sentence-1) [🔗](#lib:none,bitset) `constexpr bool none() const noexcept; ` [50](#50) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L11094) *Returns*: count() == 0[.](#50.sentence-1)