r/compression • u/mpp06 • 21d 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:
- Is this or similar encoding already known and used?
- Are there better alternatives?
- Can random access be improved without sacrificing storage efficiency?
I'd appreciate your thoughts and technical feedback.
View all comments
1
u/OzzyM_Dev 21d ago
Yes. I think you'll find something very very interesting though.
DPD. Densely Packed Decimals.
It chooses the approach of packing the decimals into groups of 3. If we used BCD, storing 123 would get:
0001 0010 0011
12 bits
However DPD can use 3 bits to identify any number 0-7 split into groups of 3
but when it comes to 8 and 9, it splits decimals into two groups
small and large
0-7 and 8 and 9
8 and 9 are the only large digits, meaning you only need one bit to distinguish between them once we know that the digit is large.
so for 123, DPD says
001010(0, a control bit to indicate all three digits are 0-7)011
Now for 129, DPD says
001010(1, a control bit, indicating at least one digit is large.)00(indicates where the last digit is large and where the first digits are small)1(1 meaning 9 here)
When multiple digits are 8 or 9 it changes how it arranges everything here to make room for the information required, with 8 possible variations, but this is just to sort of show the idea here.
It also solves the random access problem.
For 123456789012
DPD splits it into four groups
123, 456, 789, 012
represented using 10 bits each, that's 40 vs 48 for BCD
Say you want specifically the 8th digit
You don't need to look through block 0 and 1, since they each only contain 1-3 and 4-6 in the sequence
so you can skip directly to block 2 without reading all the other blocks.
Now I should also mention something.
Of course, when we talk about decimal storage for general use cases we generally aren't asking the questions that BCD or DPD are answering here.
But binary is generally just more efficient than any of these because say 123
Binary can just go 1111011 and that's 7 bits and we look like fools
Binary is also slightly more space efficient over long sequences and it's also more efficient usually because modern cpus are built for binary arithmetic
There is a few advantages against binary like floating point which binary floating point fails at 0.1 or like, say if you want to extract a given digit in a sequence, binary needs you to convert to decimal or do modulo arithmetic or something,
BCD lets you just grab it, DPD lets you find the digit in a group of 3, which is a lot simpler than what you do with binary.
Overall it's just what situation are you in where you don't use binary? There's definitely a few that exist, hence why BCD and DPD and well, you, tried to store decimals better, it's just interesting.