<bitset>


bitset · hash · operator& · operator| · operator^ · operator>> · operator<<


Include the standard header <bitset> to define the template class bitset and two supporting templates.

Beginning with C++11, some functions and constructors declared in this header use constexpr to signal that they are treated as compile-time constants.

namespace std {
template<size_t Bits>
    class bitset;

template<size_t Bits>
    struct hash<bitset<Bits> >  [added with C++11]
        : public unary_function<bitset<Bits>, size_t> {
    size_t operator()(bitset<Bits> val) const;
    };

        // TEMPLATE FUNCTIONS
template<size_t Bits>
    bitset<Bits>
        operator&(const bitset& left,
            const bitset& right) noexcept;
template<size_t Bits>
    bitset<Bits>
        operator|(const bitset& left,
            const bitset& right) noexcept;
template<size_t Bits>
    bitset<Bits>
        operator^(const bitset& left,
            const bitset& right) noexcept;

template<class Elem, class Tr, size_t Bits>
    basic_istream<Elem, Tr>&
        operator>>(basic_istream<Elem, >& istr,
            bitset<Bits>& right);
template<class Elem, class Tr, size_t Bits>
    basic_ostream<Elem, Tr>&
        operator<<(basic_ostream<Elem, Tr>& ostr,
            const bitset<Bits>& right);
}  // namespace std

bitset


all · 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_ullong · to_ulong


template<size_t Bits>
    class bitset {
public:
    typedef bool element_type;
    class reference;

    constexpr bitset() noexcept;
    constexpr bitset(unsigned long val); [replaced with C++11]
    constexpr bitset(unsigned long long val) noexcept; [added with C++11]
    bitset(const char *ptr); [added with C++11]
    template<class Elem, class Tr, class Alloc>
        explicit bitset(const basic_string<Elem, Tr, Alloc>& str,
            typename basic_string<Elem, Tr, Alloc>::size_type
                pos = 0,
            typename basic_string<Elem, Tr, Alloc>::size_type
                count = basic_string<Elem, Tr, Alloc>::npos,
            Elem digit0 = Elem('0'),
            Elem digit1 = Elem('1'));
    template<class Elem>
        explicit bitset(const Elem *ptr,
            typename basic_string<Elem>::size_type
                pos = 0,
            typename basic_string<Elem>::size_type
                count = basic_string<Elem>::npos,
            Elem digit0 = Elem('0'),
            Elem digit1 = Elem('1'));

    bitset<Bits>& operator&=(const bitset<Bits>& right) noexcept;
    bitset<Bits>& operator|=(const bitset<Bits>& right) noexcept;
    bitset<Bits>& operator^=(const bitset<Bits>& right noexcept);
    bitset<Bits>& operator<<=(size_t pos) noexcept;
    bitset<Bits>& operator>>=(size_t pos) noexcept;

    bitset<Bits>& set() noexcept;
    bitset<Bits>& set(size_t pos, bool val = true);
    bitset<Bits>& reset() noexcept;
    bitset<Bits>& reset(size_t pos);
    bitset<Bits>& flip() noexcept;
    bitset<Bits>& flip(size_t pos);

    bool operator[](size_t pos) const;
    reference operator[](size_t pos);
    unsigned long to_ulong() const;
    unsigned long long to_ullong() const; [added with C++11]
    template<class Elem = char,
        class Tr = char_traits<Elem>,
        class Alloc = allocator<Elem> >
        basic_string<Elem, Tr, Alloc>
            to_string(Elem digit0 = Elem('0'), Elem digit1 = Elem('1')) const;

    size_t count() const noexcept;
    contexpr size_t size() const noexcept;

    bool operator==(const bitset<Bits>& right) const noexcept;
    bool operator!=(const bitset<Bits>& right) const noexcept;
    bool test(size_t pos) const;
    bool any() const noexcept;
    bool none() const noexcept;
    bool all() const noexcept; [added with C++11]

    bitset<Bits> operator<<(size_t pos) const noexcept;
    bitset<Bits> operator>>(size_t pos) const noexcept;
    bitset<Bits> operator~() const noexcept;
    };

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<Bits> 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::all

bool all() const noexcept; [added with C++11]

The member function returns true if all bits are set in the bit sequence.

bitset::any

bool any() const noexcept;

The member function returns true if any bit is set in the bit sequence.

bitset::bitset

constexpr bitset() noexcept;
constexpr bitset(unsigned long val); [replaced with C++11]
constexpr bitset(unsigned long long val) noexcept; [added with C++11]
bitset(const char *ptr); [added with C++11]
template<class Elem, class Tr, class Alloc>
    explicit bitset(const basic_string<Elem, Tr, Alloc>& str,
        typename basic_string<Elem, Tr, Alloc>::size_type
            pos = 0,
        typename basic_string<Elem, Tr, Alloc>::size_type
            count = basic_string<Elem, Tr, Alloc>::npos,
        Elem digit0 = Elem('0'),
        Elem digit1 = Elem('1'));
template<class Elem> explicit bitset(const Elem *ptr, typename basic_string<Elem>::size_type pos = 0, typename basic_string<Elem>::size_type count = basic_string<Elem>::npos, Elem digit0 = Elem('0'), Elem digit1 = Elem('1'));

