C++_ series of self-study Courses _ _6_ Lesson _bitset Set _ C + + Primer fourth Edition

Source: Internet
Author: User
Tags bitset

 It takes a lot of skill to manipulate a bit of an integer in the C language. This is an abstract data type provided by the standard library in C + +

Bitset has been improved.

One, standard library bitset type

1, the role of Bitset

Bitset can be viewed as a set of bits, which can be individually accessed by one of the sets, and the result of the access can be used as a logical criterion. When using Bitset, you can

Does not care about how these bits are stored, but operates through a set of interfaces provided by the Bitset type.

Like string and vector, to use the Bitset type, it needs to be reported as follows:

#include <bitset>     // contains the associated header file #using std::bitset;   // declare the name using the appropriate namespace

2, the definition of Bitset

When defining a Bitset object, you need to indicate how many bits the Bitset object will store, which is set by the <> in the back of the Bitset, as follows:

Bitset<n>  Bittset;  // defines a Bitset object Bittest, n is the bit number of bits that an object can store

Exp:

Bitset<16> b16test; defines an object that stores a bitset of 16bit

Bitset<32> b32test; Define a Bitset object that stores 32bit

Points:

Bitset<n> Bitobj; When defined, the value of n must be an integer literal or a const object initialized with an integer literal, not a variable.

Must be noted.

Exp:

int Main () {    Bitset<5> Bitobj (5);    cout<<bitObj<<Endl;     return 0 ;}

The execution results are as follows:

[Email protected] cpp_src]# g++ test.cpp [[email protected] cpp_src]#. /a. Out00101

3. Initialization of Bitset class

Bitset provides a variety of initialization methods, as follows:

Bitset<n> bitsetObj1; Initializes all the bits of the Bitset object bitsetObj1 to 0;

Bitset<n> bitsetObj2 (U); The Bitset object BITSETOBJ2 is initialized by using the integer number of unsigned number U.

Bitset<n> bitsetObj3 (string); Initializes the Bitset object bitsetObj3 with a string object;

Bitset<n> bitsetObj4 (String, POS, M); Use a String object to initialize the M-bit starting from the POS bit

Points:

When the unsigned number U and string objects are initialized, there may be a case of length mismatch. And the string object is a sequence of characters consisting of 0 and 1.

When the unsigned number is initialized, the unsigned number is converted to binary format and stored in the Bitset object, and the string object is initialized with

The object is converted to a binary format and then stored in a Bitset object.

EXP: Initialize Bitset object

intMain () {stringStr="111110101101"; Bitset< ->bitObj1; 16 bits are all 0 bitset< +> BitObj2 (0xFFFF); 32 bit, height 16bit is all 0, low 16bit All is 1 bitset<8>bitObj3 (str); This needs to be cout the following .<<sizeof(0xFFFF) <<Endl; return 0;}

It is important to be aware that the initialization of a string object cannot be initialized with string literals.

For:

String str= "1111_1010_1101"; This is separated by _ for better viewing

bitset<8> bitObj3 (str);

bitset<32> bitObj4 (str);

The result of the BITOBJ3 bit's storage after initialization is as follows: 11111010;

the results of BITOBJ4 are as follows: 00000000000000000000111110101101

  The rule can be found: when the length of a string object is greater than the Bitset object's capacity, the second half of the string is truncated, while the lower subscript of the string object is kept relatively small

Sequence of characters.

When the length of a string object is less than the capacity of the Bitset object, all of the string objects are converted and the string object is moved "parallel" to the Bitset object's

The lower part, the remaining bitset of the lower part of the high portion with 0.

Exp:

intMain () {stringStr="111110101101"; Bitset< ->bitObj1; Bitset< +> BitObj2 (0xFFFF); Bitset<8>bitObj3 (str); Bitset< +>bitObj4 (str); cout<<bitObj3<<Endl; cout<<bitObj4<<Endl; cout<<sizeof(0xFFFF) <<Endl; return 0;}

The result of the execution is:

[Email protected] cpp_src]# g++ bitset_init.cpp [[email protected] cpp_src]#. /a. Out11111010000000000000000000001111101011014

EXP: Initializing a Bitset object using a partial string character sequence

intMain () {stringStr="1111111000000011001101"; Bitset< +> bitObj1 (str,5,4); Bitset< +> BitObj2 (str,str.size ()-4); cout<<bitObj1<<Endl; cout<<bitObj2<<Endl; cout<<str.size () <<Endl; return 0;}

The execution results are as follows:

[[email protected] cpp_src]#./a. Out00000000000000000000000000001100  00000000000000000000000000001101

Here's an explanation of the result:

String str ("11_1111_1000_0000_1100_1101"); //

