C ++ standard library bitset type

Source: Internet
Author: User

BitsetIt is used to process binary bitsets. Is a type of template.

#include<bitset>using std::bitsset

1. Definition and initialization

When defining a bitset, you must specify the number of bits in angle brackets.

bitset<32> bitvec;

The given length must beConstant expression, That is, it must be defined as an integer literal value constant or a const object.

(1). Use unsigned to initialize a bitset object

The unsigned long value is converted to the binary bit mode. If the length of the bitset type is greater than the binary digits of the unsigned long, the rest of the higher-order positions are 0. If the value is smaller than that, the lower-order digits of the unsigned long are used, higher-order bits exceeding the bitset value are discarded.

Note: low-order bit

Bitset:Taking a 32-bit bitset object as an example, the bit string starting with 0 is a low-order bit ), A 31-bit string is a high-order bit ).

Word (machine word ):On 32-bit unsigned long machines, there should be different storage methods on machines with different architectures. Reference from original English C ++ Primer 4 [On a machine with 32-bit unsigned long s, the hexadecimal value
0 xffff is represented in bits as a sequence of 16 ones followed by 16 zeroes. (Each 0xf digit is represented as 1111 .)] The storage method is low byte before and high byte after. Taking 0xffff as an example, the storage method in the machine is: 11111111111111110000000000000000, from low level --> high level.

Key Concept: computer memory storage

The memory storage method of the computer is determined by the CPU. However, for compatibility, AMD and INTEL are both stored in the same way: Low bytes are placed in front, and high bytes are placed behind. Understand,The memory address is 1 larger than the previous one,That is, low bytes are placed in a small address space, and high bytes are placed in a large address space.To sum up, no matter whether the memory storage method of bitset or computer machine words is the same, you only need to remember that the low level corresponds to a small space with a small memory address, the Higher Order corresponds to the space with a large memory address. (Low-latency, high-to-high, one-to-one matching ).

Knowing the above, it is easy to understand the storage method of bitset. Example:

// 1. use the unsigned value to initialize bitset <16> bitvec1 (0 xfffa); bitset <32> bitvec2 (0 xffff); cout <"bitvec1:" <bitvec1 <endl; cout <"bitvec2:" <bitvec2 <endl;

Output: bitvec1: 1111111111111010
Bitvec2: 00000000000000001111111111111010

At this time, strange things happen, and the output is not the expected order, but the opposite. Here, bitset outputs the <operator output from the higher-order --> lower-level (more natural) of bitset ). Therefore, if the for loop is used to output data from 0, it will be the actual storage order.

cout<<"bitvec1(RAW):"<<bitvec1<<endl;cout<<"bitvec1(FOR):";    for(size_t bs1 = 0;bs1!=bitvec1.size();bs1++){cout<<bitvec1[bs1];}cout<<endl;cout<<"bitvec2(RAW):"<<bitvec2<<endl;cout<<"bitvec2(FOR):";    for(size_t bs1 = 0;bs1!=bitvec2.size();bs1++){cout<<bitvec2[bs1];}cout<<endl;


Output: bitvec1 (RAW): 1111111111111010
Bitvec1 (FOR): 0101111111111111
Bitvec2 (RAW): 00000000000000001111111111111010
Bitvec2 (FOR): 01011111111111110000000000000000

(2). 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 order in which the bitset is read from the string object is from right to left.

// 2. initialize string strval ("1100") with a string object; bitset <8> bitvec4 (strval); cout <"bitvec4 (RAW):" <bitvec4 <endl; cout <"bitvec4 (FOR):"; for (size_t bs = 0; bs! = Bitvec4.size (); bs ++) {cout <bitvec4 [bs];}

Output: bitvec4 (RAW): 00001100

Bitvec4 (FOR): 00110000

The preceding string object strval can be viewed as a character array {'1', '1', '0', '0 '}.

It can be seen from the above that the string object and the bitset object areReverse ConversionThe rightmost character of the string object (the character with the largest subscript) is used to initialize the low-level bits of the bitset object (that is, the bit with the subscript 0 ). This is different from initializing a bitset object with the unsigned value. That isNot one-to-one correspondence with low-order.

Take the substring as the initial value.

string str("1111111000000011001101");bitset<32> bitvec5(str,5,4);bitset<32> bitvec6(str,str.size()-4);cout<<"bitvec5:"<<bitvec5<<endl;cout<<"bitvec6:"<<bitvec6<<endl;

Output: bitvec5: 00000000000000000000000000001100
Bitvec6: 00000000000000000000000000001101

Bitvec5 is initialized with a four-character substring starting with str [5.

If 3rd parameters are omitted, all characters from the start position to the end of the string object are taken. bitvec6 is initialized with four characters at the end of str.

2. bitset object operations

(1) Test the entire bitset object

Bool is_set = bitvec6.any (); // whether the bits set to 1 exist bool is_not_set = bitvec6.none (); // whether the bits set to 1 do not exist size_t bits_set = bitvec6.count (); // cout <"bits_set:" <bits_set <endl; size_t sz = bitvec6.size (); // return the number of binary bits in the bitset object. cout <"bits_size:" <sz <endl;

(2). Access bitset object bits

You can use subscript operations.Read/writeBinary bits of an index.

// Access the bits in the bitset object for (int index = 0; index! = Bitvec6.size (); index ++) {bitvec6 [index] = 1;} // you can also call set, reset, and other operations for (int index = 0; index! = Bitvec6.size (); index ++) {bitvec6.set (index) ;}// test if (bitvec6.test (3) {}// equivalent to if (bitvec6 [3]) {}

(3). Set the entire bitset object

Bitvec5.set (); // set all bitvec5.reset (); // reset all; // obtain bitvec5.flip (2); // obtain bitvec5 for a specific user [2]. flip (); bitvec5.flip (); // reverse all bits

 (4). Get the value of the bitset object

The to_long operation returns an unsigned long value, which is the same as the bitset bit mode storage value. To_long can be used only when the length of the bitset type is smaller than or equal to the length of unsigned long:

bitset<132> bitvec7(124);cout<<"bitvec7: "<<bitvec7<<endl;unsigned long ulong = bitvec7.to_ullong();cout<<ulong<<endl;

If the bitset object contains more binary digits than the length of unsigned long, a running exception occurs.

(5). Use bitwise operators

Bitset also supports built-in bit operators, which are only applicable to integer operands.

cout<<"bitvec7: "<<(bitvec7&=0xf)<<endl;


 


 

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.