Programming


Binary Coded Decimals (BCD or "8421" BCD)

Encoding a BCD Number

Definition
BCD represents each of the digits of an unsigned decimal as the 4-bit binary equivalents.
Unpacked BCD
Unpacked BCD representation contains only one decimal digit per byte. The digit is stored in the least significant 4 bits; the most significant 4 bits are not relevant to the value of the represented number.
Packed BCD
Packed BCD representation packs two decimal digits into a single byte.
DecimalBinaryBCD
UnpackedPacked
00000 0000 0000 00000000 0000
10000 0001 0000 00010000 0001
20000 0010 0000 00100000 0010
30000 0011 0000 00110000 0011
40000 0100 0000 01000000 0100
50000 0101 0000 01010000 0101
60000 0110 0000 01100000 0110
70000 0111 0000 01110000 0111
80000 1000 0000 10000000 1000
90000 1001 0000 10010000 1001
100000 1010 0000 0001 0000 00000001 0000
110000 1011 0000 0001 0000 00010001 0001
120000 1100 0000 0001 0000 00100001 0010
130000 1101 0000 0001 0000 00110001 0011
140000 1110 0000 0001 0000 01000001 0100
150000 1111 0000 0001 0000 01010001 0101
160001 0000 0000 0001 0000 01100001 0110
170001 0001 0000 0001 0000 01110001 0111
180001 0010 0000 0001 0000 10000001 1000
190001 0011 0000 0001 0000 10010001 1001
200001 0100 0000 0010 0000 00000010 0000
Invalid BCD Numbers
These binary numbers are not allowed in the BCD code:  1010, 1011, 1100, 1101, 1110, 1111

Packing a Two-Byte BCD

To pack a two-byte unpacked BCD number into a single byte creating a packed BCD number, shift the upper byte left four times, then OR the results with the lower byte.

For example,

0000 0111 0000 1001(unpacked BCD)  =  0111 1001(packed BCD)
 
0000 0111 << 4  =  0111 0000  (SHIFT LEFT 4)
0111 0000 + 0000 1001  =  0111 1001  (OR)


Converting between Decimal and BCD

From Decimal to Unpacked BCD:   To convert a decimal number into an unpacked BCD number, assign each decimal digit its 8-bit binary equivalent.

For example,

194(base 10) = 00000001 00001001 00000100(unpacked BCD)               1
       9
       4
(base 10)
00000001 00001001 00000100 (BCD)
     MSB        LSB  

From Decimal to Packed BCD:   To convert a decimal number into a packed BCD number, assign each digit of the decimal to its 4-bit equivalent, padding the upper nybble with zeroes if necessary.

For example,

238(base 10) = 00000010 00111000(packed BCD)           2
   3
   8
(base 10)
0010 0011 1000 (BCD)
 MSB    LSB  

From BCD to Decimal:   To convert a BCD into a decimal number, just reverse the appropriate process from above; beginning with the LSB, group the binary digits by either 4 or 8 bits for packed and unpacked, respectively, then convert each set into its decimal equivalent.


Converting between Binary and BCD

From Binary to Unpacked BCD:   To convert a binary number into an unpacked BCD, divide the binary number by decimal 10 and place the quotient in the most significant byte and the remainder in the least significant byte.

For example,

00110101(base 2) = 00000101 00000011(unpacked BCD)
 
  0011 0101  =  53(base 2)  
÷ 0000 1010
 =  10(base 10)
  0000 0101  =   5 such that           5
0000 0101
        3
0000 0011
with a remainder of
  0000 0011  =   3       MSB       LSB

From Two-Byte Unpacked BCD to Binary:   To convert from a two-byte unpacked BCD to a binary number, multiply the most significant byte of the BCD by decimal ten, then add the product to the least significant byte.

For example,

00001001 00000010(unpacked BCD) = 01011100(base 2)

  0000 1001 =  9
× 0000 1010
 = 10(base 10)
  0101 1010 = 90(base 10)
 
  0101 1010 = 90(base 10)
+ 0000 0010
 =  2
  0101 1100 = 92(base 10)


BCD Addition

Either packed or unpacked BCD numbers can be summed. BCD addition follows the same rules as binary addition. However, if the addition produces a carry and/or creates an invalid BCD number, an adjustment is required to correct the sum. The correction method is to add 6 to the sum in any digit position that has caused an error.

For example,

24 + 13 = 37              15 + 9 = 24              19 + 28 = 47
 
  0010 0100 = 24                0001 0101 = 15                 0001 1001 = 19 
+ 0001 0011
 = 13 + 0000 1001
 =  9  + 0010 1000
 = 28 
  0011 0111 = 37   0001 1110 = 1?(invalid)   0100 0001 = 41(error)
 
                   0001 1110 = 1?(invalid)                0100 0001 = 41(error)
    + 0000 0110
 =  6(adjustment) + 0000 0110
 =  6(adjustment)
      0010 0100 = 24    0100 0111 = 47 


