"STL" bitset Story

Foreword


Before that need to address the subject of transfer binary I see a lot of big brothers with a bitset.

However, I do not this thing. It looks very sophisticated look ......

Change the title changed tired to learn about 233.

text


A, bitset construction

There are three ways bitset structure: string construction, construction and non-integer argument constructor (own blind yy name)

1. The no-argument constructor: every default is 0.

Specific code implementation:

bitset <10> a;

Construction Results: 0000000000

2.string structure: char array configured with substantially the same. 01 is also attached a string passed as an argument bitset structure.

Specific code implementation:

string str="010110";
bitset <10> b(str);

Construction Results: 0000010110

3. integer structure: passing an integer, bitset will be automatically converted into a binary number to save.

Specific code implementation:

bitset <10> c(12);

Construction Results: 0000001100

Second, the basic operation of bitset

bitset support all bit operations. Here no one by one demonstration. As an example or different:

Specific code implementation:

bitset <20> a(string("10101101"));
bitset <20> b(string("10101100"));
cout<<(a^b)<<endl;

Output: 00000000000000000001

Three, bitset access and assignment

1.bitset value of each bit can be accessed through the $ "[" "]" $.

Specific code implementation:

bitset <10> a("1011");
cout<<a[1]<< endl;//输出1

It can also be assigned to a certain position by this method.

2. bitset comes with access function test (and is said to avoid the above access methods than access cross-border issues)

Specific code implementation:

bitset <10> a("1011");
cout<<a.test(0)<<endl;

3. By bitset own function set, reset assignment

When no parameters set all bits all set to 1, reset the contrary. Parameters for the position. +1 position will be added to the corresponding parameter set to 0/1.

Further, when the time set function specified two parameters, meaning the first element is set to +1 bit parameter value of the second parameter.

Specific code implementation:

bitset <10> a;
a.set();    //1111111111
a.reset();  //0000000000
a.set(3);   //0000001000
a.set(4);   //0000011000
a.reset(3); //0000010000
a.set(4,0); //0000000000

Not measured but is probably so be it ...... (escape

Fourth, other basic functions

count (): returns the number 1,

any (): Returns whether there is a

none (): Returns whether the no 1

flip (): all negated

flip (p): the first bit inversion p + 1

to_ulong (): returns it is converted to unsigned int result, if the error is out of range

to_ullong (): returns it to convert Long unsigned Long a result, if the error is out of range

to_string():返回它转换为string的结果

Guess you like

Origin www.cnblogs.com/xingmi-weiyouni/p/11496504.html