# cbloom rants

## 1/31/2014

### 1-31-14 - Understanding ANS - 2

So last time I wrote about how a string of output symbols like "012" describes an ANS state machine.

That particular string has all the values occuring the same number of times in as close to the same slot as possible. So they are encoded in nearly the same code length.

But what if they weren't all the same? eg. what if the decode string was "0102" ?

Then to decode, we could take (state % 4) and look it up in that array. For two values we would output a 0.

Alternatively we could say -

```
if the bottom bit is 0, we output a 0

if the bottom bit is 1, we need another bit to tell if we should output a 1 or 2

```
So the interesting thing is now to encode a 0, we don't need to do state *= 4. Our encode can be :
```
void encode(int & state, int val)
{
ASSERT( val >= 0 && val < 3 );
if ( val == 0 )
{
state = state*2;
}
else
{
state = state*4 + (val-1)*2 + 1;
}
}

```
When you encode a 0, the state grows less. In the end, state must be transmitted using log2(state) bits, so when state grows less you send a value in fewer bits.

Note that when you decode you are doing (state %4), but to encode you only did state *= 2. That means when you decode you will see some bits from previously encoded symbols in your state. That's okay because those different values for state all correspond to the output. This is why when a symbol occurs more often in the output descriptor string it can be sent in fewer bits.

Now, astute readers may have noticed that this is a Huffman code. In fact Huffman codes are a subset of ANS, so let's explore that subset.

Say we have some Huffman codes, specified by code[sym] and codelen[sym]. The codes are prefix codes in the normal top-bit first sense. Then we can encode them thusly :

```
void encode(int & state, int val)
{
state <<= codelen[sym];
state |= reversebits( code[sym] ,  codelen[sym] );
}

```
where reversebits reverses the bits so that it is a prefix code from the bottom bit. Then you can decode either by reading bits one by one to get the prefix code, or with a table lookup :
```
int decode(int & state)
{
int bottom = state & ((1<`<`maxcodelen)-1);
int val = decodetable[bottom];
state >>= codelen[val];
return val;
}

```
where decodetable[] is the normal huffman fast decode table lookup, but it looks up codes that have been reversed.

So, what does this decodetable[] look like? Well, consider the example we did above. That corresponds to a Huffman code like this :

```
normal top-bit prefix :

0: 0
1: 10
2: 11

reversed :

0:  0
1: 01
2: 11

```
so the maxcodelen is 2. We enumerate all the 2-bit numbers and how they decode :
```
00 : 0
01 : 1
10 : 0
11 : 2

decodetable[] = { 0,1,0,2 }

```
So decodetable[] is the output state string that we talked about before.

Huffman codes create one restricted set of ANS codes with integer bit length encodings of every symbol. But this same kind of system can be used with more general code lens, as we'll see later.