### Question:
BitReader { public long bits2UInt(int nb) throws IOException { Preconditions.checkArgument(nb <= 64); Preconditions.checkArgument(nb >= 0); long result = 0; int bitsLeft = nb; while (bitsLeft > 0) { if (bitPos == 0) { bitBuf = nextByte(); bitPos = BIT_LENGTH; } int size = Math.min(bitsLeft, bitPos); int myBits = bitBuf >> (bitPos - size); myBits &= BYTE_BITMASK; myBits &= ~(BYTE_BITMASK << size); int shift = bitsLeft - size; assert shift >= 0; result |= myBits << shift; bitsLeft -= size; bitPos -= size; } return result; } BitReader(byte[] test);  BitReader(RandomAccessFile raf, long startPos); void incrByte(); void setBitOffset(int bitOffset); long getPos(); long bits2UInt(int nb); long bits2SInt(int nb);  }### Answer:
@Test public void testUcar() throws IOException { BitReader bu = new BitReader(new byte[] {-1, 2, 4, 8}); assertEquals(127, bu.bits2UInt(7)); assertEquals(1, bu.bits2UInt(1)); }
@Test public void testUnsigned() throws IOException { byte[] bits = new byte[] {(byte) 199, (byte) 242, (byte) 0, (byte) 0, (byte) 6, (byte) 6}; System.out.printf("%s", Misc.showBits(bits)); BitReader bu = new BitReader(bits); assertEquals(799, (int) bu.bits2UInt(10)); assertEquals(800, (int) bu.bits2UInt(10)); assertEquals(0, (int) bu.bits2UInt(10)); assertEquals(6, (int) bu.bits2UInt(10)); assertEquals(6, (int) bu.bits2UInt(8)); }