Binary Coded Decimal (BCD)

BCD takes advantage of the fact that any one decimal numeral can be represented by a four bit pattern:

Decimal
Digit / BCD
8 4 2 1
0 / 0 0 0 0
1 / 0 0 0 1
2 / 0 0 1 0

As most computers store data in 8-bitbytes, it is possible to use one of the following methods to encode a BCD number:

  • Uncompressed: each numeral is encoded into one byte, with four bits representing the numeral and the remaining bits having no significance.
  • Packed: two numerals are encoded into a single byte, with one numeral in the least significantnibble(bits 0-3) and the other numeral in the most significant nibble (bits 4-7).

As an example, encoding the decimal number91using uncompressed BCD results in the following binary pattern of two bytes:

Decimal: 9 1

Binary : 0000 1001 0000 0001

In packed BCD, the same number would fit into a single byte:

Decimal: 9 1

Binary : 1001 0001

Hence the numerical range for one uncompressed BCD byte is zero through nine inclusive, whereas the range for one packed BCD is zero through ninety-nine inclusive.

To represent numbers larger than the range of a single byte any number of contiguous bytes may be used. For example, to represent the decimal number12345in packed BCD, a program would encode as follows:

Decimal: 1 2 3 4 5

Binary : 0000 0001 0010 0011 0100 0101

Explain why Binary Coded Decimal (BCD) is sometimes referred to as the "8421" code. Why is this code used at all?

Convert the following decimal numbers into BCD form:

73910

2510

9224110

Convert the following BCD numbers into decimal form:

1000 1001

0100 0111 0110

0011 1000 0101 0001

BCD Addition

Just as in conventional decimal addition, BCD addition is performed one decimal digit at a time. The question is, what happens when the sum exceeds what can be represented in 4 bits? Stated differently, what are the conditions under which a carry is generated to the next highest-order BCD digit?

For example, let's consider the addition of the two BCD digits 5 and 3:

Now consider the sum of 5 and 8:

The sum is 11012 = 13, but this result should be correctly represented as 0001 0011 in BCD notation. Fortunately, there is a simple way to find the correct result. We add 6(01102)to the digit sum if it exceeds 9. Let's examine the following cases:

In both cases, by adding six we obtain the correct answer in BCD.

The reason for adding 6 is that there are 16 possible 4-bit BCD values (since 24= 16), but only 10 values are valid (0000 through 1001). So adding 6 to the invalid entries results in the following:

Calculate the sums of the following numbers.

First convert them in to BCD, then add them together. Show all your working.

123 + 454 + 32165 + 234