Sunday, 15 September 2013

How can SQL store 9 digits in 4 bytes?

How can SQL store 9 digits in 4 bytes?

MySQL 5.0 Reference, 11.2. Data Type Storage Requirements:
As of MySQL 5.0.3, values for DECIMAL columns are represented using a
binary format that packs nine decimal (base 10) digits into four bytes.
Storage for the integer and fractional parts of each value are determined
separately. Each multiple of nine digits requires four bytes, and the
"leftover" digits require some fraction of four bytes. The storage
required for excess digits is given by the following table.
I can imagine storing 8 BCD digits in 4 bytes (each in 4 bits), so the
result would be (MSB first)
112 => 0x01 0x12
148,225 => 0x14 0x82 0x25
12,310,789 => 0x12 0x31 0x07 0x89
In other words, I know how to store 10 possible values (0 - 9) in each 4
bits.
How does MySQL store 9 decimal digits in 4 bytes?

No comments:

Post a Comment