BCD Subtraction

Either packed or unpacked BCD numbers can be subtracted. BCD subtraction follows the same rules as binary subtraction. However, if the subtraction causes a borrow and/or creates an invalid BCD number, an adjustment is required to correct the answer. The correction method is to subtract 6 from the difference in any digit position that has caused an error.

For example,

37 - 12 = 25              65 - 19 = 46              41 - 18 = 23
 
  0011 0111 = 37                0110 0101 = 65                 0100 0001 = 41 
- 0001 0010
 = 12 - 0001 1001
 = 19  - 0001 1000
 = 18 
  0010 0101 = 25   0100 1100 = 4?(invalid)   0010 1001 = 29(error)
 
                   0100 1100 = 4?(invalid)                0010 1001 = 29(error)
    - 0000 0110
 =  6(adjustment) - 0000 0110
 =  6(adjustment)
      0100 0110 = 46    0010 0011 = 23 


Unpacked BCD Multiplication

Multiplication cannot be performed on packed BCD; the 4 most significant bits must be zeroed for the adjustment to work. Multiply the two unpacked BCD numbers using the rules for binary multiplication. To adjust the product divide it by decimal 10, then place the quotient in the most significant byte and the remainder in the least significant byte (convert the binary answer to unpacked BCD).

For example,

00001001 × 00000100 = 00000011 00000110
 
  0000 1001  =   9    
× 0000 0100
 =   4  
  0010 0100  =  24 (error)
 
  0010 0100  =  24 (error)
÷ 0000 1010
 =  10(base 10) (adjustment)
  0000 0011  =   3  such that          3
0000 0011
        6
0000 0110
with a remainder of
  0000 0110  =   6       MSB       LSB


Unpacked BCD Division

BCD division also cannot be performed on packed numbers. Before dividing an unpacked BCD number, the division adjustment is made by converting the BCD numbers to binary. Adjust the two-byte BCD number by multiplying the upper byte by decimal 10 and adding the product to the lower byte. After the adjustment, divide the two binary numbers using the rules of binary arithmetic. Finally, convert the binary quotient into an unpacked BCD number if necessary.

For example,

00000010 00001000 ÷ 00000111 = 00000100
(28 ÷ 7 = 4)
 
  0000 0010 =  2   
× 0000 1010
 = 10(base 10)(adjustment)
  0001 0100 = 20(base 10) 
 
  0001 0100 = 20(base 10) 
+ 0000 1000
 =  8 
  0001 1100 = 28(base 10) 
 
  0001 1100 = 28(base 10) 
÷ 0000 0111
 =  7 
  0000 0100 =  4 
 
 
00000101 00000010 ÷ 00000100 = 00000001 00000011
(52 ÷ 4 = 13)
 
  0000 0101 =  5   
× 0000 1010
 = 10(base 10)(adjustment)
  0011 0010 = 50(base 10) 
 
  0011 0010 = 50(base 10) 
+ 0000 0010
 =  2 
  0110 0100 = 52(base 10) 
 
  0110 0100 = 52(base 10) 
÷ 0000 0100
 =  4 
  0000 1101 =  ?(invalid)
 
  0000 1101 =  ?(invalid)
÷ 0000 1010
 = 10(base 10)(adjustment)
  0000 0001 =  1  such that          1
0000 0001
        3
0000 0011
with a remainder of
  0000 0011 =  3       MSB       LSB


Other Decimal Codes

ASCII Representation of Digits
ASCII digits are examples of unpacked binary coded decimals. A digit is represented as its 4-bit binary equivalent, which is stored in the lower nybble. The upper nybble contains 011 and has no bearing on the value of the represented number.
 
EBCDIC Representation of Digits
EBCDIC digits are similiar to ASCII digits. The lower nybble still contains the 4-bit binary equivalent, but the upper nybble is padded with 1111 instead of 011.
 
Excess-3 (XS3)
In excess-3 representation, each decimal digit is the 4-bit binary equivalent with 3 (0011) added.
 
4221
For 4221 coded-decimals, each digit also is represented as a 4-bit group. Instead of each bit representing a successive power of two (8, 4, 2, and 1, from the left) as in binary, the bits represent 4, 2, 2, and 1.
DigitASCIIEBCDICXS34221
00011 00001111 000000110000
10011 00011111 000101000001
20011 00101111 001001010010
30011 00111111 001101100011
40011 01001111 010001111000
50011 01011111 010110000111
60011 0110 1111 011010011100
70011 01111111 011110101101
80011 10001111 100010111110
90011 10011111 100111001111


Other Resources


[  Index  |  Technical Notes  ]

DISCLAIMER

Page author: Dawn Rorvik (rorvikd@evergreen.edu)
Last modified: 05/20/2003