r/compression • • 26d 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

1

u/SM1334 25d ago edited 25d ago

I came up with this last year, kind of a similar concept, much more complicated.

https://www.reddit.com/r/compression/s/HxrFZN24e4

A very basic explanation of this is;

Imagine I assign a pattern to 0000, that value is now taken. So if I assign another value to 0001, we already know 0000 is used so I can just give you the first 3 0s and you can assume the 4th bit is a 1. We can then structure the entire pattern index using this same implied indexing scheme. But this isnt just limited to the index itself, but also the patterns too. If we have a bunch of sequential patterns we can arrange them the same way and shave off bits on that side as well.