# 设计位集
import java.util.ArrayList;
import java.util.Arrays;
import java.util.BitSet;
import java.util.List;
/**
* @author yagol
* @date 上午10:55
* @desc the description of this class
**/
class Bitset {
public static void main(String[] args) {
Bitset bitset = new Bitset(45);
System.out.println(bitset.all());
bitset.flip();
System.out.println(bitset.count());
System.out.println(bitset.all());
System.out.println(bitset.one());
System.out.println(bitset);
}
int[] data;
int count;
boolean reverse = false;
public Bitset(int size) {
data = new int[size];
count = 0;
}
public void fix(int idx) {
if (reverse == false) {
if (data[idx] == 0) {
data[idx] = 1;
count++;
}
} else {
if (data[idx] == 1) {
data[idx] = 0;
count--;
}
}
}
public void unfix(int idx) {
if (reverse == false) {
if (data[idx] == 1) {
data[idx] = 0;
count--;
}
} else {
if (data[idx] == 0) {
data[idx] = 1;
count++;
}
}
}
public void flip() {
reverse = !reverse;
}
public boolean all() {
if (!reverse) {
return count == data.length;
} else {
return count == 0;
}
}
public boolean one() {
if (!reverse) {
return count != 0;
} else {
return data.length - count > 0;
}
}
public int count() {
if (!reverse) {
return count;
} else {
return data.length - count;
}
}
@Override
public String toString() {
StringBuilder res = new StringBuilder();
if (!reverse) {
for (int datum : data) {
res.append(datum);
}
} else {
for (int datum : data) {
if (datum == 1) {
res.append("0");
} else {
res.append("1");
}
}
}
return res.toString();
}
}
//[null,null,null,null,false,null,null,true,null,2,"01010"]
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111