The usual way of expressing a decimal number in terms of a binary number is known as pure binary coding and is discussed in the Number Systems section. A number of other techniques can be used to represent a decimal number. These are summarised below.
8421 BCD Code
In the 8421 Binary Coded Decimal (BCD) representation each decimal digit is converted to its 4-bit pure binary equivalent.
For example: 57dec = 0101 0111bcd
Addition is analogous to decimal addition with normal binary addition taking place from right to left. For example,
|6||0110||BCD for 6||42||0100 0010||BCD for 42|
|+3||0011||BCD for 3||+27||0010 0111||BCD for 27|
|1001||BCD for 9||0110 1001||BCD for 69|
Where the result of any addition exceeds 9(1001) then six (0110) must be added to the sum to account for the six invalid BCD codes that are available with a 4-bit number. This is illustrated in the example below
|8||1001||BCD for 8|
|+7||0111||BCD for 7|
|1111||exceeds 9 (1001) so|
|0110||add six (0110)|
|0001 0101||BCD for 15|
Note that in the last example the 1 that carried forward from the first group of 4 bits has made a new 4-bit number and so represents the "1" in "15".
In the examples above the BCD numbers are split at every 4-bit boundary to make reading them easier. This is not necessary when writing a BCD number down.
This coding is an example of a binary coded (each decimal number maps to four bits) weighted (each bit represents a number, 1, 2, 4, etc.) code.
4221 BCD Code
The 4221 BCD code is another binary coded decimal code where each bit is weighted by 4, 2, 2 and 1 respectively. Unlike BCD coding there are no invalid representations. The decimal numbers 0 to 9 have the following 4221 equivalents
the 1's complement of a 4221 representation is important in decimal arithmetic. In forming the code remember the following rules
- Below decimal 5 use the right-most bit representing 2 first
- Above decimal 5 use the left-most bit representing 2 first
- Decimal 5 = 2+2+1 and not 4+1
Gray coding is an important code and is used for its speed, it is also relatively free from errors. In pure binary coding or 8421 BCD then counting from 7 (0111) to 8 (1000) requires 4 bits to be changed simultaneously. If this does not happen then various numbers could be momentarily generated during the transition so creating spurious numbers which could be read.
Gray coding avoids this since only one bit changes between subsequent numbers. To construct the code there are two simple rules. First start with all 0s and then proceed by changing the least significant bit (lsb) which will bring about a new state.
- The binary number and the Gray-coded number will have the same number of bits
- The binary MSB (left-hand bit) and Gray code MSB will always be the same
- To get the binary next-to-MSB (i.e. next digit to the right) add the binary MSB and the gray code next-to-MSB. Record the sum, ignoring any carry.
- Continue in this manner right through to the end.
Gray coding is a non-BCD, non-weighted reflected binary code.