### Introduction to bitset-personal

I can’t find a accurate explanation online.So there is my understanding of it.
Bitset is a STL which is easy to treat a number as a binary.

### Instructions

It is included by include<bitset>
If you want to define a bitset,the code is as follows

bitset<num> n;//num must be defined as an integer literal constant or an integer type const object initialized with a constant value.


If so initialized, each bit is 0.
Of course you can also define it like this

int a;
bitset<num> n(a);


now,the bitset n is the binary form of a.And you even can use this like a array.
Such as

#include <iostream>
#include <bitset>
using namespace std;
int main()
{
int a;
cin>>a;
bitset<4> b(a);
cout<<b<<endl;
cout<<b[0]<<endl<<b[1]<<endl<<b[2]<<endl<<b[3]<<endl;
return 0;
}

Input:
13 -5
Ouput:
1101
1
0
1
1

1011
1
1
0
1


As can be seen from these examples, when the bit position in the bitset is operated, it is reversed.
Such as:

int n = 16;
bitset<64> b(n);
b[0]=0
b[1]=0
b[2]=0
b[3]=0
b[4]=1


From the above example we can see that the negative number will directly become the complement form under the action of bitset.This has advantages and disadvantages.This has advantages and disadvantages, because some places just let you represent the binary form, no need to complement.If you meet a situtation like this,just take a judge,all numbers are processed as positive numbers, if they are negative, then add a negative sign.I’d like to paste a example here.

#### Example

Source: 简单的二进制
Description：

Input

Output

Examples
input
10
15
output
2
4
Note

Problem solving：
We can see this problem need the complement when the number is negative.So the bitset is so comfortable for this.

Code:

#include<bits/stdc++.h>
using namespace std;
int main()
{
long long n;
while(scanf("%lld",&n)!=EOF)
{
bitset<64> b(n);
cout<<b.count()<<endl;
}
return 0;
}


If you write this, the code will be very simple.The specific function functions will be discussed below.

### Many functions

Almost all STL have lots of functions,bitset is no exception.
I don’t have the ability, and I don’t have the energy to list them all, so I will list what I like and what I find useful.

#### Overview

Define a bitset first:
bitset<64> b;

function Features
b.any() Is there a binary bit with a value of 1 in b?
b.none() Is there no binary bit with a value of 1 in b?
b.count() The number of bits in b with a value of 1
b[pos] Access the binary bit at pos in b
b.test(pos) Whether the binary bit in pos is 1 in b？
b.set() Set all binary bits in b to 1
b.set(pos) Put the binary position in b at pos as 1
b.reset() Set all binary bits in b to 0
b.reset(pos) Put the binary position in b at pos to 0
b.flip() All binary bits in b are inverted bit by bit
b.flip(pos) Invert the binary bit at b in pos
b.to_string() Convert the contents of the bitset to string
b.to_ulong() Convert the contents of the bitset to unsigned long

Next I will help you understand the usage of these functions with some examples.

#### b.any() and b.none() and b.count()

b.any(): Is there a binary bit with a value of 1 in b?

#include<iostream>
#include<bitset>
using namespace std;
int main()
{
int n=5,m=0;// n = 0101, m = 0
bitset<8> b1(n);
bitset<8> b2(m);
cout<<b1.none()<<" "<<b2.none();
}
Operation result:
1 0


So you can see that the return value of b.any() is the bool type.

b.none()：Is there no binary bit with a value of 1 in b?

#include<iostream>
#include<bitset>
using namespace std;
int main()
{
int n=5,m=0;// n = 0101, m = 0
bitset<8> b1(n);
bitset<8> b2(m);
cout<<endl<<b1.none()<<" "<<b2.none();
}
Operation result:
0 1


So you can see that the return value of b.none() also is the bool type.

b.count(): The number of bits in b with a value of 1

#include<iostream>
#include<bitset>
using namespace std;
int main()
{
int n=15,m=16;// n = 1111, m = 10000
bitset<8> b1(n);
bitset<8> b2(m);
cout<<b1.count()<<" "<<b2.count();
}
Operation result:
4 1


So the function of b.count() is to return the number of 1 in the binary.Just like the example above

#### b[pos]

b[pos]: Access the binary bit at pos in b
I started the function’s introduction at the beginning of this blog, you can look up.

#### b.test(pos)

b.test(pos): Whether the binary bit in pos is 1 in b？

#include<iostream>
#include<bitset>
using namespace std;
int main()
{
int n=15,m=16;// n = 1111, m = 10000
bitset<8> b1(n);
bitset<8> b2(m);
cout<<b1.test(1)<<" "<<b2.test(4)<<" "<<b2.test(1);
}
Operation result:
1 1 0


So you can see that the return value of b.test(pos) is the bool type,too.

#### b.set() and b.set(pos)

b.set(): Set all binary bits in b to 1

#include<iostream>
#include<bitset>
using namespace std;
int main()
{
int n = 16;
bitset<8> b(n);
cout<<b<<endl;
b.set();
cout<<b;
}
Operation result:
00010000
11111111


b.set(pos): Put the binary position in b at pos as 1

#include<iostream>
#include<bitset>
using namespace std;
int main()
{
int n = 16;
bitset<8> b(n);
cout<<b<<endl;
b.set(0);
cout<<b;
}
Operation result:
00010000
00010001


#### b.reset() and b.reset(pos)

b.reset(): Set all binary bits in b to 0
b.reset(pos): Put the binary position in b at pos to 0
These two functions are very similar to the two above, so I won’t go into details.

#### b.flip() and b.flip(pos)

b.flip(): All binary bits in b are inverted bit by bit

#include<iostream>
#include<bitset>
using namespace std;
int main()
{
int n = 16;
bitset<8> b(n);
cout<<b<<endl;
b.flip();
cout<<b;
}
Operation result:
00010000
11101111


b.flip(pos): Invert the binary bit at b in pos
Basically the same as above, no longer repeat

#### b.to_string()

b.to_string(): Convert the contents of the bitset to string

#include<iostream>
#include<bitset>
using namespace std;
int main()
{
int n = 16;
bitset<8> b(n);
string s;
s=b.to_string();
cout<<s<<endl;
}
Operation result:
00010000


#### b.to_ulong()

#include<iostream>
#include<bitset>
using namespace std;
int main()
{
int n = 16;
bitset<5> b(n);
b.flip();
long a;
cout<<b<<endl;
a=b.to_ulong();
cout<<a<<endl;
}
Operation result:
01111
15