Utils { public static int bitsToInt(BitSet bits, int start, int length) { if (length <= 0) { throw new IllegalArgumentException("length must be >0"); } BigInteger result = BigInteger.ZERO; for (int i = 0; i < length; i++) { if (bits.get(start + i)) { result = result.add(TWO.pow(i)); } } return result.intValue(); } private  Utils(); static String toHex(long address); static String readUntilNull(ByteBuffer buffer); static boolean validateName(String name); static void seekBufferToNextMultipleOfEight(ByteBuffer bb); static int readBytesAsUnsignedInt(ByteBuffer buffer, int length); static long readBytesAsUnsignedLong(ByteBuffer buffer, int length); static ByteBuffer createSubBuffer(ByteBuffer source, int length); static int bitsToInt(BitSet bits, int start, int length); static int bytesNeededToHoldNumber(long number); static int[] linearIndexToDimensionIndex(int index, int[] dimensions); static int dimensionIndexToLinearIndex(int[] index, int[] dimensions); static int[] chunkIndexToChunkOffset(int chunkIndex, int[] chunkDimensions, int[] datasetDimensions); static int[] stripLeadingIndex(int[] dims);  }
@Test void testBitsToInt() { BitSet bits = new BitSet(); assertThat(Utils.bitsToInt(bits, 0, 8), is(equalTo(0))); bits.set(0, true); assertThat(Utils.bitsToInt(bits, 0, 1), is(equalTo(1))); assertThat(Utils.bitsToInt(bits, 0, 5), is(equalTo(1))); bits.set(1, true); assertThat(Utils.bitsToInt(bits, 0, 2), is(equalTo(3))); bits.set(2, true); assertThat(Utils.bitsToInt(bits, 0, 3), is(equalTo(7))); bits.set(0, 7, false); bits.set(7, true); assertThat(Utils.bitsToInt(bits, 0, 8), is(equalTo(128))); assertThat(Utils.bitsToInt(bits, 0, 7), is(equalTo(0))); assertThat(Utils.bitsToInt(bits, 5, 3), is(equalTo(4))); }
@Test void testBitsToIntThrowsWithNegativeLength() { BitSet bits = new BitSet(8); assertThrows(IllegalArgumentException.class, () -> Utils.bitsToInt(bits, 3, -1)); }