Many specialized codes are used in digital systems. You have just learned about the BCD code; now let’s look at a few others. Some codes are strictly numeric, like BCD, and others are alphanumeric; that is, they are used to represent numbers, letters, symbols, and instructions. The codes introduced in this section are the Gray code.

The Gray Code

The Gray code is unweighted and is not an arithmetic code; that is, there are no specific weights assigned to the bit positions. The important feature of the Gray code is that it exhibits only a single bit change from one code word to the next in sequence. This property is important in many applications, such as shaft position encoders, where error susceptibility increases with the number of bit changes between adjacent numbers in a sequence. Table 2–6 is a listing of the 4-bit Gray code for decimal numbers 0 through 15. Binary numbers are shown in the table for reference. Like binary numbers, the Gray code can have any number of bits. Notice the single-bit change between successive Gray code words. For instance, in going from decimal 3 to decimal 4, the Gray code changes from 0010 to 0110, while the binary code changes from 0011 to 0100, a change of three bits. The only bit change in the Gray code is in the third bit from the right: the other bits remain the same.


Binary to Gray Code Conversion

Conversion between binary code and Gray code is sometimes useful. The following rules explain how to convert from a binary number to a Gray code word:
  1. The most significant bit (left-most) in the Gray code is the same as the corresponding MSB in the binary number.
  2. Going from left to right, add each adjacent pair of binary code bits to get the next Gray code bit. Discard carries.
For example, the conversion of the binary number 10110 to Gray code is as follows:

The Gray code is 11101.

Gray to Binary Code Conversion

To convert from Gray code to binary, use a similar method; however, there are some differences. The following rules apply:
  1. The most significant bit (left-most) in the binary code is the same as the corresponding bit in the Gray code.
  2. Add each binary code bit generated to the Gray code bit in the next adjacent position. Discard carries.
For example, the conversion of the Gray code word 11011 to binary is as follows: 

The binary number is 10010.

EXAMPLE 1:
(a) Convert the binary number 11000110 to Gray code.
(b) Convert the Gray code 10101111 to binary.
Solution:
(a) Binary to Gray code:


(b) Gray code to binary: