1 package nl.tudelft.simulation.language.util;
2
3 import java.io.Serializable;
4 import java.math.BigInteger;
5 import java.util.BitSet;
6
7 import org.djutils.exceptions.Throw;
8
9
10
11
12
13
14
15
16
17
18
19
20
21 public final class BitUtil implements Serializable
22 {
23
24 private static final long serialVersionUID = 1L;
25
26
27 private BitUtil()
28 {
29
30 }
31
32
33
34
35
36
37
38
39 public static byte[] toByteArray(final BitSet bits)
40 {
41 synchronized (bits)
42 {
43 byte[] bytes = new byte[bits.length() / 8 + 1];
44 for (int i = 0; i < bits.length(); i++)
45 {
46 if (bits.get(i))
47 {
48 bytes[bytes.length - i / 8 - 1] |= 1 << (i % 8);
49 }
50 }
51 return bytes;
52 }
53 }
54
55
56
57
58
59
60
61 public static BitSet fromInt(final int value, final int length)
62 {
63 return BitUtil.fromInteger(Integer.valueOf(value), length);
64 }
65
66
67
68
69
70
71
72 public static BitSet fromInteger(final Integer value, final int length)
73 {
74 Throw.when(length <= 0, IllegalArgumentException.class, "BitUtil.fromInt should have a positive number of bits");
75 Throw.when(length > 31, IllegalArgumentException.class, "BitUtil.fromInt can have maximum 31 bits");
76 Throw.when(value.intValue() < 0, IllegalArgumentException.class, "BitUtil.fromInt can have only positive values");
77 return BitUtil.fromByteArray(new BigInteger(value.toString()).toByteArray());
78 }
79
80
81
82
83
84
85
86
87 public static int toInt(final BitSet bits, final int length)
88 {
89 Throw.when(length <= 0, IllegalArgumentException.class, "BitUtil.toInt should have a positive number of bits");
90 Throw.when(length > 31, IllegalArgumentException.class, "BitUtil.toInt can have maximum 31 bits");
91 byte[] bytes = BitUtil.toByteArray(bits);
92 return new BigInteger(bytes).intValue();
93 }
94
95
96
97
98
99
100
101 public static BitSet fromString(final String value)
102 {
103 if (!value.trim().startsWith("{"))
104 {
105 BitSet set = new BitSet(value.length());
106 for (int i = 0; i < value.length(); i++)
107 {
108 if (value.charAt(i) == '1')
109 {
110 set.set(i, true);
111 }
112 else if (value.charAt(i) == '0')
113 {
114 set.set(i, false);
115 }
116 else
117 {
118 throw new IllegalArgumentException("value should only contain ones and zeros. Try 110011");
119 }
120 }
121 return set;
122 }
123 BitSet set = new BitSet();
124 String array = value.trim();
125 if (!array.endsWith("}"))
126 {
127 throw new IllegalArgumentException("value that starts with { should end with }");
128 }
129 array = array.substring(1, array.length() - 1).trim();
130 if (array.length() == 0)
131 {
132 return set;
133 }
134 String[] bits = array.split(",");
135 for (int i = 0; i < bits.length; i++)
136 {
137 bits[i] = bits[i].trim();
138 set.set(Integer.valueOf(bits[i]).intValue());
139 }
140 return set;
141 }
142
143
144
145
146
147
148 public static BitSet fromByteArray(final byte[] bytes)
149 {
150 BitSet bits = new BitSet();
151 for (int i = 0; i < bytes.length * 8; i++)
152 {
153 if ((bytes[bytes.length - i / 8 - 1] & (1 << (i % 8))) > 0)
154 {
155 bits.set(i);
156 }
157 }
158 return bits;
159 }
160
161
162
163
164
165
166 public static BitSet fromBoolean(final boolean value)
167 {
168 BitSet result = new BitSet(1);
169 result.set(0, value);
170 return result;
171 }
172 }