<bitset>

<bitset>


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

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

namespace std {
template<size_t Bits>
    class bitset;

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

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


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<size_t Bits>
    class bitset {
public:
    typedef bool element_type;
    class reference;
    bitset();
    bitset(unsigned long val);
    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'));
    bitset<Bits>& operator&=(const bitset<Bits>& right);
    bitset<Bits>& operator|=(const bitset<Bits>& right);
    bitset<Bits>& operator^=(const bitset<Bits>& right);
    bitset<Bits>& operator<<=(const bitset<Bits>& pos);
    bitset<Bits>& operator>>=(const bitset<Bits>& pos);
    bitset<Bits>& set();
    bitset<Bits>& set(size_t pos, bool val = true);
    bitset<Bits>& reset();
    bitset<Bits>& reset(size_t pos);
    bitset<Bits>& flip();
    bitset<Bits>& flip(size_t pos);
    reference operator[](size_t pos);
    bool operator[](size_t pos) const;
    unsigned long to_ulong() const;
    template<class Elem, class Tr, class Alloc>
        basic_string<Elem, Tr, Alloc>
            to_string(Elem digit0 = Elem('0')) const;
    size_t count() const;
    size_t size() const;
    bool operator==(const bitset<Bits>& right) const;
    bool operator!=(const bitset<Bits>& right) const;
    bool test(size_t pos) const;
    bool any() const;
    bool none() const;
    bitset<Bits> operator<<(size_t pos) const;
    bitset<Bits> operator>>(size_t pos) const;
    bitset<Bits> operator~() const;
    };

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::any

bool any() const;

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

bitset::bitset

bitset();
bitset(unsigned long val);
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'));

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;

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

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;

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

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<<(const bitset<Bits>& pos);

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

bitset::operator<<=

bitset<Bits>& operator<<=(const bitset<Bits>& pos);

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;

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>>(const bitset<Bits>& pos);

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

bitset::operator>>=

bitset<Bits>& operator>>=(const bitset<Bits>& pos);

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.

bitset::operator^=

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

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

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;

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

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

  • 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<Bits>& reset();
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();
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

size_t size() const;

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, class Tr, class Alloc>
    basic_string<Elem, Tr, Alloc>
        to_string(Elem digit0 = Elem('0')) 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 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;

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<size_t Bits>
    bitset<Bits>
        operator&(const bitset& left,
            const bitset& right);

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

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

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')), 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:

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

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

Last modified: 2013-12-21



Got questions about leaving a comment? Get answers from our Disqus FAQ.

comments powered by Disqus