DOC HOME SITE MAP MAN PAGES GNU INFO SEARCH PRINT BOOK
 

bitset(3C++std)


bitset , operator>> , operator<< - defines a template class that administers sets of bits

Synopsis

  namespace std {
    template<size_t N>
       class bitset;

           // TEMPLATE FUNCTIONS
   template<class E, class T, size_t N>
       basic_istream<E, T>&
           operator>>(basic_istream<E, >& is,
               bitset<N>& x);
   template<class E, class T, size_t N>
       basic_ostream<E, T>&
           operator<<(basic_ostream<E, T>& os,
               const bitset<N>& x);
       };

Description

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

bitset


any , at , bitset , bitset_size , 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 N>
       class bitset {
   public:
       typedef bool element_type;
       class reference;
       bitset();
       bitset(unsigned long val);
       template<class E, class T, class A>
           explicit bitset(const basic_string<E, T, A>& str,
               typename basic_string<E, T, A>::size_type
                   pos = 0,
               typename basic_string<E, T, A>::size_type
                   n = basic_string<E, T, A>::npos);
       bitset<N>& operator&=(const bitset<N>& rhs);
       bitset<N>& operator|=(const bitset<N>& rhs);
       bitset<N>& operator^=(const bitset<N>& rhs);
       bitset<N>& operator<<=(const bitset<N>& pos);
       bitset<N>& operator>>=(const bitset<N>& pos);
       bitset<N>& set();
       bitset<N>& set(size_t pos, bool val = true);
       bitset<N>& reset();
       bitset<N>& reset(size_t pos);
       bitset<N>& flip();
       bitset<N>& flip(size_t pos);
       reference operator[](size_t pos);
       bool operator[](size_t pos) const;
       reference at(size_t pos);
       bool at(size_t pos) const;
       unsigned long to_ulong() const;
       template<class E, class T, class A>
           basic_string<E, T, A> to_string() const;
       size_t count() const;
       size_t size() const;
       bool operator==(const bitset<N>& rhs) const;
       bool operator!=(const bitset<N>& rhs) const;
       bool test(size_t pos) const;
       bool any() const;
       bool none() const;
       bitset<N> operator<<(size_t pos) const;
       bitset<N> operator>>(size_t pos) const;
       bitset<N> operator~();
       static const size_t bitset_size = N;
       };

The template class describes an object that stores a sequence of N 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<N> 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::at

   bool at(size_type pos) const;
   reference at(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 function throws an object of class out_of_range.

bitset::bitset

   bitset();
   bitset(unsigned long val);
   template<class E, class T, class A>
       explicit bitset(const basic_string<E, T, A>& str,
           typename basic_string<E, T, A>::size_type
               pos = 0,
           typename basic_string<E, T, A>::size_type
               n = basic_string<E, T, A>::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 n 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::bitset_size

   static const size_t bitset_size = N;

The const static member is initialized to the template parameter N.

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<N>& flip();
   bitset<N>& 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<N>& rhs) const;

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

bitset::operator&=

   bitset<N>& operator&=(const bitset<N>& rhs);

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 rhs. The function returns *this.

bitset::operator<<

   bitset<N> operator<<(const bitset<N>& pos);

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

bitset::operator<<=

   bitset<N>& operator<<=(const bitset<N>& 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<N>& rhs) const;

The member operator function returns true only if the bit sequence stored in *this is the same as the one stored in rhs.

bitset::operator>>

   bitset<N> operator>>(const bitset<N>& pos);

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

bitset::operator>>=

   bitset<N>& operator>>=(const bitset<N>& 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<N>& operator^=(const bitset<N>& rhs);

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 rhs. The function returns *this.

bitset::operator|=

   bitset<N>& operator|=(const bitset<N>& rhs);

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 rhs. The function returns *this.

bitset::operator~

   bitset<N> operator~();

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

bitset::reference

   class reference {
   public:
       reference& operator=(bool b};
       reference& operator=(const reference& x);
       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 b an object of type bool, x and y objects of type bitset<N>, and i and j valid positions within such an object, the member functions of class reference ensure that (in order):

bitset::reset

   bitset<N>& reset();
   bitset<N>& 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<N>& set();
   bitset<N>& 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 N.

bitset::test

   bool test(size_t pos, bool val = true);

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 E, class T, class A>
       basic_string<E, T, A> to_string() const;

The member function constructs str, an object of class basic_string<E, T, A>. 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<class E, class T, size_t N>
       basic_ostream<E, T>&
           operator<<(basic_ostream<E, T>& os,
               const bitset<N>& x);

The template function overloads operator<< to insert a text representation of the bit sequence in os. It effectively executes os << x.to_string<E, T, allocator<E> >(), then returns os.

operator>>

   template<class E, class T, size_t N>
       basic_istream<E, T>&
           operator>>(basic_istream<E, T>& is,
               bitset<N>& x);

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

If the function stores no characters in str, it calls is.setstate(ios_base::failbit). In any case, it returns is.

References

ios(3C++std) , stdexcept(3C++std) , string(3C++std)
18 February 2000
© 2000 The Santa Cruz Operation, Inc. All rights reserved.

Copyright © 1992-1996 by P.J. Plauger. Portions derived from work copyright © 1994 by Hewlett-Packard Company. All rights reserved.