I just had an idea, as I wanted to write a ranged integer that shouldn't consume more memory as it's needed for values between 0 and 4096

I remebered an Article from the GDmag some years ago, where they wrote about an "Arithmetic Coder" (i'm sure you know what it is).

So far, I think it would be great if you can do something like:

int n = 194;

stream.WriteRanged(n, 92, 212);

And it would just consume 7 bits because the range is only 120 and you can pack values from 0 to 120 into 7 bits.

The resulting code must be smth like this:

`WriteRanged(int value, int min, int max)`

{

int range = max-min;

int comprValue = value-min;

assert(comprValue >= 0);

int bits = ln(range) / LN_2; // LN_2 = ln(2), has to round up every time

WriteBits(reinterpret_cast<char*>(&comprValue),bits);

}

I'm not fully aware about the logarithmic operation, if it's correct. It might also be cycles consuming, but bandwidth is more expensive

(I testet the calculation with wincalc, seems to work perfectly

)

Edit:

Maybe this can improve the WriteDelta*-Methods, too.

If you know the "old value" on both sides, you can send the delta with only using as much bits

as neccessary (range 0->delta). You then just add the delta to the old value on the receiver side.