Binary Coded Decimal (BCD)

Binary Coded Decimal (BCD)

Binary Coded Decimal (BCD) is an alternative method for storing numbers in binary. In BCD denary numbers, that is regular base 10 decimal numbers, are stored one denary digit at a time where each digit is individually encoded in binary, usually as 4 bits.

For example the denary digit 1 is stored as 0001, 2 is stored as 0010, 3 is 0011 and so on up to to 9 1001.

Then each binary encoded denary digit is concatenated. For example the number 919 is stored as:

1001 0001 1001

BCD is sometimes used to store numbers with a decimal point as an alternative to binary floating point. To achieve this an imaginary decimal point is inserted at the required point in the number (see this article on fixed point for an explanation of how this works).

Floating point is subject to rounding errors in calculations and only stores approximate representations of denary numbers that can be slightly out in many instances. BCD stores the exact denary number and calculations result in a denary number with the rounding rules that we would normally expect in mathematics. This can have benefits in applications such as financial calculations where penny accuracy is important and rounding errors cannot be tolerated.

To see the problems with rounding errors for financial calculations, examine this Python program which uses binary floating point to total currency. It starts with a total of £0.00 and then adds £0.10 to the total ten times in a row. We would of course expect that 10x £0.10 = £1.00

total = 0
for i in range(10):
	total+=0.1
print(f{total}")

However we actually get:

£0.9999999999999999

This is caused by rounding errors because binary floating point is unable to represent many decimal numbers (including 0.1) precisely. We can however represent exactly 0.1 in BCD. We simply insert an imaginary decimal point between the BCD digits:

0000 . 0001

Another use for BCD is to simplify electronic circuits where numbers will appear on a denary display. For example digital clock circuitry may store numbers in BCD to avoid the circuits necessary for the conversion between the binary representation of numbers and the time in denary that the user of the clock actually wants to see.

There is a balance in efficiency as the circuits needed to do calculations in BCD can be more complicated than those needed to do calculations in binary, which is a disadvantage of BCD. Another problem with BCD is that it takes more memory to store each number than in standard binary, which can increase the costs of a system if it needs to store a lot of numbers. For example in 8-bit (unsigned) binary, numbers of up to 255 can be stored. However to store the number 255 in BCD requires 12 bits of memory:

255 in binary = 11111111 (8 bits)
255 in BCD = 001001010101 (12 bits)