Packed versus unpacked BCD for 259

exam standardtwo digits per byte, or one

Answer

000000100000010100001001

Why this example is worth doing

Packed BCD stores two digits per byte; unpacked stores one digit per byte with the high nibble unused or set to a fixed value. The page shows both for the same number and explains the trade: unpacked wastes twice the space but converts to ASCII digits by adding 48, so it is what you use immediately before display. This is the same 48 that appears on the binary-to-text page.

Try your own input in the BCD converter. Pack and unpack binary-coded decimal, and add with the +6 correction shown.

How the answer is reached

259 in unpacked BCD

Each decimal digit becomes its own 8-bit group with weights 8-4-2-1. The six patterns 1010..1111 are never produced.

259 in unpacked BCD — columns digit, 8421 code, placement
digit8421 codeplacement
20010low nibble, high nibble zero
50101low nibble, high nibble zero
91001low nibble, high nibble zero

unpacked BCD0000 0010 0000 0101 0000 1001

compare with plain binary100000011BCD is longer: it trades density for digit-by-digit decimal arithmetic

Compare with

Open this example in the BCD converter

The field arrives filled in with this example’s input.

Note:

Notation this page assumes

  • Bit strings are written most significant bit first, and bit 0 is the least significant bit.
  • A width is stated explicitly wherever it changes the answer; nothing is silently sign-extended or truncated.
  • Fractions are converted digit by digit and shown to a stated number of places rather than rounded silently.

Sources

  • Knuth, The Art of Computer Programming, Vol. 2, §4.1 “Positional Number Systems” (1997)
  • ANSI X3.4 / ISO-IEC 646, the ASCII character set