I've been trying to make sense of an example inflate implementation and this is a bit confusing:
static int decode(struct state *s, struct huffman *h)
{
int len; /* current number of bits in code */
int code; /* len bits being decoded */
int first; /* first code of length len */
int count; /* number of codes of length len */
int index; /* index of first code of length len in symbol table */
code = first = index = 0;
for (len = 1; len <= MAXBITS; len++) {
code |= bits(s, 1); /* get next bit */
count = h->count[len];
if (code - count < first) /* if length len, return symbol */
return h->symbol[index + (code - first)];
index += count; /* else update for next length */
first += count;
first <<= 1;
code <<= 1;
}
return -10; /* ran out of codes */
}
In particular this part:
first += count;
first <<= 1;
Can someone explain how does a bitshift or a multiplication by a constant yield the actual first code of a particular code length? Basically, the difference between an index of the first code and the actual first code is "times 2"?
For simplicity, just consider it rolling off from 7 to 8 bits of length, where 7 yielded the first non-zero count, in a fixed version would be 24, I guess since the interval is [256, 280>.
My assumption is that it preserves orders of magnitude between the code and the first of the next length, since their subtractive relationship is more relevant than the absolute value to determine the symbol offset -- however - I'm not quite sure that is the case.