brock

Saturday, July 7, 2012

BCD Conversion




Programming
Binary Coded Decimals (BCD or "8421" BCD)
Encoding
Packing a Two-Byte BCD
Converting between Decimal and BCD
Converting between Binary and BCD
Addition
Subtraction
Unpacked Multiplication
Unpacked Division
Other Decimal Codes
Other Resources
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.
Decimal
Binary
BCD
Unpacked
Packed
0
0000 0000
0000 0000
0000 0000
1
0000 0001
0000 0001
0000 0001
2
0000 0010
0000 0010
0000 0010
3
0000 0011
0000 0011
0000 0011
4
0000 0100
0000 0100
0000 0100
5
0000 0101
0000 0101
0000 0101
6
0000 0110
0000 0110
0000 0110
7
0000 0111
0000 0111
0000 0111
8
0000 1000
0000 1000
0000 1000
9
0000 1001
0000 1001
0000 1001
10
0000 1010
0000 0001 0000 0000
0001 0000
11
0000 1011
0000 0001 0000 0001
0001 0001
12
0000 1100
0000 0001 0000 0010
0001 0010
13
0000 1101
0000 0001 0000 0011
0001 0011
14
0000 1110
0000 0001 0000 0100
0001 0100
15
0000 1111
0000 0001 0000 0101
0001 0101
16
0001 0000
0000 0001 0000 0110
0001 0110
17
0001 0001
0000 0001 0000 0111
0001 0111
18
0001 0010
0000 0001 0000 1000
0001 1000
19
0001 0011
0000 0001 0000 1001
0001 1001
20
0001 0100
0000 0010 0000 0000
0010 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.
Digit
ASCII
EBCDIC
XS3
4221
0
0011 0000
1111 0000
0011
0000
1
0011 0001
1111 0001
0100
0001
2
0011 0010
1111 0010
0101
0010
3
0011 0011
1111 0011
0110
0011
4
0011 0100
1111 0100
0111
1000
5
0011 0101
1111 0101
1000
0111
6
0011 0110
1111 0110
1001
1100
7
0011 0111
1111 0111
1010
1101
8
0011 1000
1111 1000
1011
1110
9
0011 1001
1111 1001
1100
1111