-
Notifications
You must be signed in to change notification settings - Fork 0
/
BitVector.java
56 lines (49 loc) · 1.06 KB
/
BitVector.java
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
package CtCILibrary;
public class BitVector {
private static int DATA_SIZE = 32;
private int length;
private int[] vector;
public BitVector(int length) {
this.length = length;
if (length % DATA_SIZE == 0) {
vector = new int[length / DATA_SIZE];
} else {
vector = new int[length / DATA_SIZE + 1];
}
}
public int length() {
return length;
}
public boolean get(int i) {
int b = vector[i / DATA_SIZE];
int bit_index = i % DATA_SIZE;
if (((b >> bit_index) & 1) == 1) {
return true;
} else {
return false;
}
}
public void print() {
for (int k : vector) {
for (int i = 0; i < DATA_SIZE; i++) {
if ((k >> i & 1) == 1) {
System.out.print(1);
} else {
System.out.print(0);
}
}
System.out.println();
}
}
public void set(int i, boolean flag) {
if (i >= 0 && i < length) {
int mask = ~(1 << i);
int b = vector[i / DATA_SIZE] & mask;
if (flag) {
vector[i / DATA_SIZE] = b | (1 << i);
} else {
vector[i / DATA_SIZE] = b;
}
}
}
}