(Rpm) C ++ bitset usage

Today do find problems to use bitset, find giant good article describes.

 

Reprinted from: https: //www.cnblogs.com/magisk/p/8809922.html

 

Bitset bitset in the C ++ header file, which is an array-like structure that each element can only be 0 or 1, each element only 1bit space.

The following are specific usage

Constructor

There are four common bitset constructors, as follows

Copy the code
    the bitset < 4> bitset1; // constructor with no arguments, length is 4, is a per default 0 

    the bitset < 8> bitset2 ( 12 is ); // length 8 binary storage, front supplemented with 0 String S = " 100101, China " ; the bitset < 10> bitset3 (S); // length of 10, the front supplemented with 0 char S2 [] = " 10101 " ; the bitset < 13> bitset4 (S2); // is 13, the front supplemented with 0 cout < <bitset1 << endl; // 0000 COUT bitset2 << << endl; // 00001100 COUT bitset3 << << endl; // 0000100101 COUT bitset4 << << endl; // 0,000,000,010,101

    
Copy the code

 

note:

When configured with the string, the string can contain '0' or '1', otherwise an exception is thrown.

When configured, the need to show the size of the bitset (i.e., size) in <>.

During configuration have parameters, if the binary representation of the argument is smaller than the size bitset is supplemented with zeros in front (as in the above chestnut); BitSize if larger than, the rear part of the parameters taken as an integer, a string parameter taken the front portion (e.g. chestnut below):

Copy the code
    the bitset < 2> bitset1 ( 12 ); // binary 12 is 1100 (length 4), but the bitset1 size = 2, to take only the rear portion, i.e. 00 String S = " 100101, China " ; the bitset < 4> bitset2 (S ); // s of size = 6, and the bitset size = 4, take only the front part, i.e. 1001 char S2 [] = " 11101 " ; bitset < . 4> bitset3 (S2); // with bitset2 Similarly, only take the front portion, i.e. bitset1 << << COUT 1110 endl; // 00 COUT bitset2 << << endl; // << 1001 COUT bitset3 << endl; // 1110

    
Copy the code

 

Available operator

For binary bitset One operator, as follows

Copy the code
    the bitset < . 4> foo ( String ( " 1001 " )); the bitset < . 4> bar ( String ( " 0011 " )); COUT << (foo ^ = bar) << endl; // 1010 (foo to bar Bitwise assigned to the XOR foo) << COUT (& foo = bar) << endl; // 0010 (assigned to the bit after foo) << COUT (foo | = bar) << endl; // 0011 (bitwise or assigned to the foo) COUT << (foo << = 2) << endl; // 1100 is (left two, the low 0s, has its own assignment) COUT << (>> = foo . 1) << endl ; // 0110 (right by 1 bit, the high bit of 0, has its own assignment) COUT << (~ bar) << endl; // 1100 is (bitwise) cout << (bar <<1) << endl; // 0110 (left, no assignment) cout << (bar >>. 1) << endl; // 0001 (right, not assigned) COUT << (foo == bar) << endl; // ! To false (0011 to 0110 == false) cout << (foo = bar ) < <endl; // to true (0110 = 0011 is to true!) cout << (foo & bar) << endl; // 0010 (bitwise aND, not assigned) cout << (foo | bar) << endl; // 0111 (bitwise or, not assigned) << COUT (foo ^ bar) << endl; // 0101 (bitwise exclusive or, not assigned)
Copy the code

Further, by [] accessed element (array-like), note that the least significant bit at index 0, as follows:

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

Of course, this way of assigning a bit elements is also possible, to hold a chestnut.

 

Available functions

bitset also supports some interesting functions, such as:

Copy the code
    bitset < . 8> foo ( " 10,011,011 " ); 
 COUT << foo.count () << endl; //. 5 (COUNT function is used to find the number of bits the bitset 1, foo total of 5 1 cout << foo. size () << endl; //. 8 (bitset size function is used to find the size of a total of 8 << foo.test COUT ( 0) << endl; // to true (Test function is used to search at index element is 0 or 1, and returns false or true, where foo [0] is 1, COUT << foo.test returns to true ( 2) << endl; // false (Similarly, foo [2] is 0, << foo.any COUT returns to false () << endl; // to true (the any function to check whether there bitset COUT << foo.none 1 () << endl; // to false (none function to check whether or not a bitset << foo.all COUT () << endl; // to false (all function to check all bitset is 1
Copy the code

Side note: make check test function will subscript out of bounds, but by [] elements but will not visit subscript checking through, so, in the general case in two ways, safer choice test function

Further, some of the functions comprising:

 

Copy the code
    the bitset < . 8> foo ( " 10,011,011 " ); 
 COUT << foo.flip ( 2) << endl; // 10011111 (Flip a function-parameters, the parameters for the bit inversion, line of code will foo subscript 2 the "reverse", i.e. becomes 0 << foo.flip 1,1 COUT becomes 0 () << endl; // 01100000 time (Flip function parameter is not specified, all of the inverted every bitset cout << foo. sET () << endl; // 11111111 time (set function parameter is not specified, all of the bitset each set COUT << foo. 1. sET ( . 3, 0) << endl; // 11110111 (set function specified when the two parameters, the first parameter bit set to the value element of the second parameter, the operation of the bank corresponds foo foo [. 3] << COUT 0 = foo. sET ( . 3) << endl; // 11111111 when (set function only one parameter, the parameter index handled as << foo.reset COUT. 1 ( . 4) << endl; // 11101111 parameter index handled as 0 cout << foo when (reset function is passed an argument.reset () << endl; // 00000000 when (reset function does not pass the parameters every bitset all set to 0
Copy the code

Similarly, they will also check whether the subscript out of bounds, out of bounds if an exception is thrown

Finally, there are some types of conversion function, as follows:

Copy the code
    bitset < . 8> foo ( " 10,011,011 " ); string S = foo.to_string (); // bitset converted into a string type unsigned Long A = foo.to_ulong (); // bitset converted into unsigned long type unsigned Long Long = B foo.to_ullong (); // bitset converted to unsigned long long type S << << COUT endl; // 10,011,011 COUT A << << endl; // << B 155 COUT << endl; // 155

    
Copy the code

 

Guess you like

Origin www.cnblogs.com/FrankChen831X/p/11227308.html
Recommended