C + + standard library Bitset

Source: Internet
Author: User
Tags bit set bitset

This article address: http://www.cnblogs.com/archimedes/p/cpp-bitset.html, reprint please indicate source address.

Some programs handle an ordered set of bits, and each bit may contain a 0 (off) 1 (on) value. A bit is a concise way to hold yes/no information (sometimes called a flag) for a set of items or conditions. The Bitset class provided by the standard library simplifies the processing of bit sets. 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 Std::bitset is used:

#include <bitset>using std::bitset;
Definition and initialization of Bitset objects

The following table lists the Bitset constructors. Similar to the Vector,bitset class is a class template, and unlike a vector, a Bitset type object differs only in its length and 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<n> b; B has n bits, 0 per person.
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 the start of position Pos in S; n bits of a copy.
bitset<// + bits, all zero

The given length value must be a constant expression. As given here, the length value must be defined as an integer literal constant or as a const object of an integral type that has been initialized with a constant value. This statement defines Bitvec as a Bitset object that contains 32 bits. As with vector elements, the bits in the Bitset are not named and the programmer can only access them by location. The position number of the bit collection starts at 0, so the Bitvec's bit order is from 0 to 31. The bit string starting with 0 bits is the low order bit (Low-order), and the bit string ending with 31 bits is the higher order bit (High-order).

Initialize Bitset object with 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 bits are set to 0, and if the Bitset type is less than unsigned of the bits long value, only the lower order in the unsigned value is used, more than bists The high-order bits of the ET 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 initializerbitset< -> Bitvec1 (0xFFFF);//bits 0 ... Set to 1//bitvec2 same size as initializerbitset< +> bitvec2 (0xFFFF);//bits 0 ... Set to 1; 16 ... 0//On a 32-bit machine, bits 0 through initialized from 0xFFFFbitset< -> bitvec3 (0xFFFF);//bits through 127 initialized to zero

In the above three examples, 0 to 15 bits are set to 1. The higher order of the initial value of the BITVEC1 is discarded because the BITVEC1 bit is less than the number of bits of unsigned long. BITVEC2 and unsigned long are the same length, so all bits just place the initial value. The BITVEC3 length is greater than 32, and the higher-order bits above 31 bits are placed at 0.

Initializing a Bitset object with a String object
When the Bitset object is initialized with a string object, the string object is directly represented as a bit pattern. The order in which the bit sets are read from the string object is right-to-left, from the to:

string strval ("1100"); Bitset<> Bitvec4 (strval);

In the BITVEC4 bit mode, the position of 2nd and 3 is 1, and the remaining positions are 0. If the number of characters in a string object is less than the length of the Bitset type, the high-order position is 0.

The string object and the Bitsets object are reversed: the rightmost 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<5  4//  4 bits starting at str[5], 1100bitset<4// Use last 4 characters

This initializes the BITVEC5 with a substring of four characters starting with Str from str[5]. As usual, the Bitset object is initialized from the rightmost ending character of the substring, the binary position of BITVEC5 from 3 to 0 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:

Actions on the Bitset object

A variety of bitset operations (table 3.7) are used to test or set single or multiple bits in a Bitset object.

Test the entire Bitset object
If there is one or several binary positions in the Bitset object that are 1, the any operation returns true, that is, its return value equals 1, and the None operation returns True if BITS is all 0 in the Bitset object.

bitset<//+  bits, all zerobool//  false, all bits is zero BOOL // 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:

// returns number of bits that is on

The return type of the count operation is named size_t type 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 standard library. It is a machine-related unsigned type that is large enough to guarantee the size of the objects inside.
As with the size operation in vector and string, the size operation of Bitset returns the number of bits in the Bitset object, the type of the return value is size_t:

// returns

Accessing bits in a Bitset object
You can use subscript operators 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 binary:

// Assign 1 to even numbered bits  for (int021;

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 (int0-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// // equivalent test using subscriptif//  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.
To 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:

// Set all of the bits to 0. Bitvec. Set // set all the bits to 1

The flip action can reverse all or individual bits of a Bitset object

Bitvec.flip (0//  reverses valueof first bit bitvec[0//  also reverses The first bit//  reverses value of all bits

Gets 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:

Long ulong =""ulong << Endl;

To_ulong operations are primarily used to move Bitset objects to a C-style or standard C + + style program. A run-time exception is generated if the Bitset object contains more bits than the unsigned long length.

Output bits
You can output the bit patterns in the Bitset object with the output operator:

bitset<> bitvec2 (0xffff//  bits 0 ... set to 1;  16 ... 0"" << bitvec2 << Endl;

C + + standard library Bitset

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.