- Digital Circuits Tutorial
- Home
- Number Systems
- Base Conversions
- Binary Numbers Representation
- Signed Binary Arithmetic
- Codes
- Error Detection & Correction Codes
- Boolean Algebra
- Canonical and Standard Forms
- K-Map Method
- Quine-McCluskey Tabular Method
- Logic Gates
- Two-Level Logic Realization
- Combinational Circuits
- Arithmetic Circuits
- Decoders
- Encoders
- Multiplexers
- De-Multiplexers
- Programmable Logic Devices
- Threshold Logic
- Sequential Circuits
- Latches
- Flip-Flops
- Conversion of Flip-Flops
- Shift Registers
- Application of Shift Registers
- Counters
- Finite State Machines
- Algorithmic State Machine Charts

- Digital Circuits Resources
- Digital Circuits - Quick Guide
- Digital Circuits - Useful Resources
- Digital Circuits - Discussion

# Digital Circuits - Codes

In the coding, when numbers or letters are represented by a specific group of symbols, it is said to be that number or letter is being encoded. The group of symbols is called as **code**. The digital data is represented, stored and transmitted as group of bits. This group of bits is also called as **binary code**.

Binary codes can be classified into two types.

- Weighted codes
- Unweighted codes

If the code has positional weights, then it is said to be **weighted code**. Otherwise, it is an unweighted code. Weighted codes can be further classified as positively weighted codes and negatively weighted codes.

## Binary Codes for Decimal digits

The following table shows the various binary codes for decimal digits 0 to 9.

Decimal Digit | 8421 Code | 2421 Code | 84-2-1 Code | Excess 3 Code |
---|---|---|---|---|

0 | 0000 | 0000 | 0000 | 0011 |

1 | 0001 | 0001 | 0111 | 0100 |

2 | 0010 | 0010 | 0110 | 0101 |

3 | 0011 | 0011 | 0101 | 0110 |

4 | 0100 | 0100 | 0100 | 0111 |

5 | 0101 | 1011 | 1011 | 1000 |

6 | 0110 | 1100 | 1010 | 1001 |

7 | 0111 | 1101 | 1001 | 1010 |

8 | 1000 | 1110 | 1000 | 1011 |

9 | 1001 | 1111 | 1111 | 1100 |

We have 10 digits in decimal number system. To represent these 10 digits in binary, we require minimum of 4 bits. But, with 4 bits there will be 16 unique combinations of zeros and ones. Since, we have only 10 decimal digits, the other 6 combinations of zeros and ones are not required.

### 8 4 2 1 code

The weights of this code are 8, 4, 2 and 1.

This code has all positive weights. So, it is a

**positively weighted code**.This code is also called as

**natural BCD**(Binary Coded Decimal)**code**.

**Example**

Let us find the BCD equivalent of the decimal number 786. This number has 3 decimal digits 7, 8 and 6. From the table, we can write the BCD (8421) codes of 7, 8 and 6 are 0111, 1000 and 0110 respectively.

**∴ (786) _{10} = (011110000110)_{BCD}**

There are 12 bits in BCD representation, since each BCD code of decimal digit has 4 bits.

### 2 4 2 1 code

The weights of this code are 2, 4, 2 and 1.

This code has all positive weights. So, it is a

**positively weighted code**.It is an

**unnatural BCD**code. Sum of weights of unnatural BCD codes is equal to 9.It is a

**self-complementing**code. Self-complementing codes provide the 9’s complement of a decimal number, just by interchanging 1’s and 0’s in its equivalent 2421 representation.

**Example**

Let us find the 2421 equivalent of the decimal number 786. This number has 3 decimal digits 7, 8 and 6. From the table, we can write the 2421 codes of 7, 8 and 6 are 1101, 1110 and 1100 respectively.

Therefore, the 2421 equivalent of the decimal number 786 is **110111101100**.

### 8 4 -2 -1 code

The weights of this code are 8, 4, -2 and -1.

This code has negative weights along with positive weights. So, it is a

**negatively weighted code**.It is an

**unnatural BCD**code.It is a

**self-complementing**code.

**Example**

Let us find the 8 4-2-1 equivalent of the decimal number 786. This number has 3 decimal digits 7, 8 and 6. From the table, we can write the 8 4 -2 -1 codes of 7, 8 and 6 are 1001, 1000 and 1010 respectively.

Therefore, the 8 4 -2 -1 equivalent of the decimal number 786 is **100110001010**.

### Excess 3 code

This code doesn’t have any weights. So, it is an

**un-weighted code**.We will get the Excess 3 code of a decimal number by adding three (0011) to the binary equivalent of that decimal number. Hence, it is called as Excess 3 code.

It is a

**self-complementing**code.

**Example**

Let us find the Excess 3 equivalent of the decimal number 786. This number has 3 decimal digits 7, 8 and 6. From the table, we can write the Excess 3 codes of 7, 8 and 6 are 1010, 1011 and 1001 respectively.

Therefore, the Excess 3 equivalent of the decimal number 786 is **101010111001**

## Gray Code

The following table shows the 4-bit Gray codes corresponding to each 4-bit binary code.

Decimal Number | Binary Code | 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 |

This code doesn’t have any weights. So, it is an

**un-weighted code**.In the above table, the successive Gray codes are differed in one bit position only. Hence, this code is called as

**unit distance**code.

### Binary code to Gray Code Conversion

Follow these steps for converting a binary code into its equivalent Gray code.

Consider the given binary code and place a zero to the left of MSB.

Compare the successive two bits starting from zero. If the 2 bits are same, then the output is zero. Otherwise, output is one.

Repeat the above step till the LSB of Gray code is obtained.

**Example**

From the table, we know that the Gray code corresponding to binary code 1000 is 1100. Now, let us verify it by using the above procedure.

Given, binary code is 1000.

**Step 1** − By placing zero to the left of MSB, the binary code will be 01000.

**Step 2** − By comparing successive two bits of new binary code, we will get the gray code as **1100**.