## Section4.6Other Codes.

¶Thus far in this chapter we have used the binary number system to represent numerical values. It is an efficient code in the sense that each of the \(2^{n}\) bit patterns represents a value. On the other hand, there are some limitations in the code. We will explore some other codes in this section.

### Subsection4.6.1BCD Code

¶One limitation of using the binary number system is that a decimal number must be converted to binary before storing or performing arithmetic operations on it. And binary numbers must be converted to decimal for most real-world display purposes.

The Binary Coded Decimal (BCD) code is a code for individual decimal digits. Since there are ten decimal digits, the code must use four bits for each digit. The BCD code is shown in Table 4.6.1.

Decimal Digit | BCD Code (four bits) |

\(0\) | \(\binary{0000}\) |

\(1\) | \(\binary{0001}\) |

\(2\) | \(\binary{0010}\) |

\(3\) | \(\binary{0011}\) |

\(4\) | \(\binary{0100}\) |

\(5\) | \(\binary{0101}\) |

\(6\) | \(\binary{0110}\) |

\(7\) | \(\binary{0111}\) |

\(8\) | \(\binary{1000}\) |

\(9\) | \(\binary{1001}\) |

For example, in a 16-bit storage location the decimal number 1234 would be stored in the BCD code as

and in binary as

From Table 4.6.1 we can see that six bit patterns are “wasted.” The effect of this inefficiency is that a 16-bit storage location has a range of \(0\) – \(9999\) if we use BCD, but the range is \(0\) – \(65535\) if we use binary.

BCD is important in specialized systems that deal primarily with numerical data. There are I/O devices that deal directly with numbers in BCD without converting to/from a character code {for example, ASCII). The COBOL programming language supports a packed BCD format where two digits (in BCD code) are stored in each 8-bit byte. The last (4-bit) digit is used to store the sign of the number as shown in Table 4.6.2. The specific codes used depend upon the particular implementation.

Sign | Sign Code (four bits) |

\(+\) | \(\binary{1010}\) |

\(-\) | \(\binary{1011}\) |

\(+\) | \(\binary{1100}\) |

\(-\) | \(\binary{1101}\) |

\(+\) | \(\binary{1110}\) |

unsigned | \(\binary{1111}\) |

For example, \(\binary{0001 \; 0010 \; 0011 \; 1010}\) would represent \(+123\text{,}\) \(\binary{0001 \; 0010 \; 0011 \; 1011}\) would represent \(-123\text{,}\) and \(\binary{0001 \; 0010 \; 0011 \; 1111}\) would represent \(123\text{.}\)

### Subsection4.6.2Gray Code

¶One of the problems with both the binary and BCD codes is that the difference between two adjacent values often requires that more than one bit be changed. For example, three bits must be changed when incrementing from \(3\) to \(4\) (\(\binary{0011}\) to \(\binary{0100}\)). If the value is read during the time when the bits are being switched there may be an error. This is more apt to occur if the bits are implemented with, say, mechanical switches instead of electronic.

The Gray code is one where there is only one bit that differs between any two adjacent values. As you will see in Section 5.5, this property also allows for a very useful visual tool for simplifying Boolean algebra expressions.

decimal | Gray code |

\(0\) | \(\binary{0}\) |

\(1\) | \(\binary{1}\) |

To add a bit, first duplicate the existing pattern, but *reflected*:

Gray code | |

\(\binary{0}\) | |

\(\binary{1}\) | |

\(\binary{1}\) | |

\(\binary{0}\) |

Then add a zero to the beginning of each of the original bit patterns and a one to the beginning of each of the reflected set:

decimal | Gray code |

0 | \(\binary{00}\) |

1 | \(\binary{01}\) |

2 | \(\binary{11}\) |

3 | \(\binary{10}\) |

The Gray code for four bits is shown in Table 4.6.3. Notice that the pattern of only changing one bit between adjacent values also holds when the bit pattern “wraps around.” That is, only one bit is changed when going from the highest value (\(15\) for four bits) to the lowest (\(0\)).

decimal | Gray code |

0 | \(\binary{0000}\) |

1 | \(\binary{0001}\) |

2 | \(\binary{0011}\) |

3 | \(\binary{0010}\) |

4 | \(\binary{0110}\) |

5 | \(\binary{0111}\) |

6 | \(\binary{0101}\) |

7 | \(\binary{0100}\) |

8 | \(\binary{1100}\) |

9 | \(\binary{1101}\) |

10 | \(\binary{1111}\) |

11 | \(\binary{1110}\) |

12 | \(\binary{1010}\) |

13 | \(\binary{1011}\) |

14 | \(\binary{1001}\) |

15 | \(\binary{1000}\) |