public int readBits(int numBits){
  Assertions.checkState(getPosition() + numBits <= limit);
  if (numBits == 0) {
    return 0;
  }
  int result=0;
  int bitCount=0;
  if (bitOffset != 0) {
    bitCount=Math.min(numBits,8 - bitOffset);
    int mask=0xFF >>> (8 - bitCount);
    result=(data[byteOffset] >>> bitOffset) & mask;
    bitOffset+=bitCount;
    if (bitOffset == 8) {
      byteOffset++;
      bitOffset=0;
    }
  }
  if (numBits - bitCount > 7) {
    int numBytes=(numBits - bitCount) / 8;
    for (int i=0; i < numBytes; i++) {
      result|=(data[byteOffset++] & 0xFFL) << bitCount;
      bitCount+=8;
    }
  }
  if (numBits > bitCount) {
    int bitsOnNextByte=numBits - bitCount;
    int mask=0xFF >>> (8 - bitsOnNextByte);
    result|=(data[byteOffset] & mask) << bitCount;
    bitOffset+=bitsOnNextByte;
  }
  return result;
}

public int readBits ( int numBits ) { Assertions . checkState ( getPosition ( ) + numBits <= limit ) ; if ( numBits == 0 ) { return 0 ; } int result = 0 ; int bitCount = 0 ; if ( bitOffset != 0 ) { bitCount = Math . min ( numBits , 8 - bitOffset ) ; int mask = 0xFF > > > ( 8 - bitCount ) ; result = ( data [ byteOffset ] > > > bitOffset ) & mask ; bitOffset += bitCount ; if ( bitOffset == 8 ) { byteOffset ++ ; bitOffset = 0 ; } } if ( numBits - bitCount > 7 ) { int numBytes = ( numBits - bitCount ) / 8 ; for ( int i = 0 ; i < numBytes ; i ++ ) { result |= ( data [ byteOffset ++ ] & 0xFFL ) << bitCount ; bitCount += 8 ; } } if ( numBits > bitCount ) { int bitsOnNextByte = numBits - bitCount ; int mask = 0xFF > > > ( 8 - bitsOnNextByte ) ; result |= ( data [ byteOffset ] & mask ) << bitCount ; bitOffset += bitsOnNextByte ; } return result ; }
reads up to 32 bits .
