CSC 499 S09: Protocols

From CSWiki

Jump to: navigation, search

Front Door

HuffChar

For maintainability and flexiblity, we've decided to separate the code that encodes/decodes Huffman codes and the code that reads/writes Huffman codes. The reader/writer is responsible for (un)packing Huffman codes to fit as many characters per integer as possible. The (de)coder is responsible for providing the reader/writer with the information it needs to tightly (un)pack the Huffman-encoded characters.

A HuffChar will live inside a 32-bit C integer. The leftmost 27 bits will consist of the Huffman encoded character, most significant digit first, padded on the left by zereos. The rightmost 5 bits will represent a binary integer telling how many bits the Huffman-encoded character is, least significant digit first.

So, as an example, consider the Huffchar: 00000000000000000000001101001100 We start by reading from the right. The rightmost 5 bits are 01100. Remembering that the least significant digit is first, we can read it as a convential binary number by reversing it: 00110 = 6. This tells us that the Huffman encoded character is 6 bits, and that the rest of the HuffChar consists of padding zeroes. Reading the next 6 bits to the left, we get a Huffman encoding of 011010.

Personal tools