| CODENOTIFIER | HelpYou are not signed inSign in |
Project: Flanderra
Revision: 21
Author: pcherkas
Date: 31 Oct 2008 14:01:30
Changes:bit logic refactoring and optimization.
Files:| ... | ...@@ -1,10 +1,10 @@ | |
| 1 | 1 | package com.flanderra.commons.test.bits; |
| 2 | 2 | |
| 3 | import java.text.DecimalFormat; | |
| 4 | import java.text.MessageFormat; | |
| 5 | import java.text.NumberFormat; | |
| 3 | import java.util.Arrays; | |
| 6 | 4 | import java.util.BitSet; |
| 5 | import java.util.Random; | |
| 7 | 6 | |
| 7 | import org.apache.commons.jexl.junit.Asserter; | |
| 8 | 8 | import org.apache.commons.logging.Log; |
| 9 | 9 | import org.apache.commons.logging.LogFactory; |
| 10 | 10 | import org.junit.Assert; |
| ... | ...@@ -14,58 +14,94 @@ | |
| 14 | 14 | import com.flanderra.commons.utils.BitUtils; |
| 15 | 15 | import com.flanderra.commons.utils.Bits; |
| 16 | 16 | |
| 17 | import junit.framework.TestCase; | |
| 18 | ||
| 19 | 17 | public class BitUtilsTest { |
| 20 | 18 | private Log log = LogFactory.getLog(BitUtilsTest.class); |
| 21 | 19 | |
| 22 | @Test | |
| 20 | @Ignore | |
| 23 | 21 | public void testBinHex() { |
| 24 | String ptnHex = "00 aa AA 0b 0B"; | |
| 25 | String ptnBin = "00000000 01010101 01010101 00110011 00110011"; | |
| 26 | ||
| 22 | String ptnHex = "00 01 02 03 04 05"; | |
| 23 | // String ptnBin = "00000000 01010101 01010101 00110011 00110011"; | |
| 24 | ||
| 27 | 25 | byte[] res1 = BitUtils.hex(ptnHex); |
| 28 | 26 | byte[] res2 = BitUtils._hex(ptnHex); |
| 29 | ||
| 27 | ||
| 30 | 28 | String reverse1 = BitUtils.hex(res1); |
| 31 | 29 | String reverse2 = BitUtils._hex(res2); |
| 32 | ||
| 30 | ||
| 33 | 31 | Assert.assertArrayEquals(res1, res2); |
| 34 | 32 | Assert.assertEquals(reverse1.trim(), reverse2.trim()); |
| 35 | ||
| 33 | ||
| 36 | 34 | reverse1 = BitUtils.bin(res1); |
| 37 | 35 | reverse2 = BitUtils._bin(res2); |
| 38 | 36 | Assert.assertEquals(reverse1.trim(), reverse2.trim()); |
| 39 | ||
| 37 | ||
| 40 | 38 | BitSet bits1 = BitUtils.bytesToBits(res1); |
| 41 | int offset = 36; | |
| 42 | 39 | Bits bits2 = new Bits(); |
| 43 | 40 | bits2.setData(res2); |
| 44 | bits2.setOffset(offset); | |
| 45 | reverse1 = BitUtils.bin(bits1, offset); | |
| 46 | reverse2 = BitUtils._bin(bits2); | |
| 47 | Assert.assertEquals(reverse1.trim(), reverse2.trim()); | |
| 41 | ||
| 42 | for (int i = 0; i < 40; i++) { | |
| 43 | int offset = i; | |
| 44 | bits2.setOffset(offset); | |
| 45 | reverse1 = BitUtils.bin(bits1, offset); | |
| 46 | reverse2 = BitUtils._bin(bits2); | |
| 47 | ||
| 48 | Assert.assertEquals(reverse1.trim(), reverse2.trim()); | |
| 49 | } | |
| 48 | 50 | } |
| 49 | ||
| 50 | public String bin(byte[] bytes) { | |
| 51 | StringBuffer sb = new StringBuffer(""); | |
| 52 | for (int i = 0; i < bytes.length; i++) { | |
| 53 | byte val = bytes[i]; | |
| 54 | sb.append(new char[] { (1 & (val >> 7)) == 1 ? '1' : '0', (1 & (val >> 6)) == 1 ? '1' : '0', | |
| 55 | (1 & (val >> 5)) == 1 ? '1' : '0', (1 & (val >> 4)) == 1 ? '1' : '0', (1 & (val >> 3)) == 1 ? '1' : '0', | |
| 56 | (1 & (val >> 2)) == 1 ? '1' : '0', (1 & (val >> 1)) == 1 ? '1' : '0', (1 & (val)) == 1 ? '1' : '0' }); | |
| 57 | sb.append(' '); | |
| 51 | ||
| 52 | @Ignore | |
| 53 | public void testBitIntegers() { | |
| 54 | Random rint = new Random(); | |
| 55 | for (long testValue = 0, i = 0; i <= 100000; testValue = rint.nextLong(), i++) { | |
| 56 | BitSet result1 = BitUtils.longToBits(testValue, 64); | |
| 57 | Bits result2 = BitUtils._longToBits(testValue, 64); | |
| 58 | ||
| 59 | String reverse1 = BitUtils.bin(result1, 64); | |
| 60 | String reverse2 = BitUtils._bin(result2); | |
| 61 | Assert.assertEquals(reverse1.trim(), reverse2.trim()); | |
| 62 | ||
| 63 | Assert.assertEquals(testValue, BitUtils._bitsToLong(result2, false)); | |
| 64 | Assert.assertEquals(testValue, BitUtils._bitsToLong(result2, true)); | |
| 65 | Assert.assertEquals(BitUtils._bitsToLong(result2, 0, 64, false), BitUtils._bitsToLong(result2, false)); | |
| 66 | Assert.assertEquals(BitUtils._bitsToLong(result2, 0, 64, true), BitUtils._bitsToLong(result2, true)); | |
| 58 | 67 | } |
| 59 | return sb.toString(); | |
| 60 | 68 | } |
| 61 | ||
| 62 | public String bin(byte val) { | |
| 63 | return new String(new char[] { (1 & (val >> 7)) == 1 ? '1' : '0', (1 & (val >> 6)) == 1 ? '1' : '0', | |
| 64 | (1 & (val >> 5)) == 1 ? '1' : '0', (1 & (val >> 4)) == 1 ? '1' : '0', (1 & (val >> 3)) == 1 ? '1' : '0', | |
| 65 | (1 & (val >> 2)) == 1 ? '1' : '0', (1 & (val >> 1)) == 1 ? '1' : '0', (1 & (val)) == 1 ? '1' : '0' }); | |
| 69 | ||
| 70 | @Ignore | |
| 71 | public void testMaxBitCounts() { | |
| 72 | Random rint = new Random(); | |
| 73 | long[] longValues = new long[] { rint.nextLong(), rint.nextLong(), rint.nextLong(), rint.nextLong(), rint.nextLong(), | |
| 74 | rint.nextLong(), rint.nextLong() }; | |
| 75 | Assert.assertEquals(BitUtils.calculateMaxBitCountSI(longValues), _calculateMaxBitCountSI(longValues)); | |
| 76 | } | |
| 77 | ||
| 78 | @Test | |
| 79 | public void testFlipBytes() { | |
| 80 | ||
| 81 | ||
| 66 | 82 | } |
| 67 | 83 | |
| 68 | public int getBit(byte val, int pos) { | |
| 69 | return (1 & (val >> pos)); | |
| 84 | public Bits _flipBytes(Bits val){ | |
| 85 | return val; | |
| 70 | 86 | } |
| 87 | ||
| 88 | public static int _calculateMaxBitCountSI(long[] data) { | |
| 89 | for (int i = 0; i < data.length; i++) { | |
| 90 | if (data[i] < 0) { | |
| 91 | data[i] = (data[i] ^ -1) + 1; | |
| 92 | } | |
| 93 | } | |
| 94 | Arrays.sort(data); | |
| 95 | long maximum = data[data.length - 1]; | |
| 96 | if (maximum == 0) { | |
| 97 | return 0; | |
| 98 | } else { | |
| 99 | int result = 0; | |
| 100 | for(int i = 1; i<=64; i++){ | |
| 101 | //TODO: implement this | |
| 102 | } | |
| 103 | return result; | |
| 104 | } | |
| 105 | } | |
| 106 | ||
| 71 | 107 | } |
| ... | ...@@ -43,6 +43,40 @@ | |
| 43 | 43 | public static final double MIN_FLOAT64 = Double.MIN_VALUE; |
| 44 | 44 | public static final double MAX_FLOAT64 = Double.MAX_VALUE; |
| 45 | 45 | private static final char[] hexChars = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }; |
| 46 | ||
| 47 | public static final int[] bytenums = new int[]{ | |
| 48 | 0, 0, 0, 0, 0, 0, 0, 0, | |
| 49 | 1, 1, 1, 1, 1, 1, 1, 1, | |
| 50 | 2, 2, 2, 2, 2, 2, 2, 2, | |
| 51 | 3, 3, 3, 3, 3, 3, 3, 3, | |
| 52 | 4, 4, 4, 4, 4, 4, 4, 4, | |
| 53 | 5, 5, 5, 5, 5, 5, 5, 5, | |
| 54 | 6, 6, 6, 6, 6, 6, 6, 6, | |
| 55 | 7, 7, 7, 7, 7, 7, 7, 7 | |
| 56 | }; | |
| 57 | ||
| 58 | public static final int[] bitnums = new int[]{ | |
| 59 | 0, 1, 2, 3, 4, 5, 6, 7, | |
| 60 | 0, 1, 2, 3, 4, 5, 6, 7, | |
| 61 | 0, 1, 2, 3, 4, 5, 6, 7, | |
| 62 | 0, 1, 2, 3, 4, 5, 6, 7, | |
| 63 | 0, 1, 2, 3, 4, 5, 6, 7, | |
| 64 | 0, 1, 2, 3, 4, 5, 6, 7, | |
| 65 | 0, 1, 2, 3, 4, 5, 6, 7, | |
| 66 | 0, 1, 2, 3, 4, 5, 6, 7 | |
| 67 | }; | |
| 68 | ||
| 69 | public static final int[] reversebitnums = new int[]{ | |
| 70 | 7, 6, 5, 4, 3, 2, 1, 0, | |
| 71 | 7, 6, 5, 4, 3, 2, 1, 0, | |
| 72 | 7, 6, 5, 4, 3, 2, 1, 0, | |
| 73 | 7, 6, 5, 4, 3, 2, 1, 0, | |
| 74 | 7, 6, 5, 4, 3, 2, 1, 0, | |
| 75 | 7, 6, 5, 4, 3, 2, 1, 0, | |
| 76 | 7, 6, 5, 4, 3, 2, 1, 0, | |
| 77 | 7, 6, 5, 4, 3, 2, 1, 0 | |
| 78 | }; | |
| 79 | ||
| 46 | 80 | |
| 47 | 81 | // /////////////////////////////////////////// |
| 48 | 82 | // utility methods for basic types |
| ... | ...@@ -52,89 +86,189 @@ | |
| 52 | 86 | BitSet bitset = longToBits(val, 8); |
| 53 | 87 | return bitset; |
| 54 | 88 | } |
| 89 | ||
| 90 | public static Bits _bitsUI8(long val) { | |
| 91 | //TODO: implement this | |
| 92 | throw new IllegalStateException("not implemented yet"); | |
| 93 | } | |
| 55 | 94 | |
| 56 | 95 | public static byte[] bytesUI8(long val) { |
| 57 | 96 | return bitsToBytes(bitsUI8(val), 8); |
| 58 | 97 | } |
| 59 | 98 | |
| 99 | public static byte[] _bytesUI8(long val) { | |
| 100 | //TODO: implement this | |
| 101 | throw new IllegalStateException("not implemented yet"); | |
| 102 | } | |
| 103 | ||
| 60 | 104 | public static long parseUI8(byte[] val) { |
| 61 | 105 | return bytesToLong(new byte[] { val[0] }, false); |
| 62 | 106 | } |
| 63 | 107 | |
| 108 | public static long _parseUI8(byte[] val) { | |
| 109 | //TODO: implement this | |
| 110 | throw new IllegalStateException("not implemented yet"); | |
| 111 | } | |
| 112 | ||
| 64 | 113 | public static BitSet bitsUI16(long val) { |
| 65 | 114 | Assert.inRange(val, MIN_UI16, MAX_UI16); |
| 66 | 115 | return flipBytes(longToBits(val, 16), 2); |
| 67 | 116 | } |
| 68 | 117 | |
| 118 | public static Bits _bitsUI16(long val) { | |
| 119 | //TODO: implement this | |
| 120 | throw new IllegalStateException("not implemented yet"); | |
| 121 | } | |
| 122 | ||
| 69 | 123 | public static byte[] bytesUI16(long val) { |
| 70 | 124 | return bitsToBytes(bitsUI16(val), 16); |
| 71 | 125 | } |
| 126 | ||
| 127 | public static byte[] _bytesUI16(long val) { | |
| 128 | //TODO: implement this | |
| 129 | throw new IllegalStateException("not implemented yet"); | |
| 130 | } | |
| 72 | 131 | |
| 73 | 132 | public static long parseUI16(byte[] val) { |
| 74 | 133 | return bytesToLong(new byte[] { val[0], val[1] }, false); |
| 75 | 134 | } |
| 76 | 135 | |
| 136 | public static long _parseUI16(byte[] val) { | |
| 137 | //TODO: implement this | |
| 138 | throw new IllegalStateException("not implemented yet"); | |
| 139 | } | |
| 140 | ||
| 77 | 141 | public static BitSet bitsUI24(long val) { |
| 78 | 142 | Assert.inRange(val, MIN_UI24, MAX_UI24); |
| 79 | 143 | return flipBytes(longToBits(val, 24), 3); |
| 80 | 144 | } |
| 81 | 145 | |
| 146 | public static Bits _bitsUI24(long val) { | |
| 147 | //TODO: implement this | |
| 148 | throw new IllegalStateException("not implemented yet"); | |
| 149 | } | |
| 150 | ||
| 82 | 151 | public static byte[] bytesUI24(long val) { |
| 83 | 152 | return bitsToBytes(bitsUI24(val), 24); |
| 84 | 153 | } |
| 85 | 154 | |
| 155 | public static byte[] _bytesUI24(long val) { | |
| 156 | //TODO: implement this | |
| 157 | throw new IllegalStateException("not implemented yet"); | |
| 158 | } | |
| 159 | ||
| 86 | 160 | public static long parseUI24(byte[] val) { |
| 87 | 161 | return bytesToLong(new byte[] { val[0], val[1], val[2] }, false); |
| 88 | 162 | } |
| 89 | 163 | |
| 164 | public static long _parseUI24(byte[] val) { | |
| 165 | //TODO: implement this | |
| 166 | throw new IllegalStateException("not implemented yet"); | |
| 167 | } | |
| 168 | ||
| 90 | 169 | public static BitSet bitsUI32(long val) { |
| 91 | 170 | Assert.inRange(val, MIN_UI32, MAX_UI32); |
| 92 | 171 | return flipBytes(longToBits(val, 32), 4); |
| 93 | 172 | } |
| 173 | ||
| 174 | public static Bits _bitsUI32(long val) { | |
| 175 | //TODO: implement this | |
| 176 | throw new IllegalStateException("not implemented yet"); | |
| 177 | } | |
| 94 | 178 | |
| 95 | 179 | public static byte[] bytesUI32(long val) { |
| 96 | 180 | return bitsToBytes(bitsUI32(val), 32); |
| 97 | 181 | } |
| 98 | 182 | |
| 183 | public static byte[] _bytesUI32(long val) { | |
| 184 | //TODO: implement this | |
| 185 | throw new IllegalStateException("not implemented yet"); | |
| 186 | } | |
| 187 | ||
| 99 | 188 | public static long parseUI32(byte[] val) { |
| 100 | 189 | return bytesToLong(new byte[] { val[0], val[1], val[2], val[3] }, false); |
| 101 | 190 | } |
| 102 | 191 | |
| 192 | public static long _parseUI32(byte[] val) { | |
| 193 | //TODO: implement this | |
| 194 | throw new IllegalStateException("not implemented yet"); | |
| 195 | } | |
| 196 | ||
| 103 | 197 | public static BitSet bitsSI8(long val) { |
| 104 | 198 | Assert.inRange(val, MIN_SI8, MAX_SI8); |
| 105 | 199 | return longToBits(val, 8); |
| 106 | 200 | } |
| 107 | 201 | |
| 202 | public static Bits _bitsSI8(long val) { | |
| 203 | //TODO: implement this | |
| 204 | throw new IllegalStateException("not implemented yet"); | |
| 205 | } | |
| 206 | ||
| 108 | 207 | public static byte[] bytesSI8(long val) { |
| 109 | 208 | return bitsToBytes(bitsSI8(val), 8); |
| 110 | 209 | } |
| 111 | 210 | |
| 211 | public static byte[] _bytesSI8(long val) { | |
| 212 | //TODO: implement this | |
| 213 | throw new IllegalStateException("not implemented yet"); | |
| 214 | } | |
| 215 | ||
| 112 | 216 | public static long parseSI8(byte[] val) { |
| 113 | 217 | return bytesToLong(new byte[] { val[0] }, true); |
| 114 | 218 | } |
| 115 | 219 | |
| 220 | public static long _parseSI8(byte[] val) { | |
| 221 | //TODO: implement this | |
| 222 | throw new IllegalStateException("not implemented yet"); | |
| 223 | } | |
| 224 | ||
| 116 | 225 | public static BitSet bitsSI16(long val) { |
| 117 | 226 | Assert.inRange(val, MIN_SI16, MAX_SI16); |
| 118 | 227 | return flipBytes(longToBits(val, 16), 2); |
| 119 | 228 | } |
| 120 | 229 | |
| 230 | public static Bits _bitsSI16(long val) { | |
| 231 | //TODO: implement this | |
| 232 | throw new IllegalStateException("not implemented yet"); | |
| 233 | } | |
| 234 | ||
| 121 | 235 | public static byte[] bytesSI16(long val) { |
| 122 | 236 | return bitsToBytes(bitsSI16(val), 16); |
| 123 | 237 | } |
| 124 | 238 | |
| 239 | public static byte[] _bytesSI16(long val) { | |
| 240 | //TODO: implement this | |
| 241 | throw new IllegalStateException("not implemented yet"); | |
| 242 | } | |
| 243 | ||
| 125 | 244 | public static long parseSI16(byte[] val) { |
| 126 | 245 | return bytesToLong(new byte[] { val[0], val[1] }, true); |
| 127 | 246 | } |
| 128 | 247 | |
| 248 | public static long _parseSI16(byte[] val) { | |
| 249 | //TODO: implement this | |
| 250 | throw new IllegalStateException("not implemented yet"); | |
| 251 | } | |
| 252 | ||
| 129 | 253 | public static BitSet bitsSI32(long val) { |
| 130 | 254 | Assert.inRange(val, MIN_SI32, MAX_SI32); |
| 131 | 255 | return flipBytes(longToBits(val, 32), 4); |
| 132 | 256 | } |
| 133 | 257 | |
| 258 | public static Bits _bitsSI32(long val) { | |
| 259 | //TODO: implement this | |
| 260 | throw new IllegalStateException("not implemented yet"); | |
| 261 | } | |
| 262 | ||
| 134 | 263 | public static byte[] bytesSI32(long val) { |
| 135 | 264 | return bitsToBytes(bitsSI32(val), 32); |
| 136 | 265 | } |
| 137 | 266 | |
| 267 | public static byte[] _bytesSI32(long val) { | |
| 268 | //TODO: implement this | |
| 269 | throw new IllegalStateException("not implemented yet"); | |
| 270 | } | |
| 271 | ||
| 138 | 272 | public static byte[] bytesSTRING(String val) { |
| 139 | 273 | try { |
| 140 | 274 | if (val == null || val.equals("")) { |
| ... | ...@@ -150,23 +284,49 @@ | |
| 150 | 284 | } |
| 151 | 285 | } |
| 152 | 286 | |
| 287 | public static byte[] _bytesSTRING(String val) { | |
| 288 | //TODO: implement this | |
| 289 | throw new IllegalStateException("not implemented yet"); | |
| 290 | } | |
| 291 | ||
| 292 | ||
| 153 | 293 | public static long parseSI32(byte[] val) { |
| 154 | 294 | return bytesToLong(new byte[] { val[0], val[1], val[2], val[3] }, true); |
| 155 | 295 | } |
| 156 | 296 | |
| 297 | public static long _parseSI32(byte[] val) { | |
| 298 | //TODO: implement this | |
| 299 | throw new IllegalStateException("not implemented yet"); | |
| 300 | } | |
| 301 | ||
| 157 | 302 | public static BitSet bitsFixed88(double val) { |
| 158 | 303 | Assert.inRange(val, MIN_FIXED88, MAX_FIXED88); |
| 159 | 304 | return bitsSI16((short) (val * 256.0)); |
| 160 | 305 | } |
| 161 | 306 | |
| 307 | public static Bits _bitsFixed88(double val){ | |
| 308 | //TODO: implement this | |
| 309 | throw new IllegalStateException("not implemented yet"); | |
| 310 | } | |
| 311 | ||
| 162 | 312 | public static byte[] bytesFixed88(double val) { |
| 163 | 313 | return bitsToBytes(bitsFixed88(val), 16); |
| 164 | 314 | } |
| 165 | 315 | |
| 316 | public static byte[] _bytesFixed88(double val) { | |
| 317 | //TODO: implement this | |
| 318 | throw new IllegalStateException("not implemented yet"); | |
| 319 | } | |
| 320 | ||
| 166 | 321 | public static double parseFixed88(byte[] val) { |
| 167 | 322 | return ((double) parseSI16(val)) / 256.0; |
| 168 | 323 | } |
| 169 | 324 | |
| 325 | public static double _parseFixed88(byte[] val) { | |
| 326 | //TODO: implement this | |
| 327 | throw new IllegalStateException("not implemented yet"); | |
| 328 | } | |
| 329 | ||
| 170 | 330 | public static BitSet bitsFixed1616(double val) { |
| 171 | 331 | Assert.inRange(val, MIN_FIXED1616, MAX_FIXED1616); |
| 172 | 332 | BitSet result = new BitSet(32); |
| ... | ...@@ -174,14 +334,29 @@ | |
| 174 | 334 | return result; |
| 175 | 335 | } |
| 176 | 336 | |
| 337 | public static Bits _bitsFixed1616(double val) { | |
| 338 | //TODO: implement this | |
| 339 | throw new IllegalStateException("not implemented yet"); | |
| 340 | } | |
| 341 | ||
| 177 | 342 | public static byte[] bytesFixed1616(double val) { |
| 178 | 343 | return bitsToBytes(bitsFixed1616(val), 32); |
| 179 | 344 | } |
| 180 | 345 | |
| 346 | public static byte[] _bytesFixed1616(double val) { | |
| 347 | //TODO: implement this | |
| 348 | throw new IllegalStateException("not implemented yet"); | |
| 349 | } | |
| 350 | ||
| 181 | 351 | public static double parseFixed1616(byte[] val) { |
| 182 | 352 | return ((double) parseSI32(val)) / 65536.0; |
| 183 | 353 | } |
| 184 | 354 | |
| 355 | public static double _parseFixed1616(byte[] val) { | |
| 356 | //TODO: implement this | |
| 357 | throw new IllegalStateException("not implemented yet"); | |
| 358 | } | |
| 359 | ||
| 185 | 360 | public static BitSet bitsFloat16(double val) { |
| 186 | 361 | Assert.inRange(val, MIN_FLOAT16, MAX_FLOAT16); |
| 187 | 362 | int bits32 = Float.floatToIntBits((float) val); |
| ... | ...@@ -210,10 +385,20 @@ | |
| 210 | 385 | return bitsUI16(bits16); |
| 211 | 386 | } |
| 212 | 387 | |
| 388 | public static Bits _bitsFloat16(double val) { | |
| 389 | //TODO: implement this | |
| 390 | throw new IllegalStateException("not implemented yet"); | |
| 391 | } | |
| 392 | ||
| 213 | 393 | public static byte[] bytesFloat16(double val) { |
| 214 | 394 | return bitsToBytes(bitsFloat16(val), 16); |
| 215 | 395 | } |
| 216 | 396 | |
| 397 | public static byte[] _bytesFloat16(double val) { | |
| 398 | //TODO: implement this | |
| 399 | throw new IllegalStateException("not implemented yet"); | |
| 400 | } | |
| 401 | ||
| 217 | 402 | public static double parseFloat16(byte[] val) { |
| 218 | 403 | BitSet bits16 = bytesToBits(val); |
| 219 | 404 | bits16 = flipBytes(bits16, 2); |
| ... | ...@@ -247,19 +432,39 @@ | |
| 247 | 432 | return result; |
| 248 | 433 | } |
| 249 | 434 | |
| 435 | public static double _parseFloat16(byte[] val) { | |
| 436 | //TODO: implement this | |
| 437 | throw new IllegalStateException("not implemented yet"); | |
| 438 | } | |
| 439 | ||
| 250 | 440 | public static BitSet bitsFloat32(double val) { |
| 251 | 441 | Assert.inRange(val, MIN_FLOAT32, MAX_FLOAT32); |
| 252 | 442 | return bitsSI32(Float.floatToIntBits((float) val)); |
| 253 | 443 | } |
| 254 | 444 | |
| 445 | public static Bits _bitsFloat32(double val) { | |
| 446 | //TODO: implement this | |
| 447 | throw new IllegalStateException("not implemented yet"); | |
| 448 | } | |
| 449 | ||
| 255 | 450 | public static double parseFloat32(byte[] val) { |
| 256 | 451 | return Float.intBitsToFloat((int) parseSI32(val)); |
| 257 | 452 | } |
| 258 | ||
| 453 | ||
| 454 | public static double _parseFloat32(byte[] val) { | |
| 455 | //TODO: implement this | |
| 456 | throw new IllegalStateException("not implemented yet"); | |
| 457 | } | |
| 458 | ||
| 259 | 459 | public static byte[] bytesFloat32(double val) { |
| 260 | 460 | return bitsToBytes(bitsFloat32(val), 32); |
| 261 | 461 | } |
| 262 | 462 | |
| 463 | public static byte[] _bytesFloat32(double val) { | |
| 464 | //TODO: implement this | |
| 465 | throw new IllegalStateException("not implemented yet"); | |
| 466 | } | |
| 467 | ||
| 263 | 468 | public static BitSet bitsFloat64(double val) { |
| 264 | 469 | Assert.inRange(val, MIN_FLOAT64, MAX_FLOAT64); |
| 265 | 470 | BitSet result; |
| ... | ...@@ -283,15 +488,30 @@ | |
| 283 | 488 | return result; |
| 284 | 489 | } |
| 285 | 490 | |
| 491 | public static Bits _bitsFloat64(double val) { | |
| 492 | //TODO: implement this | |
| 493 | throw new IllegalStateException("not implemented yet"); | |
| 494 | } | |
| 495 | ||
| 286 | 496 | public static byte[] bytesFloat64(double val) { |
| 287 | 497 | return bitsToBytes(bitsFloat64(val), 64); |
| 288 | 498 | } |
| 289 | 499 | |
| 500 | public static byte[] _bytesFloat64(double val) { | |
| 501 | //TODO: implement this | |
| 502 | throw new IllegalStateException("not implemented yet"); | |
| 503 | } | |
| 504 | ||
| 290 | 505 | public static double parseFloat64(byte[] val) { |
| 291 | 506 | BitSet result = bytesToBits(new byte[] { val[3], val[2], val[1], val[0], val[7], val[6], val[5], val[4] }); |
| 292 | 507 | return Double.longBitsToDouble(bitsToLong(result, 64, true)); |
| 293 | 508 | } |
| 294 | 509 | |
| 510 | public static double _parseFloat64(byte[] val) { | |
| 511 | //TODO: implement this | |
| 512 | throw new IllegalStateException("not implemented yet"); | |
| 513 | } | |
| 514 | ||
| 295 | 515 | public static BitSet bitsFBArray(double val, int bitNumber) { |
| 296 | 516 | Assert.inRange(val, bitNumber); |
| 297 | 517 | BitSet rawResult = bitsFixed1616(val); |
| ... | ...@@ -304,10 +524,20 @@ | |
| 304 | 524 | return result; |
| 305 | 525 | } |
| 306 | 526 | |
| 527 | public static Bits _bitsFBArray(double val, int bitNumber) { | |
| 528 | //TODO: implement this | |
| 529 | throw new IllegalStateException("not implemented yet"); | |
| 530 | } | |
| 531 | ||
| 307 | 532 | public static byte[] bytesFBArray(double val, int bitNumber) { |
| 308 | 533 | return bitsToBytes(bitsFBArray(val, bitNumber), bitNumber); |
| 309 | 534 | } |
| 310 | 535 | |
| 536 | public static byte[] _bytesFBArray(double val, int bitNumber) { | |
| 537 | //TODO: implement this | |
| 538 | throw new IllegalStateException("not implemented yet"); | |
| 539 | } | |
| 540 | ||
| 311 | 541 | public static double parseFBArray(BitSet val, int bitNumber) { |
| 312 | 542 | double result = 0l; |
| 313 | 543 | if (bitNumber < 32) { |
| ... | ...@@ -322,40 +552,84 @@ | |
| 322 | 552 | return result; |
| 323 | 553 | } |
| 324 | 554 | |
| 555 | public static double _parseFBArray(Bits val) { | |
| 556 | //TODO: implement this | |
| 557 | throw new IllegalStateException("not implemented yet"); | |
| 558 | } | |
| 559 | ||
| 325 | 560 | public static BitSet bitsSBArray(long val, int bits) { |
| 326 | 561 | Assert.inRange(val, bits, true); |
| 327 | 562 | return longToBits(val, bits); |
| 328 | 563 | } |
| 329 | 564 | |
| 565 | public static Bits _bitsSBArray(long val, int bits) { | |
| 566 | //TODO: implement this | |
| 567 | throw new IllegalStateException("not implemented yet"); | |
| 568 | } | |
| 569 | ||
| 330 | 570 | public static byte[] bytesSBArray(long val, int bits) { |
| 331 | 571 | return bitsToBytes(bitsSBArray(val, bits), bits); |
| 332 | 572 | } |
| 333 | 573 | |
| 574 | public static byte[] _bytesSBArray(long val, int bits) { | |
| 575 | //TODO: implement this | |
| 576 | throw new IllegalStateException("not implemented yet"); | |
| 577 | } | |
| 578 | ||
| 334 | 579 | public static long parseSBArray(BitSet val, int bits) { |
| 335 | 580 | return bitsToLong(val, bits, true); |
| 336 | 581 | } |
| 337 | 582 | |
| 583 | public static long _parseSBArray(Bits val) { | |
| 584 | //TODO: implement this | |
| 585 | throw new IllegalStateException("not implemented yet"); | |
| 586 | } | |
| 587 | ||
| 338 | 588 | public static long parseSBArray(byte[] val) { |
| 339 | 589 | return bytesToLong(val, true); |
| 340 | 590 | } |
| 341 | 591 | |
| 592 | public static long _parseSBArray(byte[] val) { | |
| 593 | //TODO: implement this | |
| 594 | throw new IllegalStateException("not implemented yet"); | |
| 595 | } | |
| 596 | ||
| 342 | 597 | public static BitSet bitsUBArray(long val, int bits) { |
| 343 | 598 | Assert.inRange(val, bits, false); |
| 344 | 599 | return longToBits(val, bits); |
| 345 | 600 | } |
| 346 | 601 | |
| 602 | public static Bits _bitsUBArray(long val, int bits) { | |
| 603 | //TODO: implement this | |
| 604 | throw new IllegalStateException("not implemented yet"); | |
| 605 | } | |
| 606 | ||
| 347 | 607 | public static byte[] bytesUBArray(long val, int bits) { |
| 348 | 608 | return bitsToBytes(bitsUBArray(val, bits), bits); |
| 349 | 609 | } |
| 350 | 610 | |
| 611 | public static byte[] _bytesUBArray(long val, int bits) { | |
| 612 | //TODO: implement this | |
| 613 | throw new IllegalStateException("not implemented yet"); | |
| 614 | } | |
| 351 | 615 | public static long parseUBArray(BitSet val, int bits) { |
| 352 | 616 | return bitsToLong(val, bits, false); |
| 353 | 617 | } |
| 354 | 618 | |
| 619 | public static long _parseUBArray(Bits val, int bits) { | |
| 620 | //TODO: implement this | |
| 621 | throw new IllegalStateException("not implemented yet"); | |
| 622 | } | |
| 623 | ||
| 355 | 624 | public static long parseUBArray(byte[] val) { |
| 356 | 625 | return bytesToLong(val, false); |
| 357 | 626 | } |
| 358 | 627 | |
| 628 | public static long _parseUBArray(byte[] val) { | |
| 629 | //TODO: implement this | |
| 630 | throw new IllegalStateException("not implemented yet"); | |
| 631 | } | |
| 632 | ||
| 359 | 633 | public static BitSet bitsEncodedU32(byte[] pos) { |
| 360 | 634 | if (pos.length > 5) { |
| 361 | 635 | throw new RuntimeException("trying to encode more then 5 bytes"); |
| ... | ...@@ -380,14 +654,23 @@ | |
| 380 | 654 | return bitsUI32(result); |
| 381 | 655 | } |
| 382 | 656 | |
| 657 | public static BitSet _bitsEncodedU32(byte[] pos) { | |
| 658 | //TODO: implement this | |
| 659 | throw new IllegalStateException("not implemented yet"); | |
| 660 | } | |
| 661 | ||
| 383 | 662 | public static byte[] bytesEncodedU32(byte[] pos) { |
| 384 | 663 | return bitsToBytes(bitsEncodedU32(pos), 32); |
| 385 | 664 | } |
| 386 | 665 | |
| 666 | public static byte[] _bytesEncodedU32(byte[] pos) { | |
| 667 | //TODO: implement this | |
| 668 | throw new IllegalStateException("not implemented yet"); | |
| 669 | } | |
| 387 | 670 | // /////////////////////////////////////////// |
| 388 | 671 | // Resusable utility methods |
| 389 | 672 | // /////////////////////////////////////////// |
| 390 | private static BitSet longToBits(long val, int size) { | |
| 673 | public static BitSet longToBits(long val, int size) { | |
| 391 | 674 | String bits = Long.toBinaryString(val); |
| 392 | 675 | if (bits.length() > size) { |
| 393 | 676 | bits = bits.substring(bits.length() - size); |
| ... | ...@@ -405,7 +688,26 @@ | |
| 405 | 688 | return bitset; |
| 406 | 689 | } |
| 407 | 690 | |
| 408 | private static long bitsToLong(BitSet bitSet, int numBits, boolean signed) { | |
| 691 | public static Bits _longToBits(long val, int size) { | |
| 692 | long tmp = val; | |
| 693 | if(size > 64){ | |
| 694 | throw new IllegalArgumentException("sub size can not be more then 64 bits"); | |
| 695 | } | |
| 696 | if(size < 0){ | |
| 697 | throw new IllegalArgumentException("sub size can not be negative"); | |
| 698 | } | |
| 699 | int byteSize = ((int)(size / 8)) + ((size%8 == 0) ? 0 : 1); | |
| 700 | byte[] data = new byte[byteSize]; | |
| 701 | for (int i = 0; i < size; i++){ | |
| 702 | int bytenum = bytenums[i]; | |
| 703 | int bitnum = reversebitnums[i]; | |
| 704 | byte databit = (byte) ((tmp >> (size - i - 1) ) & 1); | |
| 705 | data[bytenum] |= databit<<bitnum; | |
| 706 | } | |
| 707 | return new Bits(data, size); | |
| 708 | } | |
| 709 | ||
| 710 | public static long bitsToLong(BitSet bitSet, int numBits, boolean signed) { | |
| 409 | 711 | long result = 0l; |
| 410 | 712 | if (!signed) { |
| 411 | 713 | for (int i = 0; i < numBits; i++) { |
| ... | ...@@ -422,7 +724,50 @@ | |
| 422 | 724 | return result; |
| 423 | 725 | } |
| 424 | 726 | |
| 425 | private static long bitsToLong(BitSet bitSet, int startPosition, int numBits, boolean signed) { | |
| 727 | public static long _bitsToLong(Bits val, boolean signed) { | |
| 728 | long result = 0l; | |
| 729 | ||
| 730 | if(val.getOffset() > 64){ | |
| 731 | throw new IllegalArgumentException("sub size can not be more then 64 bits"); | |
| 732 | } | |
| 733 | if(val.getOffset() < 0){ | |
| 734 | throw new IllegalArgumentException("sub size can not be negative"); | |
| 735 | } | |
| 736 | int numBits = val.getOffset(); | |
| 737 | byte[] data = val.getData(); | |
| 738 | ||
| 739 | if (!signed) { | |
| 740 | for (int i = 0; i < numBits; i++) { | |
| 741 | int bytenum = BitUtils.bytenums[i]; | |
| 742 | int bitnum = BitUtils.reversebitnums[i]; | |
| 743 | long databit = ((((data[bytenum])) & (1 << bitnum)) == 0) ? 0 : 1; | |
| 744 | result |= databit<<(numBits - i - 1); | |
| 745 | } | |
| 746 | } else { | |
| 747 | long sign = ((((data[0])) & (1 << 7)) == 0) ? 0 : 1; | |
| 748 | if(sign == 0){ | |
| 749 | for (int i = 1; i < numBits; i++) { | |
| 750 | int bytenum = BitUtils.bytenums[i]; | |
| 751 | int bitnum = BitUtils.reversebitnums[i]; | |
| 752 | long databit = ((((data[bytenum])) & (1 << bitnum)) == 0) ? 0 : 1; | |
| 753 | result |= databit<<(numBits - i - 1); | |
| 754 | } | |
| 755 | } else { | |
| 756 | for (int i = 1; i < numBits; i++) { | |
| 757 | int bytenum = BitUtils.bytenums[i]; | |
| 758 | int bitnum = BitUtils.reversebitnums[i]; | |
| 759 | long databit = ((((data[bytenum])) & (1 << bitnum)) == 0) ? 1 : 0; | |
| 760 | result |= databit<<(numBits - i - 1); | |
| 761 | } | |
| 762 | result += 1; | |
| 763 | result = (result ^ -1) + 1; | |
| 764 | } | |
| 765 | } | |
| 766 | return result; | |
| 767 | ||
| 768 | } | |
| 769 | ||
| 770 | public static long bitsToLong(BitSet bitSet, int startPosition, int numBits, boolean signed) { | |
| 426 | 771 | long result = 0l; |
| 427 | 772 | if (!signed) { |
| 428 | 773 | for (int i = startPosition; i < (startPosition + numBits); i++) { |
| ... | ...@@ -439,6 +784,59 @@ | |
| 439 | 784 | return result; |
| 440 | 785 | } |
| 441 | 786 | |
| 787 | public static long _bitsToLong(Bits val, int startPosition, int offset, boolean signed) { | |
| 788 | long result = 0l; | |
| 789 | ||
| 790 | if(startPosition < 0) { | |
| 791 | throw new IllegalArgumentException("start position can not be negative"); | |
| 792 | } | |
| 793 | ||
| 794 | if(offset < 0) { | |
| 795 | throw new IllegalArgumentException("sub size can not be negative"); | |
| 796 | } | |
| 797 | ||
| 798 | if((startPosition + offset) > 64) { | |
| 799 | throw new IllegalArgumentException("sub size can not be more then 64 bits"); | |
| 800 | } | |
| 801 | ||
| 802 | if(offset == 0){ | |
| 803 | return 0; | |
| 804 | } | |
| 805 | int numBits = offset; | |
| 806 | byte[] data = val.getData(); | |
| 807 | ||
| 808 | if (!signed) { | |
| 809 | for (int i = startPosition; i < (numBits + startPosition) ; i++) { | |
| 810 | int bytenum = bytenums[i]; | |
| 811 | int bitnum = reversebitnums[i]; | |
| 812 | long databit = ((((data[bytenum])) & (1 << bitnum)) == 0) ? 0 : 1; | |
| 813 | result |= databit<<((numBits + startPosition) - i - 1); | |
| 814 | } | |
| 815 | } else { | |
| 816 | int bytenum = bytenums[startPosition]; | |
| 817 | int bitnum = reversebitnums[startPosition]; | |
| 818 | long sign = ((((data[bytenum])) & (1 << bitnum)) == 0) ? 0 : 1; | |
| 819 | if(sign == 0){ | |
| 820 | for (int i = startPosition + 1; i < (numBits + startPosition); i++) { | |
| 821 | bytenum = bytenums[i]; | |
| 822 | bitnum = reversebitnums[i]; | |
| 823 | long databit = ((((data[bytenum])) & (1 << bitnum)) == 0) ? 0 : 1; | |
| 824 | result |= databit<<((numBits + startPosition) - i - 1); | |
| 825 | } | |
| 826 | } else { | |
| 827 | for (int i = startPosition + 1; i < (numBits + startPosition); i++) { | |
| 828 | bytenum = bytenums[i]; | |
| 829 | bitnum = reversebitnums[i]; | |
| 830 | long databit = ((((data[bytenum])) & (1 << bitnum)) == 0) ? 1 : 0; | |
| 831 | result |= databit<<((numBits + startPosition) - i - 1); | |
| 832 | } | |
| 833 | result += 1; | |
| 834 | result = (result ^ -1)+1; | |
| 835 | } | |
| 836 | } | |
| 837 | return result; | |
| 838 | } | |
| 839 | ||
| 442 | 840 | private static long bytesToLong(byte[] val, boolean signed) { |
| 443 | 841 | long result = 0l; |
| 444 | 842 | BitSet bitSet = bytesToBits(val); |
| ... | ...@@ -468,6 +866,11 @@ | |
| 468 | 866 | } |
| 469 | 867 | } |
| 470 | 868 | |
| 869 | public static int _calculateMaxBitCountUI(long[] data) { | |
| 870 | // TODO: implement this | |
| 871 | throw new IllegalStateException("not implemented yet"); | |
| 872 | } | |
| 873 | ||
| 471 | 874 | public static int calculateMaxBitCountSI(long[] data) { |
| 472 | 875 | for (int i = 0; i < data.length; i++) { |
| 473 | 876 | if (data[i] < 0) { |
| ... | ...@@ -480,7 +883,11 @@ | |
| 480 | 883 | } else { |
| 481 | 884 | return 32 - BitUtils.bitsToString(BitUtils.flipBytes(BitUtils.bitsUI32(data[data.length - 1]), 4), 32).indexOf('1') + 1; |
| 482 | 885 | } |
| 886 | } | |
| 483 | 887 | |
| 888 | public static int _calculateMaxBitCountSI(long[] data) { | |
| 889 | // TODO: implement this | |
| 890 | throw new IllegalStateException("not implemented yet"); | |
| 484 | 891 | } |
| 485 | 892 | |
| 486 | 893 | public static int calculateMaxBitCount(double[] data) { |
| ... | ...@@ -493,26 +900,9 @@ | |
| 493 | 900 | } |
| 494 | 901 | } |
| 495 |