Read "C + + Primer" (3-3 standard library bitset type)

Source: Internet
Author: User
Tags bitset

Urge reading, summarize the essence, refining notes, a point, there are inappropriate places, welcome message.

// opening #include <bitset>using std::bitset;

Issue 1, Standard library Bitset type (template)

When you need to handle bits, you can use the Bitset type provided by the C + + standard library, which is also a class template, similar to the Vectro container, except that the Bitset type needs to describe the length, use the integer literal value given by the constant expression, or the Cosnt object that has already been initialized.

    bitset<> bit; // from 0 to 31 bits, 32 bits per bit are initialized to 0

Initializes the Bitset object with an unsigned value that is initialized to a binary sequence, and if the unsigned value is less than the Bitset object, truncate the higher-order bit

    bitset<> bit1 (0xffff); // 0-15 bits are 1    . bitset<> Bit2 (0xffff); // 0-15 bit is 1,16-31 is 0    bitset<> Bit3 (0xffff); // 32-127 bits are 0    . bitset<8> bit0 (0xffff); // The high is cut off

Issue 2, problems needing attention when initializing Bitset objects with a String object

When initializing a Bitset object with a string object, it is initialized directly to the binary sequence, starting from the right of the string Object!!

1     stringSTR1 (" +");2     //the 0-3 bits of the Bit1 object are 0001 and the remaining highs are 03bitset< +>bit1 (str1);4     //It must be noted that the conversion of the string object and the Bitset object is reversed! That is, the maximum subscript (right) of the string object begins to read inside the Bitset object.5 6     //You can also initialize this7     stringSTR2 ("1111111100000000");8     //initializes the Bit2 object from the 4-bit starting at the 5th bit of the STR2 string Object9     //namely: 1100Ten     //read from the right (the high-level reading of a string object, which is 0011 of the 0-3 bits stored in the Bit2 object) Onebitset< +> Bit2 (str2,5,4);//the other bits numbers are 0 . A  -     //If the third argument is omitted, it is from the specified position to the end -     stringSTR3 ("10001000"); the     //starting from the 2nd bit of the STR3 string object to the end -     //001000 -bitset< +> Bit3 (STR3,2);//stored in the Bit3 object, 0-5 bits are 000100.

Issue 3, common operations for Bitset objects

    bitset<> B (0x0000);     // if the bit order in B contains 1, then return true    if (B.any ())    {        " Execute!  " << Endl; // is not executed and returns false    }

Measure the number of 1 in the B object.

cout << b.count () << Endl; // 0

Note that the value returned by the Count function is the size_t type, defined in the header file Cstddef, where C is stddef.h,size_t is an unsigned and machine-independent integer. Similar to unsigned int types

size_t num = B.count (); // OK

The following, although not an error, but previously said similar problems, not recommended to use, here is considered to be wrong

// int num2 = B.count ();

Similar to a container vector or string type, Bitset also has a function of length size ()

    cout << b.size () << Endl; // Print 32, stating that the length    specified at the time of the actual definition is calculated // also returned is the size_t type    size_t NUM1 = B.size ();

Access to bits in the Bitset object, similar to other containers or standard libraries, subscript operation of arrays

    bitset<> bit; // Auto Default initialized to 32 x 0     // Loop, I use the int type definition here, because the length of the Bitset object here is used 32, that is, the int type defines the     for (int0; i++)    {        1; // 32 bits. 0-31 all initialized to 1    }

Test Bitset Object One is not 1

    bitset<> bit1 (0x1000);     if (Bit.test (0))    {        " execution " << Endl; // Execute! Description The first bit is 1    }

Or use the return value of the subscript operation directly

    cout << bit1[1] << Endl; // print 0, corresponding to    false // then nature can be directly to make a bool judgment, because it is nothing more than return 0 or 1 ah

Set all bits numbers to 1

    Bit1. Set ();     << bit1.count () << Endl; //  +

Just set the 1th bit to 1.

    bitset<> Bit2 (0x0000);    Bit2. Set (0);     << bit2.count () << Endl; // 1    cout << bit2.any () << Endl; // 1

Set all bits to 0

    Bit2.reset ();     << bit2.count () << Endl; // 0

Similarly, just set a bit to 0

    bitset<> Bit3 (0x1111);     << bit3.count () << Endl; // 4    Bit3.reset (0);     << bit3.count () << Endl; // 3

Take counter action

    bitset<> Bit4 (0x0000);     << bit4.count () << Endl; // 0    Bit4.flip (); // Bitwise Inverse    of all digits cout << bit4.count () << Endl; //  +

Similarly, a similar operation only takes the reverse

    Bit4.flip (0); // reverse the first position

You can use the following function when and only if the length of the Bitset object is less than or equal to a variable of the unsigned long integer

    long ln = Bit4.to_ulong (); // OK     // Otherwise, an error occurred, abnormal

Question 4, must pay attention, Bitset object's subscript question, is starting from the right!

    // Direct Output Bitset object    bitset<> Bit5 (0xffff);     << bit5 << Endl;

    bitset<> bit (0xffff);     << bit << Endl;

It must be noted that the subscript starts from the right and is 0-31, not the traditional left.

Starting from the right 0-15 is 1, the remaining high 0 is filled.

Question 5, title,bitset<32> bit (1010101), what is the result of initializing bit?

    bitset<> bit (1010101);     // Note that this initializes the default of 1010101 is decimal! Convert first to binary    ://000000000000011110110100110110101    //  Then initialize the bit object to be a binary sequence

Don't take it for granted that this is written in 1010101 is binary, in fact the default is the decimal form

Summary:

arrays and pointers defined by the C + + standard library are low-level abstract data types, and standard library containers such as vectors, standard library bitset templates, string types, and so on, are advanced abstract data types! Where the string type provides a variable-length string store operation, and the vector container provides a storage operation for an object of a total type.

There are also learned iterators that provide indirect access to objects within the container (instead of subscript). such as accessing and traversing objects within a vector container or elements of type string.

Remember, good C + + programmers should be accustomed to using advanced abstract data types and avoid using low-level arrays and pointers as much as possible. Unless you are a module that emphasizes the speed of the program, you should use low-level composite data types, pointers, or arrays.

Read "C + + Primer" (3-3 standard library bitset type)

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.