-
Notifications
You must be signed in to change notification settings - Fork 112
/
Solution.java
49 lines (39 loc) · 1.24 KB
/
Solution.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
/**
* @author Oleg Cherednik
* @since 14.02.2019
*/
public class Solution {
public static void main(String... args) {
BitArray bitArray = new BitArray(16);
bitArray.set(5, true);
bitArray.set(6, true);
bitArray.set(7, true);
bitArray.set(9, true);
bitArray.set(13, true);
bitArray.set(15, true);
for (int i = 0; i < 16; i++)
System.out.println(i + ":" + bitArray.get(i));
}
public static final class BitArray {
private static final int INDEX_LENGTH = 8; // size of int
private final int[] arr;
public BitArray(int size) {
arr = new int[size / INDEX_LENGTH + (size % INDEX_LENGTH != 0 ? 1 : 0)];
}
public void set(int i, boolean val) {
if (val)
arr[getArrayIndex(i)] |= getBitMask(i);
else
arr[getArrayIndex(i)] &= ~getBitMask(i);
}
public boolean get(int i) {
return (arr[getArrayIndex(i)] & getBitMask(i)) != 0;
}
private static int getArrayIndex(int i) {
return i / INDEX_LENGTH;
}
private static int getBitMask(int i) {
return 1 << i % INDEX_LENGTH;
}
}
}