c++ std::bitset
转载自
作用:
及 64位 移位 取或 用64个位存储64个位,取 或 merge 。 然后查索引即知道id是否存在~~ 目标:省空间。
#include <iostream> #include <bitset> #include <string> using namespace std; int main(){ //bitset<n> b;//b有n位,每位都为0 bitset<16> bitvec; cout << bitvec << endl; //bitset<n> b(u); //b是unsigned long型u的一个副本 bitset<16> bitvec2(0xffff); cout << bitvec2 << endl; bitset<128> bitvec3(0xffff); cout << bitvec3 << endl; //bitset<n> b(s); //b是string对象s中含有的位串的副本 string strval("1100"); bitset<32> bitvec4(strval); cout << bitvec4 << endl; string str("1111111000000011001101"); //bitset<n> b(s, pos, n);//b是s中从位置pos开始的n个位的副本 bitset<32> bitvec5(str, 5, 4); cout << bitvec5 << endl; bitset<32> bitvec6(str, str.size() - 4); cout << bitvec6 << endl; cout << sizeof(unsigned long) << endl; //unsigned long a = 1; //unsigned long b = a << 63; uint64_t a = 1; uint64_t b = a << 63; bitset<64> vec(b); cout << vec << endl; return 0; }
output:
0000000000000000 16
1111111111111111 16
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001111111111111111 128
00000000000000000000000000001100 32
00000000000000000000000000001100 32
00000000000000000000000000001101 32
8 1
1000000000000000000000000000000000000000000000000000000000000000 64
-------------------------------------------
#include <iostream>
#include <stdint.h>
#include <bitset>
using namespace std;
int main() {
for(int i=0; i<64; i++){
uint64_t a = 1;
uint64_t b = a << i;
bitset<64> bs(b);
cout << i << " \t" << bs << "," << bs.to_ulong() << endl;
}
bitset<64> sum_bin;
sum_bin.set();
cout << "\t" << sum_bin << "," << sum_bin.to_ulong() << endl;
return 0;
}
0 0000000000000000000000000000000000000000000000000000000000000001,1
1 0000000000000000000000000000000000000000000000000000000000000010,2
2 0000000000000000000000000000000000000000000000000000000000000100,4
3 0000000000000000000000000000000000000000000000000000000000001000,8
4 0000000000000000000000000000000000000000000000000000000000010000,16
5 0000000000000000000000000000000000000000000000000000000000100000,32
6 0000000000000000000000000000000000000000000000000000000001000000,64
7 0000000000000000000000000000000000000000000000000000000010000000,128
8 0000000000000000000000000000000000000000000000000000000100000000,256
9 0000000000000000000000000000000000000000000000000000001000000000,512
10 0000000000000000000000000000000000000000000000000000010000000000,1024
11 0000000000000000000000000000000000000000000000000000100000000000,2048
12 0000000000000000000000000000000000000000000000000001000000000000,4096
13 0000000000000000000000000000000000000000000000000010000000000000,8192
14 0000000000000000000000000000000000000000000000000100000000000000,16384
15 0000000000000000000000000000000000000000000000001000000000000000,32768
16 0000000000000000000000000000000000000000000000010000000000000000,65536
17 0000000000000000000000000000000000000000000000100000000000000000,131072
18 0000000000000000000000000000000000000000000001000000000000000000,262144
19 0000000000000000000000000000000000000000000010000000000000000000,524288
20 0000000000000000000000000000000000000000000100000000000000000000,1048576
21 0000000000000000000000000000000000000000001000000000000000000000,2097152
22 0000000000000000000000000000000000000000010000000000000000000000,4194304
23 0000000000000000000000000000000000000000100000000000000000000000,8388608
24 0000000000000000000000000000000000000001000000000000000000000000,16777216
25 0000000000000000000000000000000000000010000000000000000000000000,33554432
26 0000000000000000000000000000000000000100000000000000000000000000,67108864
27 0000000000000000000000000000000000001000000000000000000000000000,134217728
28 0000000000000000000000000000000000010000000000000000000000000000,268435456
29 0000000000000000000000000000000000100000000000000000000000000000,536870912
30 0000000000000000000000000000000001000000000000000000000000000000,1073741824
31 0000000000000000000000000000000010000000000000000000000000000000,2147483648
32 0000000000000000000000000000000100000000000000000000000000000000,4294967296
33 0000000000000000000000000000001000000000000000000000000000000000,8589934592
34 0000000000000000000000000000010000000000000000000000000000000000,17179869184
35 0000000000000000000000000000100000000000000000000000000000000000,34359738368
36 0000000000000000000000000001000000000000000000000000000000000000,68719476736
37 0000000000000000000000000010000000000000000000000000000000000000,137438953472
38 0000000000000000000000000100000000000000000000000000000000000000,274877906944
39 0000000000000000000000001000000000000000000000000000000000000000,549755813888
40 0000000000000000000000010000000000000000000000000000000000000000,1099511627776
41 0000000000000000000000100000000000000000000000000000000000000000,2199023255552
42 0000000000000000000001000000000000000000000000000000000000000000,4398046511104
43 0000000000000000000010000000000000000000000000000000000000000000,8796093022208
44 0000000000000000000100000000000000000000000000000000000000000000,17592186044416
45 0000000000000000001000000000000000000000000000000000000000000000,35184372088832
46 0000000000000000010000000000000000000000000000000000000000000000,70368744177664
47 0000000000000000100000000000000000000000000000000000000000000000,140737488355328
48 0000000000000001000000000000000000000000000000000000000000000000,281474976710656
49 0000000000000010000000000000000000000000000000000000000000000000,562949953421312
50 0000000000000100000000000000000000000000000000000000000000000000,1125899906842624
51 0000000000001000000000000000000000000000000000000000000000000000,2251799813685248
52 0000000000010000000000000000000000000000000000000000000000000000,4503599627370496
53 0000000000100000000000000000000000000000000000000000000000000000,9007199254740992
54 0000000001000000000000000000000000000000000000000000000000000000,18014398509481984
55 0000000010000000000000000000000000000000000000000000000000000000,36028797018963968
56 0000000100000000000000000000000000000000000000000000000000000000,72057594037927936
57 0000001000000000000000000000000000000000000000000000000000000000,144115188075855872
58 0000010000000000000000000000000000000000000000000000000000000000,288230376151711744
59 0000100000000000000000000000000000000000000000000000000000000000,576460752303423488
60 0001000000000000000000000000000000000000000000000000000000000000,1152921504606846976
61 0010000000000000000000000000000000000000000000000000000000000000,2305843009213693952
62 0100000000000000000000000000000000000000000000000000000000000000,4611686018427387904
63 1000000000000000000000000000000000000000000000000000000000000000,9223372036854775808
1111111111111111111111111111111111111111111111111111111111111111,18446744073709551615