1
2
3
4
5
6 package nl.tudelft.simulation.language.util;
7
8 import java.io.Serializable;
9 import java.math.BigInteger;
10 import java.util.BitSet;
11
12 /***
13 * Utilities for the bitset class.
14 * <p>
15 * (c) copyright 2002-2005 <a href="http://www.simulation.tudelft.nl">Delft University of Technology </a>, the
16 * Netherlands.
17 * <p>
18 * See for project information <a
19 * href="http://www.simulation.tudelft.nl/dsol/language">www.simulation.tudelft.nl/language </a> <br>
20 * License of use: <a href="http://www.gnu.org/copyleft/lesser.html">Lesser General Public License (LGPL)
21 * </a>, no warranty
22 *
23 * @author <a href="http://www.peter-jacobs.com/index.htm">Peter Jacobs </a>
24 * @version $Revision: 1.7 $ $Date: 2005/07/04 12:21:25 $
25 * @since 1.5
26 */
27 public final class BitUtil implements Serializable
28 {
29 /***
30 * constructs a new BitUtil.
31 */
32 private BitUtil()
33 {
34 super();
35 }
36
37 /***
38 * @param bits
39 * the bitset to convert
40 * @return Returns a byte array of at least length 1. The most significant bit in the result is guaranteed
41 * not to be a 1 (since BitSet does not support sign extension). The byte-ordering of the result
42 * is big-endian which means the most significant bit is in element 0. The bit at index 0 of the
43 * bit set is assumed to be the least significant bit.
44 */
45 public static byte[] toByteArray(final BitSet bits)
46 {
47 synchronized (bits)
48 {
49 byte[] bytes = new byte[bits.length() / 8 + 1];
50 for (int i = 0; i < bits.length(); i++)
51 {
52 if (bits.get(i))
53 {
54 bytes[bytes.length - i / 8 - 1] |= 1 << (i % 8);
55 }
56 }
57 return bytes;
58 }
59 }
60
61 /***
62 * @param bits
63 * the bitset to convert
64 * @param length
65 * the length of the set
66 * @return Returns an int. The most significant bit in the result is guaranteed not to be a 1 (since
67 * BitSet does not support sign extension). The int-ordering of the result is big-endian which
68 * means the most significant bit is in element 0. The bit at index 0 of the bit set is assumed to
69 * be the least significant bit.
70 */
71 public static int toInt(final BitSet bits, final int length)
72 {
73 byte[] bytes = BitUtil.toByteArray(bits);
74 int value = new BigInteger(bytes).intValue();
75 if (value > Math.pow(2, length - 1) && length != -1)
76 {
77 value = value - (int) Math.pow(2, length);
78 }
79 return value;
80 }
81
82 /***
83 * constructs a new BitSet from a string in the "110110" format.
84 *
85 * @param value
86 * the value
87 * @return the BitSet
88 */
89 public static BitSet fromString(String value)
90 {
91 if (!value.startsWith("{"))
92 {
93 BitSet set = new BitSet(value.length());
94 for (int i = 0; i < value.length(); i++)
95 {
96 if (value.charAt(i) == '1')
97 {
98 set.set(i, true);
99 }
100 else if (value.charAt(i) == '0')
101 {
102 set.set(i, false);
103 }
104 else
105 {
106 throw new IllegalArgumentException("value should only contain ones and zeros. Try 110011");
107 }
108 }
109 return set;
110 }
111 BitSet set = new BitSet();
112 value = value.substring(1, value.length() - 1);
113 if (value.equals(""))
114 {
115 return set;
116 }
117 String[] bits = value.split(",");
118 for (int i = 0; i < bits.length; i++)
119 {
120 bits[i] = bits[i].trim();
121 set.set(Integer.valueOf(bits[i]).intValue());
122 }
123 return set;
124 }
125
126 /***
127 * @param bytes
128 * the byteArray
129 * @return Returns a bitset containing the values in bytes.The byte-ordering of bytes must be big-endian
130 * which means the most significant bit is in element 0.
131 */
132 public static BitSet fromByteArray(final byte[] bytes)
133 {
134 BitSet bits = new BitSet();
135 for (int i = 0; i < bytes.length * 8; i++)
136 {
137 if ((bytes[bytes.length - i / 8 - 1] & (1 << (i % 8))) > 0)
138 {
139 bits.set(i);
140 }
141 }
142 return bits;
143 }
144
145 /***
146 * returns the bitset of an integer value.
147 *
148 * @param value
149 * the value
150 * @param length
151 * the length of the bitSet to produce
152 * @return the BitSet
153 */
154 public static BitSet fromInt(final int value, final int length)
155 {
156 return BitUtil.fromInteger(new Integer(value), length);
157 }
158
159 /***
160 * returns the bitset of an integer value.
161 *
162 * @param value
163 * the value
164 * @param length
165 * the length of
166 * @return the BitSet
167 */
168 public static BitSet fromInteger(Integer value, final int length)
169 {
170 if (value.intValue() < 0 && length != -1)
171 {
172 value = new Integer((int) Math.pow(2, length) + value.intValue());
173 }
174 return BitUtil.fromByteArray(new BigInteger(value.toString()).toByteArray());
175 }
176
177 /***
178 * returns a one-size BitSet with value.
179 *
180 * @param value
181 * the value of the bitSet
182 * @return the BitSet
183 */
184 public static BitSet fromBoolean(final boolean value)
185 {
186 BitSet result = new BitSet(1);
187 result.set(0, value);
188 return result;
189 }
190 }