-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBitSet.h
More file actions
56 lines (45 loc) · 796 Bytes
/
BitSet.h
File metadata and controls
56 lines (45 loc) · 796 Bytes
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#define _CRT_SECURE_NO_DEPRECATE
#pragma once
#include<iostream>
#include<vector>
using namespace std;
//位图
class BitSet
{
public:
BitSet(const size_t range)
{
_a.resize((range>>5) + 1);
}
void Set(size_t num) //将num记录;
{
size_t index = num >> 5;
size_t pos = num % 32;
_a[index] |= (1 << pos);
}
void Reset(size_t num) //删除num记录
{
size_t index = num >> 5;
size_t pos = num % 32;
_a[index] &= ~(1 << pos);
}
bool Test(size_t num) //判断num是否存在;
{
size_t index = num >> 5;
size_t pos = num % 32;
//判断这这个位,0不存在,1存在;
return _a[index] & (1 << pos);
}
protected:
vector<int> _a;
};
void TestBitSet()
{
BitSet bs(-1);
bs.Set(1);
bs.Set(3);
bs.Set(4);
bs.Set(8);
cout << bs.Test(1) << endl;
cout << bs.Test(10) << endl;
}