Problem Set - Binary-Coded Decimal
Tags: #◇Computer Science #◇CSARCH2 #◇Computer Architecture #◇Data Representation #◇Binary-Coded Decimal #◇Decimal Arithmetic
◇Overview
Binary-Coded Decimal (BCD) is a class of digital encodings where each decimal digit ( through ) is represented by a dedicated sequence of binary bits. Unlike pure positional binary notation, BCD eliminates fractional precision loss during base-10 financial and commercial arithmetic, providing exact decimal representation for banking, tax accounting, and human-facing embedded displays. This problem set investigates the architectural formats of BCD—including Unpacked BCD, Packed BCD, and Densely Packed BCD (DPD) (as standardized in IEEE 754-2008 decimal floating-point)—alongside weighted codes (, ), self-complementing codes (Excess-3), and the arithmetic correction mechanics of BCD addition.
◇Key Definitions
- Binary-Coded Decimal (BCD): An encoding format in which each decimal digit of a number is individually converted into a fixed-width binary pattern.
- Natural BCD (8421 Code): The most common weighted BCD format, where the 4 bits of each nibble represent weights , , , and . The 10 valid codes are () through (). Bit patterns () are invalid (pseudo-tetrades).
- Unpacked BCD: A representation where each decimal digit is stored in an entire 8-bit byte ( octet). The upper 4 bits are conventionally padded with zeros () or ASCII zone bits (), while the lower 4 bits hold the BCD digit.
- Packed BCD: A more compact representation where two decimal digits are packed into a single 8-bit byte (4 bits per digit). For decimal digits, it requires bytes.
- Signed Packed BCD: Packed BCD representation appending a sign nibble at the least significant position. By convention (derived from accounting terminology for Credit and Debit):
- Positive sign ( / Credit): ()
- Negative sign ( / Debit): ()
- Densely Packed BCD (DPD): A highly efficient compression algorithm that encodes decimal digits ( BCD bits) into exactly binary bits (Declet), achieving a storage efficiency of (compared to pure binary's theoretical limit of bits). Developed by Mike Cowlishaw based on Chen-Ho encoding.
- Excess-3 Code (Stibitz Code): An unweighted, self-complementing decimal code formed by adding () to each 8421 BCD digit. The 9's complement of an Excess-3 number is obtained directly by bitwise inverting (1's complement) its bits.
- 2421 Code (Aiken Code): A weighted, self-complementing code with bit weights .
- BCD Addition Correction ( Rule): When adding two BCD digits in binary, if the intermediate sum produces a value greater than () or generates a binary carry-out (), adding () skips the invalid states () and produces the correct BCD digit and decimal carry.
◇Theoretical Framework & Encoding Tables
1. Comparison of Decimal Encodings
| Decimal Digit | 8421 BCD (Natural) | Excess-3 () | 2421 (Aiken Code) | Unpacked BCD (Byte) |
|---|---|---|---|---|
| 0 | 0000 | 0011 | 0000 | 0000 0000 |
| 1 | 0001 | 0100 | 0001 | 0000 0001 |
| 2 | 0010 | 0101 | 0010 | 0000 0010 |
| 3 | 0011 | 0110 | 0011 | 0000 0011 |
| 4 | 0100 | 0111 | 0100 | 0000 0100 |
| 5 | 0101 | 1000 | 1011 | 0000 0101 |
| 6 | 0110 | 1001 | 1100 | 0000 0110 |
| 7 | 0111 | 1010 | 1101 | 0000 0111 |
| 8 | 1000 | 1011 | 1110 | 0000 1000 |
| 9 | 1001 | 1100 | 1111 | 0000 1001 |
2. Densely Packed BCD (DPD) Forward Encoding Table
Let three decimal digits be represented in 8421 BCD as 12 bits:
where are the Most Significant Bits (MSBs) indicating whether each digit is () or ().
DPD maps these 12 bits into a 10-bit declet: using the control bits :
| Description / Active Range | |||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | 0 | 0 | 0 | All 3 digits (Small-Small-Small) | |||||||||
0 | 0 | 1 | 1 | 0 | 0 | Digit 3 is (Small-Small-Large) | |||||||
0 | 1 | 0 | 1 | 0 | 1 | Digit 2 is (Small-Large-Small) | |||||||
0 | 1 | 1 | 1 | 0 | 1 | 1 | 1 | Digits 2 & 3 are (Small-Large-Large) | |||||
1 | 0 | 0 | 1 | 1 | 0 | Digit 1 is (Large-Small-Small) | |||||||
1 | 0 | 1 | 0 | 1 | 1 | 1 | 1 | Digits 1 & 3 are (Large-Small-Large) | |||||
1 | 1 | 0 | 0 | 0 | 1 | 1 | 1 | Digits 1 & 2 are (Large-Large-Small) | |||||
1 | 1 | 1 | 0 | 0 | 1 | 1 | 1 | 1 | 1 | All 3 digits (Large-Large-Large) |
3. Densely Packed BCD (DPD) Reverse Decoding Table
To reconstruct the original 12 BCD bits from the 10-bit declet , we inspect the control bits (and when ):
| Control Bits | Digit 1 () | Digit 2 () | Digit 3 () | Decoded Condition |
|---|---|---|---|---|
0 . . . . | 0 | 0 | 0 | : all three digits |
1 0 0 . . | 0 | 0 | 1 0 0 | |
1 0 1 . . | 0 | 1 0 0 | 0 | |
1 1 0 . . | 1 0 0 | 0 | 0 | |
1 1 1 0 0 | 1 0 0 | 1 0 0 | 0 | |
1 1 1 0 1 | 1 0 0 | 0 | 1 0 0 | |
1 1 1 1 0 | 0 | 1 0 0 | 1 0 0 | |
1 1 1 1 1 | 1 0 0 | 1 0 0 | 1 0 0 |
4. BCD Addition & Correction Mechanics
In 8421 BCD arithmetic, adding two 4-bit digits using a standard binary adder yields an intermediate 4-bit sum and binary carry :
The Correction Condition:
A correction is required if and only if:
- The intermediate binary sum exceeds (), meaning , OR
- A binary carry-out is generated ().
Boolean correction detection function:
When :
- Add () to the intermediate 4-bit sum.
- Propagate a decimal carry to the next higher decimal digit position.
Why ? A 4-bit register has states, but decimal only uses states (). Adding skips the invalid states, aligning the arithmetic modulo-16 accumulator with base-10 modulo arithmetic ().
◇Worked Examples and Step-by-Step Problem Solutions
Section 1: Forward Decimal to BCD Conversions
Problem 1: Encoding Decimal
Problem Statement:
Convert the 3-digit decimal number into:
- Packed BCD
- Unpacked BCD
- Densely Packed BCD (DPD)
Show bit-level derivations and total bit counts.
Derivation:
- Decimal digits: .
- 4-bit 8421 BCD for each digit:
-
Packed BCD:
- Concatenate 4-bit nibbles:
- Total Bit Count: bits ( bytes).
- Concatenate 4-bit nibbles:
-
Unpacked BCD:
- Pad each 4-bit nibble to 8 bits with leading zeros:
- Total Bit Count: bits ( bytes).
- Pad each 4-bit nibble to 8 bits with leading zeros:
-
Densely Packed BCD (DPD):
- Check the MSBs: (All 3 digits ).
- Lookup rule for :
- Substitute variable LSBs ():
-
- Form 10-bit declet:
- Total Bit Count: bits.
- Form 10-bit declet:
Problem 2: Encoding Decimal
Problem Statement:
Convert the 4-digit decimal number into:
- Packed BCD
- Unpacked BCD
- Densely Packed BCD (DPD)
Show bit-level derivations and total bit counts.
Derivation:
- Decimal digits: .
- 4-bit 8421 BCD for each digit:
- Digit
- Digit
- Digit
- Digit
-
Packed BCD:
- Concatenate 4-bit nibbles:
- Total Bit Count: bits ( bytes).
- Concatenate 4-bit nibbles:
-
Unpacked BCD:
- Pad each 4-bit nibble to 8 bits:
- Total Bit Count: bits ( bytes).
- Pad each 4-bit nibble to 8 bits:
-
Densely Packed BCD (DPD):
-
DPD operates on 3-digit declet groups. A 4-digit number can be structured in two standard architectural conventions:
-
Convention A (Standard 2 Declets with 6-digit Zero Padding):
Pad to digits: ( full declets).
-
-
Declet 1 ():
-
-
Rule:
-
Declet 2 ():
-
-
Rule:
-
Combined 2-declet DPD ( bits):
- Convention B (1 Leading BCD Digit + 1 Declet for Lowest 3 Digits):
-
Leading digit ( bits)
-
Lowest 3 digits ( bits)
-
Combined DPD ( bits):
Section 2: Reverse Densely Packed BCD (DPD) Decoding
Problem 3: Decoding DPD Declet 1001101011
Problem Statement:
Given the 10-bit Densely Packed BCD string1001101011, reconstruct the original 3-digit decimal number using the DPD analysis method.
Step 1: Map 10 Bits to Declet Variables
Step 2: Analyze Control Bits ()
- Control bits: .
Step 3: Lookup Expansion Pattern
From the DPD decoding table, pattern corresponds to:
- Digit 1 ():
- Digit 2 ():
- Digit 3 ():
Step 4: Substitute Bit Values
- Digit 1 ():
- Digit 2 ():
- Digit 3 ():
Step 5: Verification
-
Re-encode decimal :
- (Matches).
-
Final Decoded Decimal:
Problem 4: Decoding DPD Declet 0100101111
Problem Statement:
Given the 10-bit Densely Packed BCD string0100101111, reconstruct the original 3-digit decimal number using the DPD analysis method.
Step 1: Map 10 Bits to Declet Variables
Step 2: Analyze Control Bits ()
- Primary control bits: .
- Secondary control bits: .
- Combined control code: .
Step 3: Lookup Expansion Pattern
From the DPD decoding table, pattern corresponds to:
- Digit 1 ():
- Digit 2 ():
- Digit 3 ():
Step 4: Substitute Bit Values
- Digit 1 ():
- Digit 2 ():
- Digit 3 ():
Step 5: Verification
-
Re-encode decimal :
- (Matches).
-
Final Decoded Decimal:
Section 3: BCD Arithmetic & Multi-Format Storage Comparison
Problem 5: Step-by-Step BCD Addition ()
Problem Statement:
Perform the addition of decimal numbers and using 8421 BCD arithmetic. Show binary addition, correction steps, and carry generation.
Step-by-Step Execution:
-
Represent in 8421 BCD:
-
Add Least Significant Digits (Units Position: ):
『』TEXT0111 (7) + 1000 (8) ------ 1111 (Raw binary sum = 15)- Check condition: Raw sum .
- Correction: Add ():
『』TEXT1111 (15) + 0110 (6) ------ 10101 -> Carry out = 1, Corrected sum nibble = 0101 (5) -
Add Most Significant Digits (Tens Position: ):
『』TEXT0110 (6) + 0101 (5) + 0001 (Carry in = 1) ------ 1100 (Raw binary sum = 12)- Check condition: Raw sum .
- Correction: Add ():
『』TEXT1100 (12) + 0110 (6) ------ 10010 -> Carry out = 1, Corrected sum nibble = 0010 (2) -
Assemble Final Result:
- Hundreds digit: ()
- Tens digit: ()
- Units digit: ()
Comprehensive Multi-Format Storage Comparison Table
| Decimal Value | Unsigned Binary (Min Bits) | Packed BCD (8421) | Unpacked BCD | Densely Packed BCD (DPD) |
|---|---|---|---|---|
111 ( bits) | 0111 ( bits) | 0000 0111 ( bits) | 000 000 0 111 ( bits) | |
110100 ( bits) | 0101 0010 ( bits) | 00000101 00000010 ( bits) | 000 101 0 010 ( bits) | |
10110111 ( bits) | 0001 1000 0011 ( bits) | 00000001 00001000 00000011 ( bits) | 001 010 1 011 ( bits) | |
100000000 ( bits) | 0010 0101 0110 ( bits) | 00000010 00000101 00000110 ( bits) | 010 101 0 110 ( bits) | |
111101001 ( bits) | 0100 1000 1001 ( bits) | 00000100 00001000 00001001 ( bits) | 100 100 1 111 ( bits) | |
1111011101 ( bits) | 1001 1000 1001 ( bits) | 00001001 00001000 00001001 ( bits) | 001 110 1 111 ( bits) | |
1111100111 ( bits) | 1001 1001 1001 ( bits) | 00001001 00001001 00001001 ( bits) | 001 111 1 111 ( bits) | |
100101100001 ( bits) | 0010 0100 0000 0001 ( bits) | 4 \text{ bytes} ( bits) | 0000000010 1000000001 ( bits) |
◇Common Pitfalls & Edge Cases
[!warning] Key BCD Implementation Pitfalls
- Omission of the Correction on Binary Carry: When a 4-bit addition generates a binary carry (), even if the raw sum nibble appears (e.g., , where lower nibble is ), adding is mandatory to produce the correct digit ().
- Illegal States in 8421 BCD: Nibbles in the range () are undefined in standard BCD. Decoders should treat these as invalid data exceptions unless assigned as sign codes ().
- DPD Bit Grouping Ordering: DPD places the primary selector bit at position index (bit from the left). Misaligning with payload variables will corrupt the entire decoded decimal triplet.
- Self-Complementing Codes: Excess-3 is self-complementing, meaning bitwise inversion directly yields the 9's complement. Natural 8421 BCD is not self-complementing, requiring explicit subtraction logic ().
◇References & Navigation
- Related Notes: ◇Integer Representation & Fixed-Point Arithmetic