-
Notifications
You must be signed in to change notification settings - Fork 15
/
bitset.cpp
31 lines (25 loc) · 690 Bytes
/
bitset.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <bitset>
using namespace std;
int main() {
//the index starts from the least significant bit
//initialize 0th bit as 1
bitset<5> bs(1);
cout << "count: " << bs.count() <<
", str: " << bs.to_string() <<
", long: " << bs.to_ulong() << endl;
bs.flip(2);
cout << "count: " << bs.count() <<
", str: " << bs.to_string() <<
", long: " << bs.to_ulong() << endl;
bs.flip(4);
cout << "count: " << bs.count() <<
", str: " << bs.to_string() <<
", long: " << bs.to_ulong() << endl;
return 0;
}
/*
count: 1, str: 00001, long: 1
count: 2, str: 00101, long: 5
count: 3, str: 10101, long: 21
*/