0

For my LZW compression code. I chose to store the codes in 9-bit codes, dictionary size will be 512, so there will be room for only 256 new symbols. Now I feel like I didn't choose the right buffer for the job:

byte[] buffer = new byte[3];

This buffer is more suited to store for 12 bits, what is the equivalent for 9 bits and how can I store the 9 bits in the buffer correctly?

I used this to store 8 bits in buffer[0] and 4 bits in buffer[1]. What is the equivalent for 9 bits?

buffer[0] = (byte) (code & 255);
buffer[1] = (byte) ((code >> 8) << 4);
Koolio
  • 1
  • 2
  • 8 bits in buffer[0] and 1 bit in buffer[1]? – Louis Wasserman Oct 16 '19 at 03:51
  • I thought of this, but does that mean I'll have only 2 bits in buffer[1]? Because my code now adds 4 other bits from the next code to buffer[1] and in buffer[2] adds the last remaining 8 – Koolio Oct 16 '19 at 03:54

1 Answers1

0

Nine is a hard bit count to work with. First question would be: Can you work in 8 bits?

Assuming not, I'd look at allocating at the dictionary level and packing your 9-bit words in without paying attention to byte boundaries. A 512 byte dictionary = 4096 bits = 455 9-bit symbols. You just need some math to access those symbols from your bitstream:

byte[] buffer = new byte[512];

function getWord(int wordOfs) {  
// Gets wordOfs'th 9-bit symbol from buffer, For buffer of 512 bytes, wordOfs = 0 -> 454
   if (wordOfs<0 || wordOfs>454) throw InvalidArgumentException;

   int bitsOfs = wordOfs * 9;  // Offset (in bits) of the desired 9 bit word 
   int idx = bitsOfs / 8;      // buffer[idx] contains bit 0 of the desired word
   int ofs = bitsOfs % 8;      // ... shifted this many places to the right

   // Grab at least 8 bits beyond the calculated starting point 
   unsigned word val = buffer[idx] | (buffer[idx+1]>>8);  
   // Shift and mask it down to the desired 9 bits for return
   return (val << ofs) & 0x01FF;
}

Caveat: I don't have access to a Java compiler right now, syntax may need work.

boB
  • 173
  • 10