Binary-coded decimal: Difference between revisions

Content deleted Content added
Operations with BCD: Rearranged subject order to follow a similar path of complexity from systems that utilize BCD and so onward with computers and other systems
Line 658:
1 2 7 9. 5 +0
 
== Operations with BCD ==
=={{anchor|IBMBCD}}BCD in computers==
 
=== Addition ===
It is possible to perform [[addition]] 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. The reason for adding 6 is that there are 16 possible 4-bit BCD values (since 2<sup>4</sup> = 16), but only 10 values are valid (0000 through 1001). For example:
1001 + 1000 = 10001
9 + 8 = 17
10001 is the binary, not decimal, representation of the desired result, but the most significant 1 (the "carry") cannot fit in a 4-bit binary 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 the total, 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. The [[Intel 8080]], the [[Zilog Z80]]<ref name="Zaks_1982"/> and the CPUs of the x86 family<ref name="Cloutier_2019"/> provide the opcode DAA (Decimal Adjust Accumulator).
 
=== Subtraction ===
Subtraction is done by adding the ten's complement of the [[subtrahend]] to the [[minuend]]. 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 BCD, 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 BCD, they can be added together:
0000 0011 0101 0111
0 3 5 7
+ 1001 0101 0110 1000
9 95 26 58
= 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 BCD digit greater than 1001) exists, 6 is added to generate a carry bit and cause the sum to become a valid entry. So, adding 6 to the invalid entries results in the following:
1001 1000 1011 1111
9 8 11 15
+ 0000 0000 0110 0110
<u> 0 0 6 6</u>
= 1001 1001 0010 0101
<u> 9 59 62 8</u>5
Thus the result of the subtraction is 1001 1001 0010 0101 (−925). To confirm the result, note that the first digit is 9, which means negative. This seems to be correct since 357 − 432 should result in a negative number. The remaining nibbles are BCD, so 1001 0010 0101 is 925. The ten's complement of 925 is 1000 − 925 = 75, so the calculated answer is −75.
 
If there are a different number of nibbles being added together (such as 1053 − 2), the number with the fewer digits must first be prefixed with zeros before taking the ten's complement or subtracting. So, with 1053 − 2, 2 would have to first be represented as 0002 in BCD, and the ten's complement of 0002 would have to be calculated.
 
=={{anchor|IBMBCD}}BCD in computers==
===IBM===
{{Main|BCDIC}}
Line 708 ⟶ 744:
 
The same argument applies when hardware of this type uses an embedded microcontroller or other small processor. Often, representing numbers internally in BCD format results in smaller code, since a conversion from or to binary representation can be expensive on such limited processors. For these applications, some small processors feature dedicated arithmetic modes, which assist when writing routines that manipulate BCD quantities.<ref name="IEEE_CORDIC"/><ref name="BCS_CORDIC"/>
 
== Operations with BCD ==
=== Addition ===
It is possible to perform [[addition]] 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. The reason for adding 6 is that there are 16 possible 4-bit BCD values (since 2<sup>4</sup> = 16), but only 10 values are valid (0000 through 1001). For example:
1001 + 1000 = 10001
9 + 8 = 17
 
10001 is the binary, not decimal, representation of the desired result, but the most significant 1 (the "carry") cannot fit in a 4-bit binary 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 the total, 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. The [[Intel 8080]], the [[Zilog Z80]]<ref name="Zaks_1982"/> and the CPUs of the x86 family<ref name="Cloutier_2019"/> provide the opcode DAA (Decimal Adjust Accumulator).
 
=== Subtraction ===
Subtraction is done by adding the ten's complement of the [[subtrahend]] to the [[minuend]]. 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 BCD, 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 BCD, they can be added together:
0000 0011 0101 0111
0 3 5 7
+ 1001 0101 0110 1000
<u> 9 5 6 8</u>
= 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 BCD digit greater than 1001) exists, 6 is added to generate a carry bit and cause the sum to become a valid entry. So, adding 6 to the invalid entries results in the following:
1001 1000 1011 1111
9 8 11 15
+ 0000 0000 0110 0110
<u> 0 0 6 6</u>
= 1001 1001 0010 0101
9 9 2 5
 
Thus the result of the subtraction is 1001 1001 0010 0101 (−925). To confirm the result, note that the first digit is 9, which means negative. This seems to be correct since 357 − 432 should result in a negative number. The remaining nibbles are BCD, so 1001 0010 0101 is 925. The ten's complement of 925 is 1000 − 925 = 75, so the calculated answer is −75.
 
If there are a different number of nibbles being added together (such as 1053 − 2), the number with the fewer digits must first be prefixed with zeros before taking the ten's complement or subtracting. So, with 1053 − 2, 2 would have to first be represented as 0002 in BCD, and the ten's complement of 0002 would have to be calculated.
 
==Comparison with Pure Binary==