We all have heard about the binary numbers and binary coded decimals and we also know that these are used inside the computers for the computation and processing of our commands. In computer & electronics Binary Coded Decimal or BCD is a class of binary encodings of decimal numbers where each decimal digit is represented by a fixed number of bits which are usually four digits or eight digits. BCD was used in many early decimal computers, and is implemented in the instruction set of machines such as the IBM System/360 series and its descendants and Digital’s VAX.
Although Binary Coded Decimal is not as widely used as in the past and is no longer implemented in computers’ instruction sets, decimal fixed-point and floating-point formats are still important and continue to be used in financial, commercial, and industrial computing, where subtle conversion and fractional rounding errors that are inherent in floating point binary representations cannot be tolerated.
Normally BCD are represented using the four bits or digits starting from 0000 to 1001 which are equivalent to decimal numbers 0-9 respectively, this is also called 8421 encoding of binary coded decimals. This has been explained in below table.
How to Encode Binary Coded Decimal BCD Number
As most computers deal with data in 8-bit bytes, it is possible to use one of the following methods to encode a BCD number:
- Unpacked: 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 significant nibble (bits 0 through 3) and the other numeral in the most significant nibble (bits 4 through 7).
BCD Examples Using Packed & Unpacked Encoding
As an example, encoding the decimal number 91 using unpacked Binary Coded Decimal 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 unpacked Binary Coded Decimal byte is zero through nine inclusive, whereas the range for one packed BCD is zero through ninety-nine inclusive.
BCD Examples for Larger Numbers
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 number 12345 in packed Binary Coded Decimal, using big-endian format, a program would encode as follows:
Decimal: 1 2 3 4 5 Binary : 0000 0001 0010 0011 0100 0101
Note that the most significant nibble of the most significant byte is zero, implying that the number is in actuality 012345. Also note how packed Binary Coded Decimal is more efficient in storage usage as compared to unpacked BCD; encoding the same number (with the leading zero) in unpacked format would consume twice the storage.
Shifting and Masking of Packed BCD Digits
Shifting and masking operations are used to pack or unpack a packed Binary Coded Decimal digit. Other logical operations are used to convert a numeral to its equivalent bit pattern or reverse the process.
Use of Binary Coded Decimal BCD in Electronics
BCD is very common in electronic systems where a numeric value is to be displayed, especially in systems consisting solely of digital logic, and not containing a microprocessor. By employing Binary Coded Decimal BCD, the manipulation of numerical data for display can be greatly simplified by treating each digit as a separate single sub-circuit. This matches much more closely the physical reality of display hardware—a designer might choose to use a series of separate identical seven-segment displays to build a metering circuit, for example. If the numeric quantity were stored and manipulated as pure binary, interfacing to such a display would require complex circuitry. Therefore, in cases where the calculations are relatively simple, working throughout with Binary Coded Decimal can lead to a simpler overall system than converting to and from binary. Most pocket calculators do all their calculations in BCD.
The same argument applies when hardware of this type uses an embedded microcontroller or other small processor. Often, smaller code results when representing numbers internally in BCD format, since a conversion from or to binary representation can be expensive on such limited processors. For these applications, some small processors feature BCD arithmetic modes, which assist when writing routines that manipulate BCD quantities.
Addition of Binary Coded Decimals BCD’s
It is possible to perform addition in BCD by first adding in binary, and then converting to BCD afterwards. Conversion of the simple sum of two digits can be done by adding 6 (that is, 16 – 10) when the five-bit result of adding a pair of digits has a value greater than 9. For example:
1001 + 1000 = 10001 9 + 8 = 17
Note that 10001 is the binary, not decimal, representation of the desired result. Also note that it cannot fit in a 4-bit number. In BCD as in decimal, there cannot exist a value greater than 9 (1001) per digit. To correct this, 6 (0110) is added to that sum and then the result is treated as two nibbles:
10001 + 0110 = 00010111 => 0001 0111 17 + 6 = 23 1 7
The two nibbles of the result, 0001 and 0111, correspond to the digits “1” and “7”. This yields “17” in BCD, which is the correct result.
This technique can be extended to adding multiple digits by adding in groups from right to left, propagating the second digit as a carry, always comparing the 5-bit result of each digit-pair sum to 9. Some CPUs provide a half-carry flag to facilitate BCD arithmetic adjustments following binary addition and subtraction operations.
Subtraction of Binary Coded Decimals BCD’s
Subtraction is done by adding the ten’s complement of the subtrahend. To represent the sign of a number in BCD, the number 0000 is used to represent a positive number, and 1001 is used to represent a negative number. The remaining 14 combinations are invalid signs. To illustrate signed BCD subtraction, consider the following problem: 357 − 432.
In signed Binary Coded Decimal, 357 is 0000 0011 0101 0111. The ten’s complement of 432 can be obtained by taking the nine’s complement of 432, and then adding one. So, 999 − 432 = 567, and 567 + 1 = 568. By preceding 568 in BCD by the negative sign code, the number −432 can be represented. So, −432 in signed BCD is 1001 0101 0110 1000.
Now that both numbers are represented in signed Binary Coded Decimal, they can be added together:
0000 0011 0101 0111 0 3 5 7 + 1001 0101 0110 1000 9 5 6 8 = 1001 1000 1011 1111 9 8 11 15
Since BCD is a form of decimal representation, several of the digit sums above are invalid. In the event that an invalid entry (any Binary Coded Decimal digit greater than 1001) exists, add 6 to generate a carry bit and cause the sum to become a valid entry.
The reason for adding 6 is that there are 16 possible 4-bit Binary Coded Decimal values (since 24 = 16), but only 10 values are valid (0000 through 1001). So adding 6 to the invalid entries results in the following:
1001 1000 1011 1111 9 8 11 15 + 0000 0000 0110 0110 0 0 6 6 = 1001 1001 0010 0101 9 9 2 5
Thus the result of the subtraction is 1001 1001 0010 0101 (-925). To check the answer, note that the first digit is 9, which means negative. This seems to be correct, since 357 − 432 should result in a negative number. To check the rest of the digits, represent them in decimal. 1001 0010 0101 is 925. The ten’s complement of 925 is 1000 − 925 = 999 − 925 + 1 = 074 + 1 = 75, so the calculated answer is −75. To check, perform standard subtraction to verify that 357 − 432 is −75.
Note that in the event that there are a different number of nibbles being added together like 1053 − 122. The number with the fewest number of digits must first be padded with zeros before taking the ten’s complement or subtracting. Therefore with 1053 − 122, we shall represent 122 as 0122, and the calculate ten’s complement of 0122.