cplusplus.com cplusplus.com
cplusplus.com   C++ : Reference : STL Containers : bitset
- -
C++
Information
Documentation
Reference
Articles
Sourcecode
Forum
Reference
C Library
IOstream Library
Strings library
STL Containers
STL Algorithms
STL Containers
bitset
deque
list
map
multimap
multiset
priority_queue
queue
set
stack
vector
bitset
bitset::bitset
bitset operators
member functions:
· bitset::any
· bitset::count
· bitset::flip
· bitset::none
· bitset::operator[]
· bitset::reset
· bitset::set
· bitset::size
· bitset::test
· bitset::to_string
· bitset::to_ulong

-

bitset class template
<bitset>

Bitset

A bitset is a special container class that is designed to store bits (elements with only two possible values: 0 or 1, true or false, ...).

The class is very similar to a regular array, but optimizing for space allocation: each element occupies only one bit (which is eight times less than the smallest elemental type in C++: char).

Each element (each bit) can be accessed individually: for example, for a given bitset named mybitset, the expression mybitset[3] accesses its fourth bit, just like a regular array accesses its elements.

Because no such small elemental type exists in most C++ environments, the individual elements are accessed as special references which mimic bool elements:

class bitset::reference {
  friend class bitset;
  reference();                                 // no public constructor
public:
  ~reference();
  operator bool () const;                      // convert to bool
  reference& operator= ( bool x );             // assign from bool
  reference& operator= ( const reference& x ); // assign from bit
  reference& flip();                           // flip bit value
  bool operator~() const;                      // return inverse value
}

Apart from overriding several operators and to provide direct access to the bits, bitsets have the feature of being able to be constructed from and converted to both integer values and binary strings (see constructor, bitset::to_ulong and bitset::to_string). They can also be directly inserted and extracted from streams in binary format.

Bitsets have a fixed size. For a similar container class that also optimizes for space allocation and allows for dynamic resizing, see the bool specialization of vector (vector<bool>).

Member functions

(constructor) Construct bitset (public member function)
applicable operators Bitset operators (functions)

Bit access:

operator[] Access bit (public member function)

Bit operations:

set Set bits (public member function)
reset Reset bits (public member function)
flip Flip bits (public member function)

Bitset operations:

to_ulong Convert to unsigned long integer (public member function)
to_string Convert to string (public member function)
count Count bits set (public member function)
size Return size (public member function)
test Return bit value (public member function)
any Test if any bit is set (public member function)
none Test if no bit is set (public member function)

© The C++ Resources Network, 2000-2007 - All rights reserved
Spotted an error? - contact us