C + + Stl-bitset

Source: Internet
Author: User
Tags bitset

Statement of the 1.bitset

#include <bitset>using std::bitset;

The definition and initialization of a 2.bitset object can be declared as a variable of that type as follows:

Bitset<n>Varm (M); // where Varm is the variable name.  //n Indicates the number of bits that the type occupies in memory, which is binary.  //m represents the initial value of the variable varm. 

Methods for initializing Bitset

Bitset<n> b;    // b has n bits, each ofwhich is 0 bitset<n> B (u); //     B is a copy of unsigned long 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

We can also use the string type to initialize the value of the bitset when we use it.

However, a string value of 1 and 0 is required.

It is also important to note that:

The following is an example of a reverse conversion between a string object and a Bitset object:

Actions on a 3.bitset object

The following direct references to C + + primer

1. Test the entire Bitset object

If one or more of the binary positions in the Bitset object is 1, the any operation returns true, that is, its return value equals 1, and conversely, if the bits in the Bitset object is 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 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, and the type of the return value is size_t:

size_t sz = bitvec.size (); Returns 32

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;

To_ulong operations are primarily used to move Bitset objects to a C-style or standard C + + style 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.13 and 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

C + + Stl-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.