The bitset abstraction, Include files, Declaration and initialization of bitset – HP Integrity NonStop H-Series User Manual
Page 93: Accessing and testing elements
![background image](/manuals/396950/93/background.png)
Click on the banner to return to the user guide home page.
©Copyright 1996 Rogue Wave Software
The bitset Abstraction
A bitset is really a cross between a
set
and a
vector
. Like the vector abstraction vector
abstraction represents a set of binary (0/1 bit) values. However, set operations can be performed
on bitsets using the logical bit-wise operators. The class
bitset
does not provide any iterators for
accessing elements.
Include Files
#include
Declaration and Initialization of bitset
A
bitset
is a template class abstraction. The template argument is not, however, a type, but an
integer value. The value represents the number of bits the set will contains.
bitset<126> bset_one; // create a set of 126 bits
An alternative technique permits the size of the set to be specified as an argument to the
constructor. The actual size will be the smaller of the value used as the template argument and
the constructor argument. This technique is useful when a program contains two or more bit
vectors of differing sizes. Consistently using the larger size for the template argument means that
only one set of methods for the class will be generated. The actual size, however, will be
determined by the constructor.
bitset<126> bset_two(100); // this set has only 100 elements
A third form of constructor takes as argument a string of 0 and 1 characters. A bitset is created
that has as many elements as are characters in the string, and is initialized with the values from
the string.
bitset<126> small_set("10101010"); // this set has 8 elements
Accessing and Testing Elements
An individual bit in the bitset can be accessed using the subscript operation. Whether the bit is
one or not can be determined using the member function test(). Whether any bit in the bitset is
"on" is tested using the member function any(), which yields a boolean value. The inverse of
any() is returned by the member function none().