private static void addLong ( long input , int count , int startPos , byte [ ] dest ) { if ( DEBUG_LEV > 30 ) System . err . println ( "EncodedElement::addLong : Begin" ) ; int currentByte = startPos / 8 ; int currentOffset = startPos % 8 ; int bitRoom ; //how many bits can be placed in current byte long 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 = 255 >>> currentOffset ; upShift = 0 ; if ( downShift < 0 ) { //upMask = 255 >>> bitRoom-count; upShift = bitRoom - count ; upMask = 255 >>> ( currentOffset + upShift ) ; downShift = 0 ; } if ( DEBUG_LEV > 30 ) { System . err . println ( "count:offset:bitRoom:downShift:upShift:" + count + ":" + currentOffset + ":" + bitRoom + ":" + downShift + ":" + upShift ) ; } long currentBits = ( input >>> downShift ) & ( upMask ) ; //shift bits back up to match offset currentBits = currentBits << upShift ; upMask = ( 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::addLong : End" ) ; }
This method adds a given number of bits of a long to a byte array .