Explain C++ bitset

  • 2020-06-15 09:54:27
  • OfStack

C++ bitset In the bitset header file, it is an array-like structure where each element can only be 0 or 1, and each element USES only 1bit space.

Here's how to use it

The constructor

There are four common constructors for bitset, as follows


bitset<4> bitset1;  // No parameter construction, length 4, default per 1 Bit is zero 

 bitset<8> bitset2(12);  // The length is 8, 2 Save in base and add in 0 

 string s = "100101";
 bitset<10> bitset3(s);  // Length of 10 And I'm going to put a zero in front of it 
 
 char s2[] = "10101";
 bitset<13> bitset4(s2);  // Length of 13 And I'm going to put a zero in front of it 

 cout << bitset1 << endl;  //0000
 cout << bitset2 << endl;  //00001100
 cout << bitset3 << endl;  //0000100101
 cout << bitset4 << endl;  //0000000010101

Note:

When constructed with strings, strings can only contain '0' or '1', otherwise an exception is thrown.

When constructing, must be in < > Indicates the size of bitset (i.e. size).

During parameter construction, if the base 2 representation of the parameter is smaller than size of bitset, it is supplemented with 0 in the front (chestnuts above); If larger than bitsize, take the following part if the parameter is an integer, and the first part if the parameter is a string (such as the following chestnut) :


bitset<2> bitset1(12);  //12 the 2 Into the system for 1100 (Length 4), but bitset1 the size=2 And I'm just going to take the next part, which is 00

 string s = "100101";  
 bitset<4> bitset2(s);  //s the size=6 And the bitset the size=4 , just take the front part, that is 1001

 char s2[] = "11101";
 bitset<4> bitset3(s2);  // with bitset2 Similarly, just take the first part, which is 1110

 cout << bitset1 << endl;  //00
 cout << bitset2 << endl;  //1001
 cout << bitset3 << endl;  //1110

Available operators

bitset has an operator for base 2, as follows


bitset<4> foo (string("1001"));
 bitset<4> bar (string("0011"));

 cout << (foo^=bar) << endl; // 1010 (foo right bar To assign by bit or post foo)
 cout << (foo&=bar) << endl; // 0010 ( Assign to bit and post foo)
 cout << (foo|=bar) << endl; // 0011 ( To assign to a bit or post foo)

 cout << (foo<<=2) << endl; // 1100 ( Move 2 bits to the left, fill 0 in the lower order, have its own assignment )
 cout << (foo>>=1) << endl; // 0110 ( Move 1 bit to the right, fill 0 to the top, assign itself )

 cout << (~bar) << endl;  // 1100 ( According to the not )
 cout << (bar<<1) << endl;  // 0110 ( Left shift, no assignment )
 cout << (bar>>1) << endl;  // 0001 ( Right shift, no assignment )

 cout << (foo==bar) << endl; // false (0110==0011 for false)
 cout << (foo!=bar) << endl; // true (0110!=0011 for true)

 cout << (foo&bar) << endl; // 0010 ( Bits and, no value assigned )
 cout << (foo|bar) << endl; // 0111 ( Bitwise or, no value assigned )
 cout << (foo^bar) << endl; // 0101 ( Bitwise or, not assigned )

In addition, you can access elements (like arrays) through [], note that the least-significant index is 0, as follows:


 bitset<4> foo ("1011");
 
 cout << foo[0] << endl;  //1
 cout << foo[1] << endl;  //1
 cout << foo[2] << endl;  //0

Of course, it's ok to assign a value to a 1 bit element in this way, but I'm not going to put it in there.

Available functions

bitset also supports 1 interesting functions, such as:


bitset<8> foo ("10011011");

 cout << foo.count() << endl;  //5 ( count The function is used to find bitset In the 1 The figures, foo There are five ones 
 cout << foo.size() << endl;   //8 ( size The function is used to find bitset The size of the 1 A total of eight 

 cout << foo.test(0) << endl;  //true ( test The function looks up whether the element at the index is 0 or 1 and returns false or true And here, foo[0] Is 1. Return true
 cout << foo.test(2) << endl;  //false (in the same way, foo[2] Zero, return false

 cout << foo.any() << endl;  //true ( any Function checks bitset Is there a 1 in theta 
 cout << foo.none() << endl;  //false ( none Function checks bitset Is there no 1 in theta 
 cout << foo.all() << endl;  //false ( all Function checks bitset Is all 1 

Note 1: the test function checks for subscript overbounds, while accessing elements through [] does not check for subscripts, so it is safer to select the test function when both methods are common

In addition, there are 1 functions:


bitset<8> foo ("10011011");

  cout << foo.flip(2) << endl;  //10011111 ( flip When a function passes an argument, it is used to reverse the argument bit foo The subscript 2 " reverse " So 0 goes to 1,1 goes to 0 
  cout << foo.flip() << endl;   //01100000 ( flip When the function does not specify parameters bitset every 1 All the bits are reversed 

  cout << foo.set() << endl;    //11111111 ( set When the function does not specify parameters bitset Each of the 1 All the bits are set to 1 
  cout << foo.set(3,0) << endl;  //11110111 ( set When the function specifies two - digit arguments, the 1 The element of the parameter bit is set as the control 2 Parameter, row pair foo Operation is equivalent to foo[3]=0
  cout << foo.set(3) << endl;   //11111111 ( set Function only 1 , the parameter subscript is disposed as 1 

  cout << foo.reset(4) << endl;  //11101111 ( reset Function and 1 When the parameter is 0, the parameter subscript is disposed as 0 
  cout << foo.reset() << endl;   //00000000 ( reset The function will pass no arguments bitset Each of the 1 All the bits are set to 0 

Again, they all check to see if the index is out of bounds, and if it is, an exception is thrown

Finally, there are some type conversion functions, as follows:


bitset<8> foo ("10011011");

 string s = foo.to_string();  // will bitset Converted to string type 
 unsigned long a = foo.to_ulong();  // will bitset Converted to unsigned long type 
 unsigned long long b = foo.to_ullong();  // will bitset Converted to unsigned long long type 

 cout << s << endl;  //10011011
 cout << a << endl;  //155
 cout << b << endl;  //155

Related articles: