Some programs handle an ordered set of bits, and each bit may contain a value of 0 (off) or 1 (on). A bit is a concise way to hold yes/no information (sometimes called a flag) for a set of items or conditions. The standard library provides a Bitset class that makes it easier to work with bit collections. To use the Bitset class, you must include the associated header file. In the example provided in this book, it is assumed that the using declaration of the Std::bitset is used:
#i nclude <bitset>
Using Std::bitset;
Definition and initialization of 3.5.1 Bitset
Table 3-6 Lists the constructor functions for the Bitset. Like vectors, theBitset class is a class template, and unlike a vector, a bitset type object differs only in its length, not its type. When defining a bitset, it is necessary to specify how many bits the Bitset contains, and to give its length value within the angle brackets:
bitset<32> Bitvec;//32-bit, all 0.
The given length value must be a constant expression (section 2.7). As given here, the length value must be defined as an integer literal constant or as a const object of an integer type that has been initialized with a constant value .
This statement defines Bitvec as a Bitset object that contains 32 bits . like the elements of vectors, thebits in Bitset are not named, and programmers can only access them by location. The position number of the bit collection starts at 0, so theBitvec's bit order is from 0 to 31. The bit string starting with 0 bits is the low order bit (low-order bit), and the bit string ending with 31 bits is the higher order bit (high-order bit).
Table 3-6 methods for initializing Bitset objects
Bitset<n> b; |
B has n bits, each of which is 0 |
Bitset<n> b(u); |
B is a copy of the unsigned long type U |
Bitset<n> b(s); |
B is a copy of the bit string contained in the string object s |
Bitset<n> b(s, pos, n); |
B is a copy of n bits starting from position Pos in S |
1. Initialize the bitset object with the unsigned value
When the unsigned Long value is used as the initial value of the Bitset object, the value is converted into binary bit mode. Instead, a bit set in the Bitset object is used as a copy of this bit pattern. If the bitset type is longer than the bits number of unsigned long, the remaining high-order position is 0, and if the bitet type is less than unsigned of the bits Long value, use only the The low-order bits in the unsigned value, and higher-order bits that exceed the Bitet type length are discarded.
on a 32-bit unsigned long machine, a hexadecimal value of 0xffff means that bits is 16 1 and 16 0 (each 0xf can be represented as 1111). The Bitset object can be initialized with 0xFFFF:
bitvec1 is smaller than the initializer
Bitset<16> BITVEC1 (0XFFFF); bits 0 ... Set to 1
bitvec2 same size as initializer
Bitset<32> bitvec2 (0XFFFF); bits 0 ... Set to 1; 16 ... 0
On a 32-bit machine, bits 0 through initialized from 0xffff
Bitset<128> BITVEC3 (0XFFFF); bits through 127 initialized to zero
In the above three examples,0 to 15 bits are set to 1. Because the bitvec1 bit is less than the number of bits of unsigned long, the higher-order bits of the BITVEC1 's initial value are discarded. bitvec2 and unsigned long are the same length, so all bits just place the initial value. The length of the BITVEC3 is greater than 31, andthe higher-order bit is set to 0.
2. Initializing a Bitset object with a string object
When the Bitset object is initialized with a string object , thestring object is directly represented as a bit pattern. The order in which a bit set is read from a string object is right-to-left:
String Strval ("1100");
Bitset<32> BITVEC4 (Strval);
In the BITVEC4 bit mode , positions 2 and 3 are 1 and the rest are 0. If the number of characters of a string object is less than the length of the Bitset type, the higher order bit is set to 0.
The string object and the Bitset object are reversed: therightmost character of the string object (the one with the largest subscript) is used to initialize the low-order bit of the Bitset object (that is, the bit with the subscript 0). It is important to remember this difference when initializing a Bitset object with a string object.
The entire string object is not necessarily the initial value of the Bitset object. Instead, you can use only one substring as the initial value:
String str ("1111111000000011001101");
Bitset<32> bitvec5 (str, 5, 4); 4 bits starting at Str[5], 1100
Bitset<32> bitvec6 (str, str.size ()-4); Use last 4 characters
This initializes the bitvec5 with a substring of four characters starting with str[5] in str . As usual, the Bitset object is initialized from the rightmost ending character of the substring, the binaryposition of BITVEC5 from 0 to 3 is 1100, and the other bits are set to 0. Omitting the third argument means taking all characters from the start position to the end of the string. In this example, the four bits at the end of STR are taken out to initialize the low four bits of the bitvec6. BITVEC6 The remaining bits are initialized to 0. The diagram of these initialization processes is as follows:
A variety of bitset operations (table 3-7) are used to test or set single or multiple bits in a Bitset object:
Table 3-7 bitset Operations
B.any () |
Is there a bits in B that is set to 1? |
b. None() |
There is no bits in B that is set to 1? |
b. Count() |
number of Bits with 1 in B |
b. Size() |
Number of bits in B |
b[pos] |
Access the bits at pos in B |
b. Test(pos) |
B is the bits at POS at 1? |
b. Set() |
Place all bits in B to 1 |
b. Set(pos) |
Put the binary position in B at pos at 1 |
b. Reset() |
Place all bits in B to 0 |
b. Reset(pos) |
Put the binary position in B at Pos at 0 |
b. Flip() |
Take all the bits in B counter- |
b. Flip(pos) |
Reverse the bits in the POS at the point B |
b. To_ulong() |
returns a unsigned Long value with the same bits in B |
OS << b |
output the bit set in B to the OS stream |
1. Test the entire bitset object
If one or more binary positions in the Bitset object are 1, the Any operation returns True, that is, its return value equals 1,and conversely, if the bits in the Bitset object are all 0, the none operation returns true.
Bitset<32> Bitvec; + bits, all zero
BOOL Is_set = Bitvec.any (); false, all bits is zero
BOOL Is_not_set = Bitvec.none (); true, all bits is zero
If you need to know the number of bits that is set to 1, you can use the count operation, which returns the number of bits that are set to 1:
size_t Bits_set = Bitvec.count (); returns number of bits that is on
The return type of the count operation is the type named size_t in the standard library. The size_t type is defined in the cstddef header file, which is the C + + version of the header file stddef.h of the C standard library. It is a machine-related unsigned type, and the size guarantees that the objects in memory will be stored.
As with the size operation in the vector and string, the bitset size operation returns the number of bits in the Bitset object, the type of the return value is size_ T:
size_t sz = bitvec.size (); returns
2. Accessing bits in a bitset object
You can use the subscript operator to read or write the bits of an index position, or you can use the subscript operator to test the value of a given bits or set a value for a bits:
Assign 1 to even numbered bits
for (int index = 0; Index! =); index + = 2)
Bitvec[index] = 1;
The above loop Bitvec the even bottom of the cursor to 1.
In addition to using the subscript operator, you can test or set the value of a given bits with the set, test, and reset operations:
equivalent loop using Set operation
for (int index = 0; Index! =); index + = 2)
Bitvec.set (index);
To test whether a bits is 1, you can use test to manipulate or test the return value of the subscript operator:
if (Bitvec.test (i))
Bitvec[i] is on
equivalent test using subscript
if (Bitvec[i])
Bitvec[i] is on
If the subscript operator tests a bits of 1, the result of the returned test value is true, otherwise false is returned .
3. Set the entire bitset object
The set and reset operations are used for all bits of the entire Bitset object, respectively, with 1 and 0 as a whole:
Bitvec.reset (); Set all of the bits to 0.
Bitvec.set (); set all the bits to 1
The flip action can reverse the bitwise of all or individual bits of the Bitset object:
Bitvec.flip (0); reverses value of first bit
Bitvec[0].flip (); also reverses the first bit
Bitvec.flip (); reverses value of all bits
4. Get The value of the Bitset object
The To_ulong operation returns a unsigned Long value that is the same as the Bitset object's bit-mode store value. You can use the To_ulong operation only if the length of the Bitset type is less than or equal to the length of unsigned long :
unsigned long ulong = Bitvec3.to_ulong ();
cout << "ulong =" << ulong << Endl;
The To_ulong operation is mainly used to move the Bitset object to a C-style or standard C+ + style before the program. If the Bitset object contains more bits than the length of the unsigned long, a run-time exception is generated. This book describes the exception (exception) in section 6.13and discusses it in detail in section 17.1.
5. Output bits
You can output the bit patterns in the Bitset object with the output operator:
Bitset<32> bitvec2 (0XFFFF); bits 0 ... Set to 1; 16 ... 0
cout << "BITVEC2:" << bitvec2 << Endl;
The output is:
bitvec2:00000000000000001111111111111111
6. Using bitwise operators
The Bitset class also supports built-in bitwise operators.
The use and introduction of C + + Bitset class