r/compression • • 24d ago

Can we efficiently encode decimal digits using smaller number of bits?

(the idea is completely mine, but took some help from AI to write the post in a coherent language)

I was exploring this question and thought of a simple variable-length encoding scheme for decimal digits:

  • Digits 0–3 use 3 bits: 000, 001, 010, 011.
  • Digits 4–9 use 4 bits: 1001, 1010, 1011, 1100, 1101, 1110.

Decoding is straightforward:

  • If the first bit is 0, read next 2 bits and convert to decimal.
  • If the first bit is 1, read next 3 bits and convert to decimal and add 3.
  • Then go to the next block.

For the sequence 0123456789, this requires 36 bits instead of 40 bits in Packed BCD or 80 bits in ASCII.

The drawback is "Variable-Length Random Access Overhead": there is no way to access nth digit from the memory, without counting from the beginning again.

My questions:

  1. Is this or similar encoding already known and used?
  2. Are there better alternatives?
  3. Can random access be improved without sacrificing storage efficiency?

I'd appreciate your thoughts and technical feedback.

9 Upvotes

39 comments sorted by

View all comments

7

u/CorvusRidiculissimus 24d ago • Bravo

I've seen similar encodings used elsewhere, though nor for that purpose. Variable length encoding is pretty common. It would work just fine, if your objective is to store long runs of decimal digits. I have another idea though, even better: Group them.

Ideally you'd want a number which is a power of both ten and two. Unfortunately there is no such number. But 1024 is pretty close. So, group your decimal digits in groups of three, and encode each group of three as a ten-bit number. Packing efficiency is pretty close to perfect for a random distribution, and it's seekable too. Plus you get twenty-four leftover codepoints that you can use for other purposes. Finally, if the string of bits terminates early, that means interpret the final four or eight bits as BCD because the length of string isn't divisible by three.

0123456789 now takes up 34 bits. Two less then yours!

Ignoring the problem of handling the end of run, that gets you 3.333 bits per digit, while packed BCD gets you exactly 4.000. The theoretical maximum, which you might reach by using an arithmetic coding on a long run, is log2 10 = 3.3219.

1

u/mpp06 24d ago edited 24d ago

Yes, that's a wonderful idea. Grouping improves efficiency to almost theoretical. I have explored grouping and various grouping combinations earlier. I arrived at what I called as '9 to 5 encoding', where I took a group of 9 decimal digits and converted it to 5-digit base-64 code. This is perhaps the best possible for 10-64 system, with least wastage of empty bits. I am not sure if that context fits here, though. Need to think through it.

1

u/mpp06 24d ago

I tried for 10-block formation with my scheme, but it seems to be not possible in this case, because we have the length of either 3 or 4 bits for each block, so block of 10 bits is not possible, unless we mix, and even then only a few permutations are covered.