2.10 BCD: Binary-Coded Decimal Encoding
Encode a decimal number as BCD by translating each digit separately into 4 bits, and identify why bit patterns 1010-1111 are invalid BCD digits.
Binary-Coded Decimal (BCD) is a way of encoding decimal numbers where each individual decimal digit (0-9) is stored as its own separate 4-bit binary group, rather than converting the whole number into a single pure-binary value. BCD trades some storage efficiency for a very practical benefit: it makes converting to and from human-readable decimal digits (for seven-segment displays, calculators, and financial software) trivial, since each 4-bit group corresponds to exactly one decimal digit with no complex arithmetic conversion required.
Digit-by-digit encoding
In BCD, each decimal digit 0 through 9 is represented by its ordinary 4-bit binary equivalent: 0=0000, 1=0001, 2=0010, ..., 9=1001. A multi-digit decimal number is encoded by concatenating the 4-bit BCD codes of each of its digits in order, with no positional weighting applied across the whole number the way pure binary does — each group of 4 bits stays tied to exactly one decimal digit.
This is fundamentally different from converting the entire decimal number into pure binary. For example, decimal 59 in pure binary is 111011 (a single 6-bit binary value), but in BCD it is 0101 1001 — two separate 4-bit groups, one for the digit 5 and one for the digit 9, each independently following ordinary binary weighting only within its own 4 bits.
Packed vs unpacked BCD
Packed BCD stores two BCD digits per 8-bit byte, one in each nibble (4-bit half), making efficient use of memory — this is the typical storage format in hardware and older calculator/computer systems. Unpacked BCD stores only one BCD digit per byte, using just the lower 4 bits and leaving the upper 4 bits as zero (or sometimes used for other purposes such as a sign flag), which is simpler to process but uses twice the memory of packed BCD for the same digit count.
Invalid codes and BCD addition
Because a decimal digit only ever ranges from 0 to 9, only 10 of the 16 possible 4-bit patterns (0000 through 1001) are valid BCD digits; the remaining six patterns (1010 through 1111, corresponding to what would be 10-15 in pure binary) are invalid and must never appear in correctly formed BCD data. A circuit or software routine that encounters one of these six patterns in a BCD field knows immediately that the data is corrupted or malformed.
This restriction to only 10 valid codes per nibble also means ordinary binary addition of two BCD digits can produce an invalid intermediate result whenever the true decimal sum is 10 or greater (e.g., adding BCD 5 + BCD 6 in pure binary gives 1011, an invalid BCD pattern) — BCD addition circuits must detect this and apply a correction (adding 6, i.e., 0110) to skip over the six invalid codes and land back on the correct BCD digit plus a carry into the next decimal digit position.
BCD digit table
Only 10 of the 16 possible 4-bit patterns are valid BCD digits; the other 6 patterns are simply never used. Packed BCD places two digits per byte (one per nibble); unpacked BCD places one digit per byte.
Decimal digit -> BCD (4 bits):
0=0000 1=0001 2=0010 3=0011 4=0100
5=0101 6=0110 7=0111 8=1000 9=1001
Invalid BCD codes (never valid, 6 patterns): 1010 1011 1100 1101 1110 1111Code Examples
Each decimal digit is translated independently into its own 4-bit BCD code: the digit 5 becomes 0101, and the digit 9 becomes 1001. Concatenating these two nibbles gives the packed-BCD encoding of 59, which is visually distinguishable from pure binary 59 (111011) by the fact that it is naturally grouped in 4-bit chunks that each individually never exceed 9.
Decimal 59 in BCD is 0101 1001 (not the same as pure-binary 111011)
Decimal: 5 9
BCD: 0101 1001
59 (decimal) = 0101 1001 (BCD, packed)BCD nibble structure vs pure binary
BCD keeps each decimal digit as its own isolated 4-bit group; pure binary treats the whole number as one positionally-weighted value.
Decimal 59:
Pure binary: 1 1 1 0 1 1 (single 6-bit value, positional across all bits)
BCD: 0101 | 1001 (two independent 4-bit digit groups)
5 9Important Rules
Each decimal digit (0-9) is encoded independently as its own 4-bit binary group in BCD.
Only 10 of 16 possible 4-bit patterns (0000-1001) are valid BCD digits; 1010-1111 are invalid and never appear in correct BCD data.
Packed BCD stores two digits per byte; unpacked BCD stores one digit per byte.
BCD addition that produces an invalid intermediate code (>= 10 in a nibble) requires a +6 correction to skip the six unused codes and generate the correct carry.
Where this shows up
BCD is used heavily in seven-segment display drivers, digital clocks, calculators, and financial/accounting software, precisely because it avoids the rounding and conversion overhead of translating pure binary values into decimal digits for display or currency calculation, where exact decimal representation (not a binary approximation) is often a hard requirement.
Treating a BCD-encoded number as if it were a single pure-binary value and applying ordinary binary positional weighting across the whole bit string.
Interpret each 4-bit group of a BCD number independently as its own decimal digit — never sum the whole bit string with a single set of binary place weights.
BCD deliberately keeps each decimal digit isolated in its own nibble; mixing this up with pure binary interpretation of the same bits gives a completely different (and wrong) numeric value, since pure-binary 59 (111011) and BCD 59 (0101 1001) are different bit patterns entirely.
Assuming the 4-bit pattern 1010 is a valid BCD digit representing the value 10.
Recognize that 1010 is outside the valid BCD digit range and treat its presence in BCD data as a sign of corruption or a formatting error.
1010 is not a valid BCD digit because a decimal digit only uses 0000 through 1001. BCD deliberately never uses six of the sixteen 4-bit patterns, since a decimal digit can never itself be 10 or greater.
[tricky] Why is 1010 not a valid BCD digit? A. Because BCD only uses 3-bit groups, not 4-bit groups B. Because a decimal digit only ever uses the values 0000 through 1001; 1010 would represent 10, which is not a single decimal digit C. Because 1010 is reserved for the BCD carry flag D. Because BCD does not support even numbers
B. Because a decimal digit only ever uses the values 0000 through 1001; 1010 would represent 10, which is not a single decimal digit — 1010 is not a valid BCD digit because a decimal digit only uses 0000 through 1001 (values 0-9). Since a single decimal digit can never be 10 or higher, the six patterns 1010-1111 are simply never used in valid BCD data.
[interview] What correction must a BCD adder circuit apply when the binary sum of two BCD digits lands in the invalid range? A. Subtract 6 from the result B. Add 6 (0110) to the result to skip over the six unused codes and produce a correct BCD digit plus carry C. Discard the result and output zero D. No correction is ever needed; BCD addition is identical to pure binary addition
B. Add 6 (0110) to the result to skip over the six unused codes and produce a correct BCD digit plus carry — When adding two BCD digits produces a binary sum of 10 or more within a nibble, that raw sum lands on one of the six invalid BCD codes. Adding 6 shifts the result past those six invalid codes, producing the correct BCD digit for the ones place along with a carry into the next decimal digit position.
Encode the decimal number 384 in packed BCD, and state whether the bit pattern 1100 could ever legally appear in a correctly formed BCD number.
384 in packed BCD: 3=0011, 8=1000, 4=0100, giving 0011 1000 0100. The pattern 1100 is outside the valid BCD range of 0000-1001 (it falls in the invalid 1010-1111 block), so it can never legally appear in correctly formed BCD data.
Translate each digit separately using the BCD digit table; check 1100 against the valid range 0000-1001.