The first constructor resets all bits in the bit sequence. The second or third constructor sets only those bits at position J for which val & 1 << J is nonzero.

The fourth 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 digit1, 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 digit1.

The fifth constructor is effectively the same as:

bitset(count == basic_string<Elem>::npos
    ? basic_string<Elem>(ptr)
    : basic_string<Elem>ptr, count),
	0, count, digit0, digit1)

bitset::count

size_t count() const noexcept;

The member function returns the number of bits set in the bit sequence.

bitset::element_type

typedef bool element_type;

The type is a synonym for bool.

bitset::flip

bitset<Bits>& flip() noexcept;
bitset<Bits>& flip(size_t pos);

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 noexcept;

The member function returns true if none of the bits are set in the bit sequence.

bitset::operator!=

bool operator !=(const bitset<Bits>& right) const noexcept;

The member operator function returns true only if the bit sequence stored in *this differs from the one stored in right.

bitset::operator&=

bitset<Bits>& operator&=(const bitset<Bits>& right) noexcept;

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<Bits> operator<<(size_t pos) noexcept;

The member operator function returns bitset(*this) <<= pos.

bitset::operator<<=

bitset<Bits>& operator<<=(size_t pos) noexcept;

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<Bits>& right) const noexcept;

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<Bits> operator>>(size_t pos) noexcept;

The member operator function returns bitset(*this) >>= pos.

bitset::operator>>=

bitset<Bits>& operator>>=(size_t pos) noexcept;

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);

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.

The member function throws nothing.

bitset::operator^=

bitset<Bits>& operator^=(const bitset<Bits>& right) noexcept;

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<Bits>& operator|=(const bitset<Bits>& right) noexcept;

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<Bits> operator~() const noexcept;

The member operator function returns bitset(*this).flip().

bitset::reference

class reference {
    reference() noexcept;
public:
    ~reference() noexcept;
    reference& operator=(bool val) noexcept;
    reference& operator=(const reference& bitref) noexcept;
    bool operator~() const noexcept;
    operator bool() const noexcept;
    reference& flip() noexcept;
    };

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<Bits>, and I and J valid positions within such an object, the member functions of class reference ensure that (in order):

bitset::reset

bitset<Bits>& reset() noexcept;
bitset<Bits>& reset(size_t pos);

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<Bits>& set() noexcept;
bitset<Bits>& set(size_t pos, bool val = true);

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

constexpr size_t size() const noexcept;

The member function returns Bits.

bitset::test

bool test(size_t pos);

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<class Elem = char,
    class Tr = char_traits<Elem>,
    class Alloc = allocator<Elem> >
    basic_string<Elem, Tr, Alloc>
        to_string(Elem digit0 = Elem('0'), Elem digit1 = Elem('1')) const;

The member function constructs str, an object of class basic_string<Elem, Tr, Alloc>. For each bit in the bit sequence, the function appends digit1 if the bit is set, otherwise digit0. The last element appended to str corresponds to bit position zero. The function returns str.

bitset::to_ullong

unsigned long long to_ullong() const; [added with C++11]

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 long. Otherwise, it returns the sum of the bit values in the bit sequence.

bitset::to_ulong

unsigned long to_ulong() const;

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.

hash

template<size_t Bits>
    struct hash<bitset<Bits> > [added with C++11]
        : public unary_function<bitset<Bits>, size_t> {
    size_t operator()(bitset<Bits> val) const;
    };

The template class defines its member function as returning a value uniquely determined by val. The member function defines a hash function, suitable for mapping values of type bitset<Bits> to a distribution of index values.

operator&

template<size_t Bits>
    bitset<Bits>
        operator&(const bitset& left,
            const bitset& right) noexcept;

The template function returns (temp = left) &= right, where temp has type bitset<Bits>.

operator|

template<size_t Bits>
    bitset<Bits>
        operator|(const bitset& left,
            const bitset& right)noexcept;

The template function returns (temp = left) |= right, where temp has type bitset<Bits>.

operator^

template<size_t Bits>
    bitset<Bits>
        operator^(const bitset& left,
            const bitset& right) noexcept;

The template function returns (temp = left) ^= right, where temp has type bitset<Bits>.

operator<<

template<class Elem, class Tr, size_t Bits>
    basic_ostream<Elem, Tr>&
        operator<<(basic_ostream<Elem, Tr>& ostr,
            const bitset<Bits>& right);

The template function overloads operator<< to insert a text representation of the bit sequence in ostr. It effectively executes ostr << right.to_string<Elem, Tr, allocator<Elem> >( use_facet< ctype<Elem> >( ostr.getloc()). widen('0'), ostr.getloc()).widen('1')), then returns ostr.

operator>>

template<class Elem, class Tr, size_t Bits>
    basic_istream<Elem, Tr>&
        operator>>(basic_istream<Elem, Tr>& istr,
            bitset<Bits>& right);

The template function overloads operator>> to store in right the value bitset(str), where str is an object of type basic_string<Elem, Tr, allocator<Elem> >& extracted from istr. The function extracts elements and appends them to str until:

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 and the Index.

Copyright © 1992-2013 by P.J. Plauger. All rights reserved.