Bitest (bit set)------C + + programming principles and Practices (advanced)

Source: Internet
Author: User
Tags bit set bitset bitwise operators

The standard library template class Bitset is defined in <bitset> and is used to describe and manipulate bits collections. The size of each bitset is fixed and is specified at creation time:

bitset<4> flags;bitset<128> dword_bits;bitset<12345> Lots;

By default, Bitset is initialized to full 0, but usually we give it an initial value, which can be an unsigned integer or a string consisting of "0" and "1". For example:

bitset<4> flags=0xb;bitset<128>dword_bits{string{"1010101010101010"}};bitset<12345>lots;

In these two pieces of code, lots is initialized to full 0,dword_bits Money 112 is initialized to full 0, and the latter 16 bits are explicitly specified by the program. If you give an initialization string that contains symbols other than 0 and 1, Bitset throws a Std::invalid_argument exception:

String s;cin>>s;bitset<12345> my_bits (s);    May throw Std::invalid_argument

The common bitwise operators are available for Bitset. For example, assume that B1, B2, and B3 are all Bitset:

b1=b2&b3;    and b1=b2|b3;    or b1=b2^b3;    different or b1=~b2;    Complement b1=b2<<2;    Move left b1=b2>>3;    Move right

In general, for bit operations, Bitset is like unsigned int, except that its size is arbitrary and is specified by the user. What you can do to unsigned int (in addition to arithmetic operations) is what you can do with Bitset. In particular, Bitset is also useful for I/O:

cin>>b;    Reads a bitsetcout<<bitset<8> (' C ') from the input;    Bit pattern for output character ' C '

When Bitset is read, the input stream looks for 0 and 1, for example, if you enter the following:

10121

Input stream will be read into 101,21 will be left.

For bytes and words, the bits in the bitset are numbered from right to left (from the least significant bit to the most significant bit). Thus, the value of the 7th bit is 27;

7: 6: 5: 4: 3: 2: 1: 0:
1 0 1 0 0 1 1 1

For Bitset, the numbering sequence is not just a matter of convention, but also the index subscript of bits. For example:

#include <iostream> #include <bitset>using namespace Std;int main () {int n;const int j = n;constexpr int max = 10 ; for (bitset<max> b; Cin >> B;) {cout << b << ' \ n '; for (int i = 0; i < max; ++i) cout << b[i];cout << ' \ n ';}}

  

Principles and Practice of C + + programming (Advanced article)

Bitest (bit set)------C + + programming principles and Practices (advanced)

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.