In this tutorial, you will learn about the unweighted codes like excess-3 and gray codes. You will also learn how to interconvert binary and gray code, excess-3 and decimal as well as see the advantages of using excess-3 and gray code.

**Contents:**

- What is Excess-3 Code?
- Decimal to Excess-3 Conversion
- Excess-3 to Decimal Conversion
- Advantages of Excess-3 Code
- What is Gray Code?
- Binary to Gray Code Conversion
- Gray Code to Binary Conversion
- Advantages of Gray Code

## What is Excess-3 Code?

Excess-3 code is an unweighted, self-complementary code that is used to overcome the shortcomings found in BCD codes. Excess-3 like BCD is also used to represent decimal numbers and is a 4-bit encoded form of it in terms of 0s and 1s.

For each decimal digit, the excess-3 code is obtained by adding three (0 0 1 1) to its corresponding BCD code or it can also be obtained by adding 3 to its decimal digit and then writing the equivalent binary number for it.

The following table shows the excess-3 code for decimal digits.

Decimal Numbers | BCD | Excess-3 code |
---|---|---|

0 | 0000 | 0011 |

1 | 0001 | 0100 |

2 | 0010 | 0101 |

3 | 0011 | 0110 |

4 | 0100 | 0111 |

5 | 0101 | 1000 |

6 | 0110 | 1001 |

7 | 0111 | 1010 |

8 | 1000 | 1011 |

9 | 1001 | 1100 |

As shown in the table, each excess-3 code is obtained by adding 3 to its BCD code.

## Decimal to Excess-3 Conversion

To convert a decimal number into excess-3, we write down the equivalent excess-3 codes for each decimal digit separately. To get the excess-3 codes for each digit,

- We add 3 to each decimal digit individually, the maximum sum that can occur this way is 9 +3 = 12.
- We then write down the binary values in 4-bits for each obtained digit.
- Alternatively, we can add 0011 to the BCD codes for each decimal digit directly.

For example,

- (67.43)
_{10}in excess-3 is given as: –

67.43 + 33.33 = (9) (10) . (7) (3) (Add 3 individually to each digit)

= (1001 1010. 0111 0011) (Each digit’s corresponding binary value is written) - (1542) in excess-3 is given as: –

1542 + 3333 = (4) (8) (7) (5) (Add 3 individually to each digit)

= (0100 1000 0111 0101) (Each digit’s corresponding binary value is written)

## Excess-3 to Decimal Conversion

To convert a decimal number into excess-3, we will follow reverse steps of what we did previously. The steps are:

- Make groups of 4-bits beginning from the LSB.
- Subtract 0011 from each group of 4-bits, and write down its equivalent decimal value.
- For conversion into decimal, the groups must lie between (0011-1100) as only these are valid Excess-3 groups that have decimal single-digit representations.
- For example, if we had 1111 as the group, subtracting 0011 from it, it becomes 1100 which is 12 in decimal and is not a single digit.

Examples of Excess-3 to decimal conversion are,

- (1011 1000) in decimal is given as: –

(1011 – 0011) (1000 – 0011) = (1000) (0101) (Subtracting 0011 from each group)

= (85)_{10} - (1010 1100) in decimal is given as: –

(1010 – 0011) (1100 – 0011) = (0111) (1001) (Subtracting 0011 from each group)

= (79)_{10}

## Advantages of Excess-3 Code

The benefit of using excess-3 code is that

- It becomes self-complementary, that is 9’s complement of the decimal digits have their respective excess-3 codes as 1’s complement, for example, 0 has its 9’s complement as 9 and its excess-3 code 0011 on inverting all digits, become 1100 which is the excess-3 code for 9.
- It also makes it easier to detect overflow which isn’t possible with BCD Codes.
- It remains sequential like BCD.

## What is Gray Code?

Gray Code is also an unweighted code. Gray Code is obtained from the binary form of decimal digits. It is known as a unit distance code as between the gray codes of any two successive binary numbers only one-bit changes. Here is a table that shows the gray codes for digits from 0-15.

Binary’s decimal value | Binary Number | Gray Code |
---|---|---|

0 | 0000 | 0000 |

1 | 0001 | 0001 |

2 | 0010 | 0011 |