Bitset<32> bitObj1 (str, 5, 4); This initialization means starting with str[5] in the Str object and taking 4 characters to initialize the BitObj1 object. Because it's taken out.

The character length is less than 32bit, so it follows the previously mentioned assignment method of a string length less than the length of the Bitset object.

Bitset<32> bitObj2 (str, str.size ()-4); There are only two parameters, and the last parameter does not, it means to take the character sequence from Str.size ()-4, to take the word all the time.

Characters to the last character of the string object, and then convert, and the conversion process follows the assignment rules for the length of the string object and the Bitset object as previously mentioned.

Therefore, the result of the above operation is obtained.

  In fact, the following two types of initialization mainly need to pay attention to how to take the character sequence, the character sequence is taken out, the extracted character sequence as a new string object for the initial

It's OK .

  

4, the Bitset class provides the operation

As with other standard library classes, Bitset also offers a number of operations, mainly as follows:

Bitset<n> Bitobj;

Bitobj.any (); Returns whether a bit is already set to 1 in the Bitobj object

Bitobj.none (); Return bitobj does not exist as 1 binary bit, is to test whether all the bit is all 0, all is 0 o'clock return True

Bitobj.count (); Returns the number of bits in Bitobj 1

Bitobj.size (); Returns the number of bits of the Bitobj object

Bitobj[pos]; Returns the value of the binary bit at the POS location of the Bitobj

Bitobj.set (); Set to 1;

Bitobj.set (POS); Set the bit at POS at 1;

Bitobj.reset (); The settings are all 0;

Bitobj.reset (POS); Set POS position to 0

Bitobj.flip (); All bits take the reverse;

Bitobj.flip (POS); Reverse the bit at the POS location.

Bitobj.to_ulong () converts bitobj to unsigned long int type

os<<b; The Bitset set in B is output to the OS stream.

intMain () {Bitset<5> Bitobj (5); cout<<bitObj<<Endl; cout<<"The size of Bitobj is:"<<bitobj.size () <<Endl; cout<<"Bitobj.to_ulong () is:"<<bitobj.to_ulong () <<Endl; cout<<"Bitobj[3] is:"<<bitobj[3]<<Endl; if(Bitobj.any ()) cout<<"Bitobj has some bit = = 1"<<Endl; Elsecout<<"Bitobj has no bit = = 1"<<Endl; if(Bitobj.none ()) cout<<"bitobj all bit is 0."<<Endl; Elsecout<<"Bitobj has some bit = = 1"<<Endl; Bitobj.Set(); cout<<"After Bit.set () bitobj is:"<<bitObj<<Endl; Bitobj.reset (3); cout<<"After Bit.reset (3), the bitobj is:"<<bitObj<<Endl;    Bitobj.reset (); cout<<"After Bitobj.reset () bitobj is:"<<bitObj<<Endl; Bitobj.Set(4); cout<<"After Bitobj.set (4), Bitobj is:"<<bitObj<<Endl; cout<<"Bitobj.flip () is:"<<bitobj.flip () <<Endl; cout<<"Bitobj is"<<bitObj<<Endl; Bitobj.flip (1); cout<<"After Bitobj.flip (1), Bitobj is:"<<bitObj<<Endl; return 0;}

The execution results are as follows:

[[email protected] cpp_src]#./A. out 00101The size of Bitobj is:5Bitobj.to_ulong () is:5bitobj[3] is:0Bitobj has some bit==1Bitobj has some bit==1After bit.Set() bitobj is:11111After Bit.reset (3), the Bitobj is:10111After Bitobj.reset () bitobj is:00000After bitobj.Set(4), Bitobj is:10000Bitobj.flip () is:01111bitobj is01111after Bitobj.flip (1), Bitobj is:01101[email protected] cpp_src]#

The above example can be used to know that two features require special attention:

B.set (n), B.reset (n), B.flip (n); This n is calculated starting from 0, and is calculated from the lowest bit.

Here's another example:

 int   main () {bitset  <5  > Bitobj ();    cout  <<bitobj<<ENDL;    cout  <<bitobj[0 ]<<ENDL;    cout  <<bitobj[1 ]<<ENDL;    cout  <<bitobj[2 ]<<ENDL;    cout  <<bitobj[3 ]<<ENDL;    cout  <<bitobj[4 ]<<ENDL;  return  0  ;}  

The result of the execution is:

[Email protected] cpp_src]# g++ test.cpp [[email protected] cpp_src]#. /a. Out0110000110

  From the above results, the subscript operation of the Bitset is counted starting from 0, and is the lowest bit of bitset, which is counted from the lowest to the beginning, that is, bitobj[0].

This time, here, the following will be the contents of the C language-related arrays and pointers, to be continued ...

C++_ series of self-study Courses _ _6_ Lesson _bitset Set _ C + + Primer fourth Edition

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.