-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbitvector.cpp
More file actions
58 lines (45 loc) · 1.09 KB
/
bitvector.cpp
File metadata and controls
58 lines (45 loc) · 1.09 KB
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
57
58
#include <iostream>
#include <vector>
#include <bitset>
using namespace std;
class bitvector : public vector<unsigned char> {
int size_bits;
public:
bitvector(){size_bits=0;}
void push_back(bool v){
if (size_bits==vector<unsigned char>::size()*8){
vector<unsigned char>::push_back(0);
}
int idx=size_bits/8;
int j=size_bits%8;
if (v){
data()[idx]=(data()[idx] |(1<<j));
}
cout << "v" << v << " idx:" << idx << "j: " << j<< " data:"<< std::bitset<8>(data()[idx]) << "\n";
size_bits++;
}
bool operator [](int indx){
int idx=indx/8;
int j=indx%8;
return (data()[idx] & (1<<j));
}
int size(){
return size_bits;
}
};
int main(){
bitvector v;
for(int i=0;i<10;i++){
v.push_back(false);
v.push_back(true);
v.push_back(false);
v.push_back(false);
v.push_back(false);
v.push_back(false);
v.push_back(false);
v.push_back(false);
}
for(int i=0;i<v.size();i++){
cout << i << " "<<v[i] <<endl;
}
}