3 | 0011 | 0010 |

4 | 0100 | 0110 |

5 | 0101 | 0111 |

6 | 0110 | 0101 |

7 | 0111 | 0100 |

8 | 1000 | 1100 |

9 | 1001 | 1101 |

10 | 1010 | 1111 |

11 | 1011 | 1110 |

12 | 1100 | 1010 |

13 | 1101 | 1011 |

14 | 1110 | 1001 |

15 | 1111 | 1000 |

As shown in the table,

- Between any two successive gray codes, only one-bit changes. It also can be noted that gray code is non-sequential, as the next decimal digit’s gray code cannot be obtained by adding 1 to its previous gray code.

## Binary to Gray Code Conversion

The gray code for a number can be obtained from its binary number using the following steps: –

- Write down the binary form of the number.
- The first digit (MSB) of binary is copied as the first digit of gray code.
- The next bits can be obtained by performing XOR operations between the neighbouring binary digits going from MSB to LSB.
- This is done till we perform XOR operation between the two last binary digits.

Here are some examples shown in the figure.

As shown in the figure,

- (1011) in gray code is written as: –
- The first digit 1 is copied as it is.
- The next gray digit is (1 XOR 0) as 1 is the MSB of binary and 0 is its right neighbouring bit. Thus,

1 XOR 0 = 1 - The next digit is (0 XOR 1) = 1
- The next is, 1 XOR 1 = 0
- Thus, the obtained gray code is 1110.
- (1100) in gray code is written as: –
- The first digit 1 is copied as it is.
- The next gray digit is (1 XOR 1) as 1 is the MSB of binary and 1 is its right neighbouring bit. Thus,

1 XOR 1 = 0 - The next digit is (1 XOR 0) = 1
- The next is, 0 XOR 0 = 0
- Thus, the obtained gray code is 1010
- All the arrows shown in red denote XOR operation in the right direction between binary digits.

## Gray Code to Binary Conversion

The binary code can be obtained from gray code by using XOR operations in the reverse direction. The steps to follow for converting a gray number into binary are: –

- Write the MSB of gray code as the MSB of the binary number directly.
- The next digits of the binary number are obtained by performing XOR operations with the obtained binary digit and the next gray digit right to the MSB.
- The process is repeated to its right till the last gray digit is XORed with.

The following figure shows the examples of gray to binary conversion.

As shown in the figure,

- Gray code of (1110) in binary is given as: –
- The MSB is copied as it is. MSB = 1
- Then we XOR this obtained binary digit with the next gray digit.

(1 XOR1) = 0 - The next digit is, (0 XOR 1) =1
- And the LSB is (1 XOR 0) =1
- Thus, obtained binary number is 1011
- Gray code of (1100) in binary is given as: –
- The MSB is copied as it is. MSB = 1
- Then we XOR this obtained binary digit with the next gray digit.

(1 XOR1) = 0 - The next digit is, (0 XOR 0) =0
- And the LSB is (0 XOR 0) =0
- Thus, obtained binary number is 1000
- The red arrow shows the direction of the XOR operation performed.

## Advantages of Gray Code

Gray code has its advantages which are listed as follows: –

- It is a unit distance code due to which it is mostly used to represent switching logic.
- In binary, all the bits change if we want to change 3 (011) into 4 (100) which means there are many transient switching states in between [011 –> 010 –> 000 –> 100]. So, Gray code removes this ambiguity as between any two of its successive codes, only 1-bit changes.

## Key Points to Remember

Here are the key points to remember in “Excess-3 and Gray Code”.

- Excess-3 and gray codes are unweighted codes as they do not depend on positional values.
- Excess-3 codes for decimal number is obtained by adding 0011 to its BCD codes or by adding 3 to its decimal value and then writing its binary form.
- Excess-3 code is more used than BCD as they become self-complementary but remain sequential like BCD codes.
- Gray code is a unit distance code because between any of its two successive codes, only one-digit changes.
- Gray codes are obtained from binary numbers by performing XOR operation between two neighbouring binary digits starting from MSB to LSB.
- Gray codes are advantageous when used in switching circuits as switching in binary numbers produces ambiguous states in between.

**If you find any mistake above, kindly email to [email protected]**