Binary Codes
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
Decimal 4221 1's complement
0 0000 1111
1 0001 1110
2 0010 1101
3 0011 1100
4 1000 0111
5 0111 1000
6 1100 0011
7 1101 0010
8 1110 0001
9 1111 0000
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 Code
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 first 16 Gray coded numbers are indicated below.
Decimal Gray Code
0 0000
1 0001
2 0011
3 0010
4 0110
5 0111
6 0101
7 0100
8 1100
9 1101
10 1111
11 1110
12 1010
13 1011
14 1001
15 1000
To convert a Gray-coded number to binary then follow this
method
1. The binary number and the Gray-coded number will
have the same number of bits
2. The binary MSB (left-hand bit) and Gray code MSB
will always be the same
3. 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.
4. Continue in this manner right through to the end.
An example of converting a gray code number to its pure
binary equivalent is available in the Solved Problems
Gray coding is a non-BCD, non-weighted reflected binary
code.