# <bitset> <bitset> # `` * * * **`bitset· operator&· operator|· operator^· operator>>· operator<<`** Include the standard header **``** to define the template class `bitset` and two supporting templates. namespace std { template class bitset; // TEMPLATE FUNCTIONS template bitset operator&(const bitset& left, const bitset& right); template bitset operator|(const bitset& left, const bitset& right); template bitset operator^(const bitset& left, const bitset& right); template basic_istream& operator>>(basic_istream& istr, bitset& right); template basic_ostream& operator<<(basic_ostream& ostr, const bitset& right); } // namespace stdCopy to clipboard ## `bitset` * * * **`any· bitset· count· element_type· flip· none· operator!=· operator&=· operator<<· operator<<=· operator==· operator>>· operator>>=· operator[]· operator^=· operator|=· operator~· reference· reset· set· size· test· to_string· to_ulong`** * * * template class bitset { public: typedef bool element_type; class reference; bitset(); bitset(unsigned long val); template explicit bitset(const basic_string& str, typename basic_string::size_type pos = 0, typename basic_string::size_type count = basic_string::npos, Elem digit0 = Elem('0')); bitset& operator&=(const bitset& right); bitset& operator|=(const bitset& right); bitset& operator^=(const bitset& right); bitset& operator<<=(size_t pos); bitset& operator>>=(size_t pos); bitset& set(); bitset& set(size_t pos, bool val = true); bitset& reset(); bitset& reset(size_t pos); bitset& flip(); bitset& flip(size_t pos); reference operator[](size_t pos); bool operator[](size_t pos) const; unsigned long to_ulong() const; template basic_string to_string(Elem digit0 = Elem('0')) const; size_t count() const; size_t size() const; bool operator==(const bitset& right) const; bool operator!=(const bitset& right) const; bool test(size_t pos) const; bool any() const; bool none() const; bitset operator<<(size_t pos) const; bitset operator>>(size_t pos) const; bitset operator~() const; };Copy to clipboard The template class describes an object that stores a sequence of `Bits` bits. A bit is **set** if its value is 1, **reset** if its value is 0. To **flip** a bit is to change its value from 1 to 0 or from 0 to 1. When converting between an object of class `bitset` and an object of some integral type, bit position `J` corresponds to the bit value `1 << J`. The integral value corresponding to two or more bits is the sum of their bit values. ### `bitset::any` bool any() const;Copy to clipboard The member function returns true if any bit is set in the bit sequence. ### `bitset::bitset` bitset(); bitset(unsigned long val); template explicit bitset(const basic_string& str, typename basic_string::size_type pos = 0, typename basic_string::size_type count = basic_string::npos, Elem digit0 = Elem('0'));Copy to clipboard The first constructor resets all bits in the bit sequence. The second constructor sets only those bits at position `J` for which `val & 1 << J` is nonzero. The third constructor determines the initial bit values from elements of a string determined from `str`. If `str.size() < pos`, the constructor throws an object of class `out_of_range`. Otherwise, the effective length of the string `rlen` is the smaller of `count` and `str.size() - pos`. If any of the `rlen` elements beginning at position `pos` is other than `digit0` or `digit0 + 1`, the constructor throws an object of class `invalid_argument`. Otherwise, the constructor sets only those bits at position `J` for which the element at position `pos + J` is `digit0 + 1`. ### `bitset::count` size_t count() const;Copy to clipboard The member function returns the number of bits set in the bit sequence. ### `bitset::element_type` typedef bool element_type;Copy to clipboard The type is a synonym for `bool`. ### `bitset::flip` bitset& flip(); bitset& flip(size_t pos);Copy to clipboard The first member function flips all bits in the bit sequence, then returns `*this`. The second member function throws `out_of_range` if `size() <= pos`. Otherwise, it flips the bit at position `pos`, then returns `*this`. ### `bitset::none` bool none() const;Copy to clipboard The member function returns true if none of the bits are set in the bit sequence. ### `bitset::operator!=` bool operator !=(const bitset& right) const;Copy to clipboard The member operator function returns true only if the bit sequence stored in `*this` differs from the one stored in `right`. ### `bitset::operator&=` bitset& operator&=(const bitset& right);Copy to clipboard The member operator function replaces each element of the bit sequence stored in `*this` with the logical AND of its previous value and the corresponding bit in `right`. The function returns `*this`. ### `bitset::operator<<` bitset operator<& operator<<=(size_t pos);Copy to clipboard The member operator function replaces each element of the bit sequence stored in `*this` with the element `pos` positions earlier in the sequence. If no such earlier element exists, the function clears the bit. The function returns `*this`. ### `bitset::operator==` bool operator ==(const bitset& right) const;Copy to clipboard The member operator function returns true only if the bit sequence stored in `*this` is the same as the one stored in `right`. ### `bitset::operator>>` bitset operator>>(size_t pos);Copy to clipboard The member operator function returns `bitset(*this) >>= pos`. ### `bitset::operator>>=` bitset& operator>>=(size_t pos);Copy to clipboard The member function replaces each element of the bit sequence stored in `*this` with the element `pos` positions later in the sequence. If no such later element exists, the function clears the bit. The function returns `*this`. ### `bitset::operator[]` bool operator[](size_type pos) const; reference operator[](size_type pos);Copy to clipboard The member function returns an object of class `reference`, which designates the bit at position `pos`, if the object can be modified. Otherwise, it returns the value of the bit at position `pos` in the bit sequence. If that position is invalid, the behavior is undefined. ### `bitset::operator^=` bitset& operator^=(const bitset& right);Copy to clipboard The member operator function replaces each element of the bit sequence stored in `*this` with the logical EXCLUSIVE OR of its previous value and the corresponding bit in `right`. The function returns `*this`. ### `bitset::operator|=` bitset& operator|=(const bitset& right);Copy to clipboard The member operator function replaces each element of the bit sequence stored in `*this` with the logical OR of its previous value and the corresponding bit in `right`. The function returns `*this`. ### `bitset::operator~` bitset operator~() const;Copy to clipboard The member operator function returns `bitset(*this).flip()`. ### `bitset::reference` class reference { public: reference& operator=(bool val}; reference& operator=(const reference& bitref); bool operator~() const; operator bool() const; reference& flip(); };Copy to clipboard The member class describes an object that designates an individual bit within the bit sequence. Thus, for `val` an object of type `bool`, `bs` and `bs2` objects of type `bitset`, and `I` and `J` valid positions within such an object, the member functions of class `reference` ensure that (in order): - **`bs[I] = val`** stores `val` at bit position `I` in `bs` - **`bs[I] = bs2[J]`** stores the value of the bit `bs2[J]` at bit position `I` in `bs` - **`val = ~bs[I]`** stores the flipped value of the bit `bs[I]` in `val` - **`val = bs[I]`** stores the value of the bit `bs[I]` in `val` - **`bs[I].flip()`** stores the flipped value of the bit `bs[I]` back at bit position `I` in `bs` ### `bitset::reset` bitset& reset(); bitset& reset(size_t pos);Copy to clipboard The first member function resets (or clears) all bits in the bit sequence, then returns `*this`. The second member function throws `out_of_range` if `size() <= pos`. Otherwise, it resets the bit at position `pos`, then returns `*this`. ### `bitset::set` bitset& set(); bitset& set(size_t pos, bool val = true);Copy to clipboard The first member function sets all bits in the bit sequence, then returns `*this`. The second member function throws `out_of_range` if `size() <= pos`. Otherwise, it stores `val` in the bit at position `pos`, then returns `*this`. ### `bitset::size` size_t size() const;Copy to clipboard The member function returns `Bits`. ### `bitset::test` bool test(size_t pos);Copy to clipboard The member function throws `out_of_range` if `size() <= pos`. Otherwise, it returns true only if the bit at position `pos` is set. ### `bitset::to_string` template basic_string to_string(Elem digit0 = Elem('0')) const;Copy to clipboard The member function constructs `str`, an object of class `basic_string`. For each bit in the bit sequence, the function appends `digit0 + 1` if the bit is set, otherwise `digit0`. The *last* element appended to `str` corresponds to bit position zero. The function returns `str`. ### `bitset::to_ulong` unsigned long to_ulong() const;Copy to clipboard The member function throws `overflow_error` if any bit in the bit sequence has a bit value that cannot be represented as a value of type *unsigned long.* Otherwise, it returns the sum of the bit values in the bit sequence. ## `operator&` template bitset operator&(const bitset& left, const bitset& right);Copy to clipboard The template function returns `(temp = left) &= right`, where `temp` has type `bitset`. ## `operator|` template bitset operator|(const bitset& left, const bitset& right);Copy to clipboard The template function returns `(temp = left) |= right`, where `temp` has type `bitset`. ## `operator^` template bitset operator^(const bitset& left, const bitset& right);Copy to clipboard The template function returns `(temp = left) ^= right`, where `temp` has type `bitset`. ## `operator<<` template basic_ostream& operator<<(basic_ostream& ostr, const bitset& right);Copy to clipboard The template function overloads `operator<<` to insert a text representation of the bit sequence in `ostr`. It effectively executes `ostr << right.to_string >( use_facet< ctype >( ostr.getloc()). widen('0'))`, then returns `ostr`. ## `operator>>` template basic_istream& operator>>(basic_istream& istr, bitset& right);Copy to clipboard The template function overloads `operator>>` to store in `right` the value `bitset(str)`, where `str` is an object of type `basic_string >&` extracted from `istr`. The function extracts elements and appends them to `str` until: - `Bits` elements have been extracted and stored - end-of-file occurs on the input sequence - the next input element is neither `digit0` nor `digit0 + 1`, where `digit0` is `use_facet< ctype >( istr.getloc()). widen('0')`, in which case the input element is not extracted If the function stores no characters in `str`, it calls `istr.setstate(ios_base::failbit)`. In any case, it returns `istr`. * * * See also the **[Table of Contents](https://docs.qualcomm.com/doc/80-N3040-23/topic/cpp_index.html#Table%20of%20Contents)** and the **[Index](https://docs.qualcomm.com/doc/80-N3040-23/topic/_cpp_index.html)**. *[Copyright](https://docs.qualcomm.com/doc/80-N3040-23/topic/cpp_crit_pjp.html) © 1992-2006by P.J. Plauger. All rights reserved.* Last Published: Oct 06, 2026 Previous Topic Next Topic