private static void packInt ( int [ ] inputArray , int bitSize , int startPosIn , int start , int skip , int countA , byte [ ] dest ) { if ( DEBUG_LEV > 30 ) System . err . println ( "EncodedElement::packInt : Begin" ) ; for ( int valI = 0 ; valI < countA ; valI ++ ) { //int input = inputArray[valI]; int input = inputArray [ valI * ( skip + 1 ) + start ] ; int count = bitSize ; int startPos = startPosIn + valI * bitSize ; int currentByte = startPos / 8 ; int currentOffset = startPos % 8 ; int bitRoom ; //how many bits can be placed in current byte int upMask ; //to clear upper bits(lower bits auto-cleared by L-shift int downShift ; //bits to shift down, isolating top bits of input int upShift ; //bits to shift up, packing byte from top. while ( count > 0 ) { //find how many bits can be placed in current byte bitRoom = 8 - currentOffset ; //get those bits //i.e, take upper 'bitsNeeded' of input, put to lower part of byte. downShift = count - bitRoom ; //upMask = uRSHFT(255 ,currentOffset); upMask = ( currentOffset >= 32 ) ? 0 : 255 >>> currentOffset ; upShift = 0 ; if ( downShift < 0 ) { //upMask = 255 >>> bitRoom-count; upShift = bitRoom - count ; //upMask = uRSHFT(255,(currentOffset+upShift)); upMask = ( ( currentOffset + upShift ) >= 32 ) ? 0 : 255 >>> ( currentOffset + upShift ) ; downShift = 0 ; } if ( DEBUG_LEV > 30 ) { System . err . println ( "count:offset:bitRoom:downShift:upShift:" + count + ":" + currentOffset + ":" + bitRoom + ":" + downShift + ":" + upShift ) ; } //int currentBits = uRSHFT(input, downShift) & (upMask); int currentBits = ( downShift >= 32 ) ? 0 : ( input >>> downShift ) & upMask ; //shift bits back up to match offset //currentBits = lSHFT(currentBits, upShift); currentBits = ( upShift >= 32 ) ? 0 : currentBits << upShift ; //upMask = lSHFT((byte)upMask, upShift); upMask = ( upShift >= 32 ) ? 0 : ( ( byte ) upMask ) << upShift ; dest [ currentByte ] = ( byte ) ( dest [ currentByte ] & ( ~ upMask ) ) ; //merge bytes~ dest [ currentByte ] = ( byte ) ( dest [ currentByte ] | currentBits ) ; //System.out.println("new currentByte: " + dest[currentByte]); count -= bitRoom ; currentOffset = 0 ; currentByte ++ ; } } if ( DEBUG_LEV > 30 ) System . err . println ( "EncodedElement::packInt: End" ) ; }
Append an equal number of bits from each int in an array within given limits to the given byte array .