[Previous] [Contents] [Next]

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

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

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

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() 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 1 if the bit is set, otherwise 0. 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> >(), 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-2002 by P.J. Plauger. All rights reserved.

[Previous] [Contents] [Next]