Recognize the bitset type in C ++ and the cbitset type

Source: Internet
Author: User
Tags bit set bitset

Recognize the bitset type in C ++ and the cbitset type

Understanding the bitset type of the standard library

Bitwise is a concise method used to save the yes/no (1 or 0) information of a group of items or conditions. Then the bitwise set is the ordered set of binary bits. The bitset class provided by the standard library in C ++ effectively simplifies bit set processing in our program.

Definition of a bitset object

To use bitset, we must first include the header file bitset. Unlike a vector object, a bitset object is only distinguished by its length rather than its type. When defining a bitset, you must specify the number of BITs contained in the bitset. The length value must be given in angle brackets. The length value must be defined as an integer const object whose nominal value is a constant or has been initialized with a constant value.

1 const int maxn=16;2 bitset<32> bitvec2;3 bitset<maxn> bitvec(1);

The bit string starting with 0 in a bitset is a low-order bit, And the Bit String ending with 31 is a high-order bit.

Bitset object initialization

1. Use the unsigned value for initialization.

When the unsigned long value is used as the initial value of the bitset object, the value is converted to the binary bit mode. The bit set in the bitset object acts as a copy of this bit mode. If the length of the bitset type is greater than the binary digits of the unsigned long value, the other high-level bits are set to 0. If the length of the bitset type is smaller than the binary digits of the unsigned long value, only the low-order bits in the unsigned value are used. The higher-order bits that exceed the length of the bitset type are discarded.

1 bitset<32> bitvec3(0xffff);2 cout<<bitvec3<<endl;3 for (int i=0 ; i<bitvec3.size() ; i++)4     cout<<bitvec3[i]<<" ";5 cout<<endl;

 

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.

1 string str("11100");2 bitset<8> bitvec5(str);3 cout<<bitvec5<<endl;4 for (int i=0 ; i<bitvec5.size() ; i++)5     cout<<bitvec5[i]<<" ";

Note: The string object and bitset object are converted reversely. the rightmost character of the string object (that is, the character with the largest subscript) it is used to initialize the low-level bits of a bitset object (that is, the bits of subscript 0 ).

Access bits in a bitset object

Like a vector, bitset can also use the subscript operator to read and write the binary bits of an index location. Similarly, you can also use the subscript operator to test the value of a given binary bit or set a binary value. In fact, accessing the bitset object is a for loop. As mentioned in the Code illustration above, we will not detail it here.

Bitset operation functions

Bitset operation function implementation

 

 1 #include<iostream> 2 #include<cstdio> 3 #include<cstdlib> 4 #include<cstring> 5 #include<cmath> 6 #include<algorithm> 7 #include<bitset> 8 using namespace std; 9 const int maxn=16;10 11 int main()12 {13     bitset<16> b2;14     cout<<"b2.any() = "<<b2.any()<<endl;15     cout<<"b2.none()= "<<b2.none()<<endl;16     cout<<"b2.size()= "<<b2.size()<<endl;17     cout<<"b2[4]= "<<b2[4]<<endl;18     cout<<"b2.test(4)= "<<b2.test(4)<<endl;19     b2.set() ; cout<<endl;20     cout<<"b2.any() = "<<b2.any()<<endl;21     cout<<"b2.none()= "<<b2.none()<<endl;22     cout<<"b2.size()= "<<b2.size()<<endl;23     cout<<"b2[4]= "<<b2[4]<<endl;24     cout<<"b2.test(4)= "<<b2.test(4)<<endl;25     b2.reset() ; cout<<endl;26     cout<<"b2.any() = "<<b2.any()<<endl;27     cout<<"b2.none()= "<<b2.none()<<endl;28     cout<<"b2.size()= "<<b2.size()<<endl;29     cout<<"b2[4]= "<<b2[4]<<endl;30     cout<<"b2.test(4)= "<<b2.test(4)<<endl;31     b2.set(4) ; cout<<endl;32     cout<<"b2[4]= "<<b2[4]<<endl;33     cout<<"b2.test(4)= "<<b2.test(4)<<endl;34     return 0;35 }

 

 

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.