public long bits2UInt ( int nb ) throws IOException { assert nb <= 64 ; assert nb >= 0 ; long result = 0 ; int bitsLeft = nb ; while ( bitsLeft > 0 ) { // we ran out of bits - fetch the next byte... if ( bitPos == 0 ) { bitBuf = nextByte ( ) ; bitPos = BIT_LENGTH ; } // -- retrieve bit from current byte ---------- // how many bits to read from the current byte int size = Math . min ( bitsLeft , bitPos ) ; // move my part to start int myBits = bitBuf >> ( bitPos - size ) ; // mask-off sign-extending myBits &= BYTE_BITMASK ; // mask-off bits of next value myBits &= ~ ( BYTE_BITMASK << size ) ; // -- put bit to result ---------------------- // where to place myBits inside of result int shift = bitsLeft - size ; assert shift >= 0 ; // put it there result |= myBits << shift ; // -- put bit to result ---------------------- // update information on what we consumed bitsLeft -= size ; bitPos -= size ; } return result ; }
Read the next nb bits and return an Unsigned Long .