Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Binary-coded decimal
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== 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]] and the CPUs of the x86 family 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 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 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 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 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.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)