C ++ standard library bitset, standard library bitset
URL: http://www.cnblogs.com/archimedes/p/cpp-bitset.html.
Some programs need to process the ordered set of binary bits. Each bit may contain 0 (off) 1 (on) values. Bit is a concise method used to save the yes/no information (sometimes called a flag) of a group of items or conditions. The bitset class provided by the standard library simplifies bit set processing. To use the bitset class, you must include the relevant header file. In the example provided in this book, assume that both use the using declaration of std: bitset:
#include <bitset>using std::bitset;
Definition and initialization of bitset objects
The following table lists the bitset constructor. Similar to vector, the bitset class is a type template. Unlike vector, bitset objects differ only in their length but not in their type. When defining a bitset, you must specify the number of BITs contained in the bitset. The length value must be given in angle brackets:
Bitset <n> B; |
B has n bits, and each has 0 |
Bitset <n> B (u ); |
B is a copy of unsigned long u. |
Bitset <n> B (s ); |
B is a copy of the bits contained in string object s. |
Bitset <n> B (s, pos, n ); |
B starts from the position pos in s; n-digit copies. |
bitset<32> bitvec; // 32 bits, all zero
The given length value must be a constant expression. As shown here, the length value must be defined as an integer literal value constant or an integer const object initialized with a constant value. This statement defines bitvec as a bitset object containing 32 digits. Like vector elements, bitset has no names, and programmers can only access bitset by position. The position number of the bit set starts from 0. Therefore, the bitvec order is from 0 to 31. The bits starting with 0 bits are low-order bits, and the bits ending with 31 bits are high-order bits ).
Use the unsigned value to initialize a bitset object
When the unsigned long value is used as the initial value of the bitset object, the value is converted to the binary bit mode. The bit set in the bitset object acts as a copy of this bit mode. If the length of the bitset type is greater than the binary digits of the unsigned long value, the rest of the high-level bits are set to 0. If the length of the bitset type is smaller than the binary digits of the unsigned long value, only the low-order bits in the unsigned value are used. The higher-order bits that exceed the length of the bistset type are discarded.
On 32-bit unsigned long machines, the hexadecimal value 0 xffff indicates that the binary value is sixteen 1 and sixteen 0 (each 0xf can be expressed as 1111 ). You can use 0 xffff to initialize a bitset object:
// bitvec1 is smaller than the initializer bitset<16> bitvec1(0xffff); // bits 0 ... 15 are set to 1 // bitvec2 same size as initializer bitset<32> bitvec2(0xffff); // bits 0 ... 15 are set to 1; 16 ... 31 are 0 // on a 32-bit machine, bits 0 to 31 initialized from 0xffff bitset<128> bitvec3(0xffff); // bits 32 through 127 initialized to zero
In the above three examples, 0 to 15 digits are set to 1. Because bitvec1 has fewer digits than unsigned long, the higher order of the initial value of bitvec1 is discarded. Bitvec2 and unsigned long have the same length, so the initial values are placed for all bits. The length of bitvec3 is greater than 32, and the higher-order bits with 31 or more bits are set to 0.
Use a string object to initialize a bitset object
When a bitset object is initialized with a string object, the string object is expressed as a bit. The ascending order of the bit set read from the string object is from right to left (from right to left ):
string strval("1100");bitset<32> bitvec4(strval);
In bitvec4 bit mode, the positions 2nd and 3 are 1, and the remaining positions are 0. If the number of characters of the string object is smaller than the length of the bitset type, the higher order is 0.
The string object and bitsets object are converted in reverse order: the rightmost character of the string object (that is, the character with the largest subscript) is used to initialize the low-level bit of the bitset object (that is, the bit with the subscript 0). Remember this difference when initializing a bitset object with a string object.
You do not have to use the entire string object as the initial value of the bitset object. Instead, you can use a substring as the initial value:
string str("1111111000000011001101");bitset<32> bitvec5(str, 5, 4); // 4 bits starting at str[5], 1100bitset<32> bitvec6(str, str.size() - 4); // use last 4 characters
Here, 'str' is used to initialize bitvec5 from 'str' [5], which contains four characters. As usual, when initializing a bitset object, it always starts from the rightmost ending character of the substring. bitvec5's binary position from 3 to 0 is 1100, and other binary bits are set to 0. If the third parameter is omitted, all characters from the start position to the end of the string are taken. In this example, retrieve the four digits at the end of str to initialize the lower four digits of bitvec6. The remaining bitvec6 bits are initialized to 0. The following figure shows the initialization process:
Operations on bitset objects
Multiple bitset operations (table 3.7) are used to test or set one or more binary bits in a bitset object.
Test the entire bitset object
If one or more of the bitset objects have a binary position of 1, The any operation returns true, that is, the return value is equal to 1. On the contrary, if the bitset object has a binary position of 0, true is returned for the none operation.
bitset<32> bitvec; // 32 bits, all zerobool is_set = bitvec.any(); // false, all bits are zerobool is_not_set = bitvec.none(); // true, all bits are zero
If you need to know the number of binary bits set to 1, you can use the count operation. This operation returns the number of binary bits set to 1:
size_t bits_set = bitvec.count(); // returns number of bits that are on
The return type of the count operation is size_t in the standard library. The size_t type is defined in the cstddef header file. This file is the C ++ version of The stddef. h header file of the C standard library. It is a machine-related unsigned type, and its size is sufficient to ensure the size of objects in the storage.
Like the size operation in vector and string, the size operation of bitset returns the number of binary bits in the bitset object. The returned type is size_t ::
size_t sz = bitvec.size(); // returns 32
Access bits in a bitset object
You can use the subscript operator to read or write binary digits at an index position. Similarly, you can use the subscript operator to test the value of a given binary digit or set the value of a binary:
// assign 1 to even numbered bits for (int index = 0; index != 32; index += 2) bitvec[index] = 1;
The above loop sets the even bits in bitvec to 1.
In addition to the subscript operator, you can also use the set;, test, and reset operations to test or set the value of a given binary bit:
// equivalent loop using set operation for (int index = 0; index != 32; index += 2) bitvec.set(index);
To test whether a binary digit is 1, you can use the test operation 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 binary bit tested by the subscript operator is 1, the returned test value is true. Otherwise, false is returned.
Set the entire bitset object
The set and reset operations are used to set 1 and 0 for all binary bits of the entire bitset object respectively:
bitvec.reset(); // set all the bits to 0.bitvec.set(); // set all the bits to 1
The flip operation can reverse all or individual bits of a bitset object.
bitvec.flip(0); // reverses value of first bitbitvec[0].flip(); // also reverses the first bitbitvec.flip(); // reverses value of all bits
Obtains the value of a bitset object.
The to_ulong operation returns an unsigned long value, which is the same as the bit mode storage value of the bitset object. To_ulong can be used only when the length of the bitset type is smaller 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 convert a bitset object to a C-style or standard C ++-style program. If the binary number of a bitset object package exceeds the length of unsigned long, a running exception occurs.
Output binary bit
The bitset object bit mode can be output using the output OPERATOR:
bitset<32> bitvec2(0xffff); // bits 0 ... 15 are set to 1; 16 ... 31 are 0cout << "bitvec2: " << bitvec2 << endl;