In this article, I will write a little about the arithmetic of binary numbers in particular between two binary-coded decimals. I will include the necessary basics and an explanation of what BCD numbers are, I will also present the necessary notation of binary numbers, and binary-coded decimals.
Introduction
The usual operations on binary coded decimal in some way resemble the typical addition operations known from the binary, or even decimal system. An example of an addition operation in the standard binary system is as follows:
$ \begin{array}{r} 01\overset{1}01 \\ {} + 1001 \\ \hline 1110 \\ \end{array} $
Before we move on, it is worth recalling some knowledge related to binary numbers.
1's complement
1's complement can be mentally simplified to simply inverting the bits of a binary number. That is, changing bits 1 to 0 and 0 to 1
$ \begin{array}{lc} |A|_2 & = 1\ 1\ 0\ 1\ 0\ 1 \\ & \downarrow\\ |\overline{A}|_2 & = 0\ 0\ 1\ 0\ 1\ 0 \\ \end{array} $
If one wanted to be more precise 1's complement is obtained by subtracting each bit of the number from 1.
$ \newcommand\Tstrut{\Rule{0.0ex}{2.6ex}{0.0ex}} \begin{array}{rl} \begin{equation*} \begin{array}{r} 111111 \\ - 110101 \\ \hline\Tstrut = 001010 \\ \end{array} \end{equation*} & \begin{equation*} \begin{array}{ll} &\\ &\leftarrow{} |A|_2\\\Tstrut &\leftarrow{} |\overline{A}|_2 \end{array} \end{equation*} \end{array} $
2's complement
The 2's complement of a binary number is the addition of 1 to the least significant bit (bit the most to the right) of 1's complement.
$ \newcommand\Tstrut{\Rule{0ex}{2.6ex}{0ex}} \newcommand\carry{\overset{1}} \begin{array}{rl} \begin{array}{r} 011\carry01 \\ {} + 1 \\ \hline\Tstrut = 011 10 \\ \end{array} & \begin{array}{ll} &\Tstrut \leftarrow{} |\overline{A}|_{2}\\ &\\\Tstrut &\leftarrow{} |\overline{\overline{A}}|_{2} \end{array} \end{array} $
Sign Magnitude
By far the simplest form of representation of signed binary numbers is Sign Magnitude. The most significant bit determines the sign of the number.
$\underbrace{1\ }_\text{Sign bit}\underbrace{0\ 0\ 0\ 1\ 1\ 0}_\text{Magnitude}$
Number in decimal notation meaning -6
$\underbrace{0\ }_\text{Sign bit}\underbrace{0\ 0\ 0\ 1\ 1\ 0}_\text{Magnitude}$
Number in decimal notation meaning 6
Example numbers can therefore be broken down as follows:
$ \begin{array}{ rl } 0000 = +0 & 1000 = -0 \\ 0001 = +1 & 1001 = -1 \\ 0010 = +2 & 1010 = -2 \\ 0011 = +3 & 1011 = -3 \\ 0100 = +4 & 1100 = -4 \\ 0101 = +5 & 1101 = -5 \\ 0110 = +6 & 1110 = -6 \\ 0111 = +7 & 1111 = -7 \\ \end{array} $
Unfortunately, this notation suffers from double zero which causes problems when making comparisons. The following notations eliminate this problem.
Diminished Radix Complement
This notation depends on the number system. For the decimal system, it means 9's complement. It is taken from the base of the number system minus 1.
$ \begin{array}{lll} \text{For decimal, it is} & 10 - 1 = 9, & \text{so 9's complement.} \ \text{For binary it is} & 2 - 1 = 1, & \text{so 1's complement.} \ \end{array} $
For the decimal system, 9's complement is obtained by subtracting each character of a number from 9.
$ \newcommand\Tstrut{\Rule{0ex}{2.6ex}{0ex}} \newcommand\carry{\overset{1}} \begin{array}{rl} \begin{array}{r} 9999 \\ {} - 4537 \\ \hline\Tstrut = 5462 \\ \end{array} & \begin{array}{ll} &\\ &\leftarrow{} |A|_{10}\\\Tstrut &\leftarrow{} |\overline{A}|_{10} \end{array} \end{array} $
For a binary system, it is again nothing but inversion.
$ \newcommand\Tstrut{\Rule{0ex}{2.6ex}{0ex}} \newcommand\carry{\overset{1}} \begin{array}{rl} \begin{array}{r} 111111 \\ - 110101 \\ \hline\Tstrut = 001010 \\ \end{array} & \begin{array}{ll} &\\ &\leftarrow{} |A|_2\\\Tstrut &\leftarrow{} |\overline{A}|_2 \end{array} \end{array} $
In Diminished Radix Complement there is still the problem of positive and negative zeros.
Signed binary numbers
However, let us focus on the notation representing positive and negative values. Let's look at how the value of $6$ and $-6$ will be represented:
$\underbrace{1\ }_\text{Sign bit}\underbrace{1\ 1\ 1\ 0\ 0\ 1}_\text{Magnitude}$
Notation of $-6$ as Diminished Radix Complement
$\underbrace{0\ }_\text{Sign bit}\underbrace{0\ 0\ 0\ 1\ 1\ 0}_\text{Magnitude}$
Notation of $6$ as Diminished Radix Complement
In other words, positive values are written identically as Signed Magnitude, but negative values are represented as 1 in MSB, and the remaining number is 1's complement.
Radix Complement
For the decimal system, Radix Complement is 10's complement and for binary it is 2's complement. That is, they are equal to the base of the number system. Using prior knowledge, we can calculate Radix Complement for both decimal and binary systems.
Signed binary numbers
Let us again focus on the notation representing positive and negative values. Let's look at how the value of $6$ and $-6$ will be represented:
In other words, positive values are written identically as Signed Magnitude, and Diminished Radix Complement, but negative values are represented with 1 as most significant bit, and other bits are inverted, with extra 1 added to the least significant bit.
BCD Numbers
BCD is a system of writing numerals that assigns a tetrad to each digit of decimal number. Each digit is encoded separately. Each tetrad can store up to 16 values (0 to 15), but only 10 (0 to 9) are used.
- It can be easily used to drive displays of decimal numbers.
- It allows easy conversion to decimal.
- It allows easy multiplying by factors of 10.
- It is possible to store decimals for which the binary representation would have infinite expansion.
- They're more human-readable
But...
- Arithmetic operations are complicated.
- It is not true binary so it requires more space.
Weighted BCD codes
Weighted codes are those codes in which each bit depending on its position is assigned a specific weight. The sum of the weights for bits equal to 1 determines the output number. The simplest weighted is one in which each decimal digit is represented by its exact binary equivalent and it is called 8421, where 8421 is weight derived from the successive powers of two.
| Digit | 8 4 2 1 | 2 4 2 1 | 6 4 2 -3 |
|---|---|---|---|
| 0 | 0000 | 0000 | 0000 |
| 1 | 0001 | 0001 | 0101 |
| 2 | 0010 | 0010 | 0010 |
| 3 | 0011 | 0011 | 1001 |
| 4 | 0100 | 0100 | 0100 |
| 5 | 0101 | 1011 | 1011 |
| 6 | 0110 | 1100 | 0110 |
| 7 | 0111 | 1101 | 1101 |
| 8 | 1000 | 1110 | 1010 |
| 9 | 1001 | 1111 | 1111 |
Table: Various weight BCD codes
2421 code is otherwise known as Aiken's code. In addition, there are also such codes as 7421 and 5211.
Non-weighted BCD Codes
These are codes that cannot be determined by weights. They simply have to be memorized. Although some can be calculated in their own way. For example, Excess-3 is actually 8421, just with 3 added to each digit.
| Digit | Excess-3 | Watt's | Gray |
|---|---|---|---|
| 0 | 0011 | 0000 | 0000 |
| 1 | 0100 | 0001 | 0001 |
| 2 | 0101 | 0011 | 0011 |
| 3 | 0110 | 0010 | 0010 |
| 4 | 0111 | 0110 | 0110 |
| 5 | 1000 | 1110 | 0111 |
| 6 | 1001 | 1010 | 0101 |
| 7 | 1010 | 1011 | 0100 |
| 8 | 1011 | 1001 | 1100 |
| 9 | 1100 | 1000 | 1101 |
Table: Various non-weight BCD codes
Self-complementing
BCD may be self-complement which means that when the code representation of any decimal digit is given, the code for its 9's complement is found by changing all the 0's to 1's with no additional corrections. Examples are codes 2421 (Aiken), 642-3, and Excess-3. Codes such as 2421, Excess-3, 84-2-1 are self-complementing.
8421
When dealing with 8421 code we must be careful for the tetrad to not be bigger than 9. Otherwise, a special amendment of 0110 is needed. It is a multi-stage process as one amendment may lead to another amendment.
9's and 10's complement
Code 8421 is not a self-complementary code, so before converting to Diminished Radix Complement, a correction must be made. The correction is to add a binary equivalent of 6 to each group of bits representing individual digit.
Signed BCD
Here the difference relative to complements is small. 8421 also needs a correction here as well.
Addition and subtraction
DRC
- Perform typical binary addition, carriers move freely in tetrads and between tetrads. Sign bit carry if occurred is added to the LSB.
- Add amendments of binary equivalent of 6 (0110) to invalid tetrads and those where carry occured - only in first line
- Add amendments again to invalid tetrads as long as it is necessary. This may occur many times as it is multi-stage process and adding one amendment may cause need of adding another amendment as carriers freely move between tetrads.
If the result is negative:
- Add correction (0110) to each tetrad.
- 1's complement each bit (invert).
RC
- Perform typical binary addition, carriers move freely in tetrads and between tetrads. Sign bit carry if occurred is disregarded.
- Add amendments of binary equivalent of 6 (0110) to invalid tetrads and those where carry occurred (0110) - only first line
- Add amendment again to invalid tetrads as long as it is necessary. This may occur many times as it is multi-stage process and adding one amendment may cause need of adding another amendment as carriers freely move between tetrads.
If the result is negative:
- Add correction that is binary equivalent of 6 (0110) to each tetrad.
- 1's complement each bit (invert).
- Add +1.
Excess3
Excess-3 is carry-depended. Carry between tetrads resulting from adding amendments is always disregarded. But it is okay to have a carry in a typical arithmetic operation. Here this process in addition is one-step. This system is carry-dependent and we introduce amendments to all tetrads.
- Add 0011 to those tetrads where carry occured.
- Add 1101 to those tetrads where carry don't occured. It is also possible to subtract 0011 which in my opinio is worse.
9's and 10's complement
Code Excess3 is a self-complementary code, so there is no need for any correction before converting to Diminished Radix Complement.
Signed BCD
Here the difference relative to binary numbers is not much. Excess3 also do not need here any correction at all.
Addition and subtraction
DRC
- Perform typical binary addition, carry moves between tetrads and inside tetrads.
- If carry occurred add 0011, otherwise add 1101 and discard new carries
If the result is negative just 1's complement each bit (invert).
RC
- Perform typical binary addition, carry moves between tetrads and inside tetrads.
Sign bit carry if occurred is discarded. - If carry occurred add 0011, otherwise add 1101 and discard new carries
If the result is negative:
- 1's complement each bit (invert).
- Add +1.
Multiplication
By 10
Just shift to left all tetrads adding new zero-ed least significant tetrad
By 2
- Identify tetrads that are greater than 4.
- Shift one-bit left
- Add 0110 to the tetrad that before was greater than 4.
Or...
- Identify tetrads that are greater than 4.
- Add 0011 to the tetrad that is greater than 4.
- Shift one-bit left
Others
We may add/subtract multiple times values made by multiplying by 2 or 10.
Division
By 10
Just shift to the right all tetrads removing the least significant tetrad (or moving it past the decimal point).
By 2
- Shift one-bit right
- Identify to which tetrad there was a carry.
- Add 1101 (disregard carry) or subtract 0011 from that tetrad.