Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>A very simple approach is to write one bit at a time with something like the following:</p> <pre><code>unsigned char acc; // Accumulator of bit waiting to be written int bitcount; // How many bits are aready present in the accumulator // write a single bit (0/1) void writebit(int bit) { acc |= (bit &lt;&lt; bitcount); if (++bitcount == 8) { writebyte(acc); acc = 0; bitcount = 0; } } </code></pre> <p>to read back a sigle bit the procedure is symmetrical</p> <pre><code>unsigned char acc; // bits waiting to be extracted int bitcount; // how many bits are still available in acc int readbit() { if (bitcount == 0) { bitcount = 8; acc = readbyte(); } --bitcount; return (acc &gt;&gt; (7 - bitcount)) &amp; 1; } </code></pre> <p>of course this is just the simplest approach, but I'd wait before worrying about code speed until you are first able to save and load correctly your encoded data.</p> <h2>Example:</h2> <p>Suppose you have the following Huffman coded symbols</p> <pre><code>A - 0 B - 10 C - 110 D - 111 </code></pre> <p>and that you want to encode the sequence</p> <pre><code>A B A A C D A D B B </code></pre> <p>then you would call in order</p> <pre><code>writebit(0); // A writebit(1); writebit(0); // B writebit(0); // A writebit(0); // A writebit(1); writebit(1); writebit(0); // C writebit(1); writebit(1); writebit(1); // D writebit(0); // A writebit(1); writebit(0); // B writebit(1); writebit(0); // B </code></pre> <p>The actual bytes written would therefore be</p> <pre><code>(01100010) = 0x62 (01010111) = 0x57 </code></pre> <p>(Note that the code shown starts from the least significant bit, i.e. you should read the bit sequences inside the parenthesis from right to left if you want to recognize the symbols).</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload