public int read ( int location , int length , boolean swapOrder ) { if ( length < 0 || length > 32 ) throw new IllegalArgumentException ( "Length can't exceed 32" ) ; if ( location + length > size ) throw new IllegalArgumentException ( "Attempting to read past the end" ) ; // TODO speed up by reading in byte chunks int output = 0 ; if ( swapOrder ) { for ( int i = 0 ; i < length ; i ++ ) { output |= get ( location + i ) << ( length - i - 1 ) ; } } else { for ( int i = 0 ; i < length ; i ++ ) { output |= get ( location + i ) << i ; } } return output ; }
Read bits from the array and store them in an int