# Binary Codes or Digital Codes with Examples

## Conversion of BCD, Gray code, Excess -3 Code and ASCII Code

Contents

**Binary Codes or Digital Codes**

**Binary codes or digital codes- **In the digital logic circuits, each decimal or piece of information is represented through an equivalent combination of binary digits. A complete group of such combinations, which represents numbers, layers, or symbols, is known as a digital or binary code. Further, the method of converting a decimal number to a binary number is called coding. These are codes, which apparently look like to be a combination of binary numbers, however, they are not featured according to the ideology of binary numbers. These codes are normally used in order to switch binary numbers from one system to the other.

Codes are used for security purposes so that in case the message has been detected, it may not be deciphered or decoded. In modern digital instruments, codes are used in order to describe, represent and process arithmetic information (i.e. addition, subtraction, multiplication, and division). The selection of a code depends on the nature or purpose of the work going to be availed through it. Some codes are suitable for conducting arithmetic processes while others are famous for their high accuracy and efficiency (i.e. such codes deliver significant information through the application of a few bits)

Under specific applications, the circuit required for processing of information becomes simple and concise as a result of the application of one code or the other. Further, the reliability of a digital system enhances as a result of a finite switching circuit.

**Types of Binary Codes or Digital Codes**

Further codes being used in the digital systems are of the following two types

- Numeric weighted code
- Special binary codes or non-weighted code

**Numeric or Weighted Codes**

These are the codes, wherein every digit has got some particular weight. For example, BCD (binary coded decimal) is a weighted code. The explanation of BCD is as follows:

**BCD or 842 Code **

These are the codes wherein every decimal digit is denoted by a group of four bits. As the weight of a 4-bit position from right to left is 8-4-2-1, that’s why it is also known as an 8421 code. As every digit present in it has a specific weight, that’s the reason it is generally considered as a weighted or numerical code.

Binary Code Decimal (BCD) is a method for the description of every decimal digit with respect to its binary code. As there are just 10 code groups (0-9) in the BCD system, therefore it is quite easy to convert decimal to BCD or BCD to decimal. As a result of being familiar with the decimal number system, we prefer to read or write in decimals. Whereas, looking at any binary number, it seems a bit difficult to calculate an equivalent decimal number forthwith. For example, binary 00110101 denotes 35 in decimal, nonetheless, it is obviously difficult to tell its decimal number immediately after looking at this binary. However, its equivalent decimal number may be calculated from this binary after a few minutes. Thus, this process entails a substantial waste of time, which is a significant drawback of the binary to decimal conversion method (which has been discussed in the previous pages). In order get rid of the specific flaw, engineers have designed a special code which keenly resembles a decimal number system. This specific code is called BCD. As decimal numbers are used on input and output of most of the digital components and circuits, therefore this particular code is fairly popular owing to having a characteristic combination of both binary as well as digital systems.

In other words, binary coded decimal (or the decimal coded into binary) means expression of every decimal digit (0-9) by means of a 4 – bits binary code, while code 8421 represents the binary weights of these four bits (i.e. 2_{3}, 2_{2}, 2_{1}, 2_{0}). The greatest advantage of this code is that it is pretty easy to interconvert 8421 code numbers with ordinary decimal numbers. Therefore, whenever we talk about BCD, it always means 8421 code. In table 1.5, the conversion or coding of 10 decimal digits into BCD has been shown. Remember that a total of 16 binary numbers (from 0000 to 1111) can be represented with 4 – bits. However, only first ten numbers out of these are used in 8421 code, whereas the remaining six (i.e. 1010, 1011, 1100, 1101, 1110 and 1111) have been invalidated in the BCD code 8421. This has been illustrated with the help of following table:

Table 1.5 Decimal to BCD Conversion

Decimal | BCD |

0 | 0000 |

1 | 0001 |

2 | 0010 |

3 | 0011 |

4 | 0100 |

5 | 0101 |

6 | 0110 |

7 | 0111 |

8 | 1000 |

9 | 1001 |

You might well imagine that BCD codes are simply analogous to the binary codes or Digital Codes, however practically it is not so. For example, 10 is represented as 1010 in the binary system, however it is denoted 0000 0001 in BCD. Similarly, 17 is shown as 10001 in the binary system, whereas in BCD code, it is denoted as 0001 0111). Actually, confusion occurs when we see that the first 9 numbers both in BCD and binary are exactly similar (as can be seen through the table) however, they become completely different later on.

Table 1.6

Decimal | BCD |

26 | 0010 0110 |

59 | 0101 1001 |

673 | 0110 0111 0011 |

2498 | 0010 0100 1001 1000 |

For the purpose of converting any decimal number to BCD, a 4-digit binary code is mentioned with its corresponding decimal digit. An appropriate space is also kept within every 4 – bits group, so that a distinction could be maintained between an ordinary binary code and a BCD code.

As can be seen from the table 1.6, the greatest advantage of a BCD code is that it is fairly easy to read, write, identify and even to memorize the arrangement of first 10 numbers. As BCD numbers look like decimal numbers, therefore when we have to work continuously with BCD numbers, we can quite easily convert a BCD number to an equivalent decimal number and a decimal number into BCD instantly after having a look at these. However, in spite of its merits, BCD code has certain deficiencies or drawbacks as well. For example, performance of a BCD is inferior as compared to a pure binary code. Moreover, relatively large bits are required for the conversion of any decimal number into a BCD code. As a result, performance suffers, apart from the need for the provision of an extra circuit (e.g. 25 in the pure binary code is written as 11001 (look at the table 1.2). However, in situation of BCD, 25 is written as 0010 0101). Resultantly, cost goes up, circuit becomes more complex and more power consuming. Moreover, by invalidating or not using the six numbers (10 – 15) is in itself a major drawback. As a consequence, effectiveness deteriorates further.

**Conversion of Decimal to BCD Code & BCD Code to Decimal**

Any decimal number can be converted to a BCD code quite effortlessly. For this purpose, a 4-bit binary code is written with each and every decimal digit of a decimal number. And a reasonable space is left within every 4 – bit groups. On the contrary, a BCD number can also quite conveniently be converted to a decimal number. For this end, the code number is divided into a 4 – bit groups (starting with the LSB side). Thereafter, a decimal digit is mentioned with every 4 – bit group.

**Example. 18 Convert the decimal number 369 in BCD**

Solution:

With the help of table 1.5, replace the value of each decimal digit by its 4 – bit equivalent from the BCD code.

3 = 0011.6 6= 0110 9= 1001 Thus, 36910 = 0011 0110 1001 BCD (Ans)

**Example. 19 Convert the BCD code number 000101000 1110101 into equivalent decimal value.**

Solution:

Starting from the LSB, the given number can be divided into groups of four bits as.

0001 0100 0111 0101

With the help of table, we get.

0001 0100 0111 0101

↓ ↓ ↓ ↓

Hence, 0001010001110101_{BCD} = 1475_{10 }(Ans)

** Special Binary Codes or Non-Weighted Codes**

These codes are normally of the following types.

- Gray Codes
- Excess-3 Code
- ASCII (American Standard Code for Information Interchange)

A brief explanation of the aforementioned codes is as under:

** Gray Code**

Gray code is a non – weighted or un – weighted and non – arithmetic code, which is profusely employed in mechanical switching systems. It means that no specific weight or position value is fixed for the bit positions of this code, nor can they be utilized for any kind of arithmetic operations (i.e. gray code is not used when numbers are required to be added, subtracted or during any other arithmetic operation). This code is also known as a cyclic or reflective code.

In figure 1.7, a 4 – bit gray code has been demonstrated. Just like a pure binary code, the first ten digits (0 – 9) are also used in BCD. An important advantage of gray code is that a change of only one bit takes place from one code number to other (which has been explained via the table). The very advantage of this code makes it pretty suitable for a number of applications. For example, in a shaft position encoder, where the possibility of errors mounts owing to a change between the quantity of bits of arranged numbers located adjacently. In such situations, gray codes are enormously used, which eliminate inherent faults existing in the binary code, because, a change of just one – bit occurs between such end-to-end numbers. As a consequence, the quantity of switching declines and trust on the switching system grows further. Moreover, function of the circuit also improves effectively due to a minimum possibility of committing any sort of errors under the gray code technique.

In table 1.7, a 4 – bit gray code has been displayed, which consists of a list of 4 – bit gray codes for the decimal numbers ranged from 0 – 15. In order to comprehend mechanism that a variation of just one bit occurs between one number of a gray code to the other (or end -to – end numbers), it is necessary to go through the given table carefully. For example, note down the change taking place between decimal number 3 and decimal number 4. As a result of an inter -change taking place between these two decimal numbers, a change of just one bit occurs in the gray code from 0010 to 0110. Whereas, in case of binary codes **or Digital Codes**, a change of 3 bits occurs from 0011 to 0100 (that’s first both bits change from 11 to 00 and the third bit changes from 0 to 1)

Table 1.7 Four–Bit Gray Code

Decimal | Binary | Gray Code | Decimal | Binary | Gray Code |

0 | 0000 | 0000 | 8 | 1000 | 1100 |

1 | 0001 | 0001 | 9 | 1001 | 1101 |

2 | 0010 | 0011 | 10 | 1010 | 1111 |

3 | 0011 | 0010 | 11 | 1011 | 1110 |

4 | 0100 | 0110 | 12 | 1100 | 1010 |

5 | 0101 | 0111 | 13 | 1101 | 1011 |

6 | 0110 | 0101 | 14 | 1110 | 1001 |

7 | 0111 | 0100 | 15 | 1111 | 1000 |

**The Excess -3 Code**

The excess-3 code (which is shortly denoted as XS-3), is actually a modified form of BCD. It is an unweighted code, which is immensely used in digital instruments in order to express numerical or arithmetic data and information.

The excess-3 code has been explained vide table 1.8 through decimal numbers from 0 to 9. Every code number mentioned in XS-3 code is three-fold compared to the number mentioned in BCD code. For example, decimal number 6, which is equal to 0110 in case of BCD, whereas it is mentioned as 1001 in case of XS-3 (as has been illustrated in the table), which in fact is 9. It implies that XS-3 is three times larger as compared to a BCD. It needs to be remembered here that XS-3 is more feasible for the purpose of carrying out arithmetic operations as compared to BCD.

Table 1.8 Excess-3 Code

Decimal | BCD | Excess-3 |

0 | 0000 | 0001 |

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 |

Thus, excess – 3 is an important 4 – bit code, which can occasionally be used in conjunction with BCD and which is utilized by means of adding 3 to every decimal digit (i.e. in order to convert a decimal number to an equivalent 3 – excess code, 3 is added with every decimal digit) and then the sum is changed to a BCD number. For example, excess – 3 codes of decimal 2 and decimal 9 are as under:

2 9

+3 +3

ꟷ ꟷ

5 → 0101 12 → 1100

**Example. 20 Convert 12 to an excess – 3 code.**

Solution:

First add 3 to each decimal digit.

1 2

+3 +3

ꟷ ꟷ

4 5

Second, convert the sum to BCD form.

4 5

↓ ↓

0100 0101

So, 0100 0101 is the excess -3 code which stands for decimal 12.

**Alpha- Numeric Code**

The special type of codes that are generally used to depict numbers, letters, symbols, and instructions, are known as alpha–numeric codes (that’s the binary codes **or Digital Codes** which are used in order to express the characteristics of both alphabetic as well as alphanumeric type. Because for the transmission or communication of information from one place to another, we not only need numbers, we also require the application of certain letters)

Alphanumeric code is a binary code, which comprises 10 decimal digits, 26 alphabet letters and a number of other symbols (e.g. $, % and? etc.). The most important type of alphanumeric code is ASCII, which is known as an American standard code for information. The detail of this code is as under:

**American Standard Code for Information Interchange(ASCII)**

ASCII is a world recognized alpha – numeric code which is mostly used in computers and other digital electronic devices. Most of the computers’ keyboards have been standardized with ASCII. When you enter a letter or word, a number or a control command, the pertinent ASCII code passes on to the computer.

ASCII consists a total of 128 characters and symbols, which is represented via a binary code (i.e. it is a 7 – bit binary code because a total of 2^{7}= 128 different conditions or numbers can be represented via a 7- bit). The first 32 ASCII characters include non- graphic commands, which can neither be printed nor displayed. Rather, these are only used for the purpose of exercising control. Null, line feed, start of text and escape etc. are some of the instances of control character. Other characters are graphic symbols, which can both be printed or displayed. It contains letters of alphabet (i.e. capital and small English characters), 10 decimal digits (i.e. numbers from 0 to 9) and punctuation signs and other commonly used symbols and codes.

In table 1.9, a complete list of ASCII code (which comprises 128 characters and symbols) has been illustrated. This table represents every character and symbol in a decimal, hexadecimal and binary. The names of 32 control characters (00 to IF hexadecimal), have been presented in the left section. These control characters are used for communication purposes while transmitting data or information through devices e.g. computers and printers. Whereas graphic symbols (from 20 to 7F hexadecimal) have been demonstrated in the remaining section of the table.

Table 1.10 ASCII Control Characters

Name | Decimal | Hex | Key | Description |

Null | 0 | 00 | CTRL a | Null character |

SOH | 1 | 01 | CTRL.A | Start of header |

STX | 2 | 02 | CTRL.B | Start of text |

ETX | 3 | 03 | CTRL.C | End of text |

EOT | 5 | 05 | CTRL. D | End of transmission |

ENQ | 5 | 05 | CTRL.E | Enquire |

ACK | 6 | 06 | CTRL. F | Acknowledge |

BEL | 7 | 07 | CTRL.G | Bell |

BS | 8 | 08 | CTRL.H | Back Space |

HT | 9 | 09 | CTRL.I | Horizontal tab |

LF | 10 | 0A | CTRL.J | Line feed |

VT | 11 | 0B | CTRL.K | Vertical tab |

FF | 12 | 0C | CTRL.L | Form feed |

CR | 13 | 0D | CTRL.M | Carriage return |

SO | 14 | 0E | CTRL.N | Shift out |

SI | 15 | 0F | CTRL.O | Shift in |

DLF | 16 | 10 | CTRL.P | Date line escape |

DC1 | 17 | 11 | CTRL.Q | Device control1 |

DC2 | 18 | 12 | CTRL.R | Device control 2 |

DC3 | 19 | 13 | CTRL.S | Device control 3 |

DC4 | 20 | 14 | CTRL.T | Device control 4 |

NAK | 21 | 15 | CTRL.U | Negative acknowledge |

SYN | 22 | 16 | CTRL.V | Synchronize |

ETB | 23 | 17 | CTRL.W | End of transmission block |

CAN | 24 | 18 | CTRL.X | Cancel |

EM | 25 | 19 | CTRL.Y | End of medium |

SUB | 26 | 1A | CTRL.Z | Substitute |

ESC | 27 | 1B | CTRL. │ | Escape |

FS | 28 | 1C | CTRL. / | File separator |

GS | 29 | 1D | CTRL. │ | Group separator |

RS | 30 | 1E | CTRL. ^ | Record separator |

US | 31 | 1F | CTRL. | Unit separator |

In order to provide and receive information via a computer, we often need to apply certain alpha numeric codes. There was a time when manufactures used their own alpha – numeric codes, which caused all sorts of mix-up and confusion. Eventually, the industry agreed on an input – output code, which is known as American Standard Code for Information Interchange (ASCII). It must be remembered that ASCII is pronounced as ask – ee). After the introduction of this code, different computer hardware’s e.g. keyboard, printers, video displays etc. got standardized. ASCII is a 7 – bit code, the format of which is as follows:

3-Bit Group 4- Bit Group

X_{6} X_{5} X_{4} X_{3} X_{2} X_{1} X_{0}

It should be remembered that the ASCII 7 – bit code for every letter, number or control function is composed through a group of 3 – bit and another group of 4 – bit and that the value of each X in every group is zero or 1. In the table 1.11, ASCII code has been displayed. Through the application of this table, alphabet letters or some commonly used symbols can be determined from top or bottom. For example, the table reveals that the X_{4}, X_{5}, X_{6} value of capital letter A is 100 (this is binary value of 3 – bit) and value of X_{0}, X_{1}, X_{2}, X_{3} (i.e. binary value of next 4 – bit) is 0001. As such, the ASCII code value for A is as under:

X_{6} X_{5} X_{4} X_{3} X_{2} X_{1} X_{0}

1 0 0 0 0 0 1

For reading purposes, some space is left blank in-between. i.e.

100 0001 (A)

Some other examples are as follows:

110 0001 (a)

110 0010 (b)

110 0011 ©

010 0100 ($)

010 1011 (+)

011 1101 (=)

010 0000 (SP or Space)

ASCII code is quite extensively used in computers and data communication devices (e.g. printers, telephone lines etc.) for data communication.

Table 1.11 ASCII Code

X_{6}X_{5}X_{4} | ||||||

X_{3}X_{2}X_{1}X_{0} | 010 | 011 | 100 | 101 | 110 | 111 |

0000 | SP | 0 | @ | P | p | |

0001 | ! | 1 | A | Q | a | q |

0010 | “ | 2 | B | R | b | r |

0011 | # | 3 | C | S | c | s |

0100 | $ | 4 | D | T | d | t |

0101 | % | 5 | E | U | e | u |

0110 | & | 6 | F | V | f | v |

0111 | ‘ | 7 | G | W | g | w |

1000 | ( | 8 | H | X | h | x |

1001 | ) | 9 | I | Y | i | y |

1010 | * | : | J | Z | j | z |

1011 | + | ; | K | k | ||

1100 | ‘ | < | L | l | ||

1101 | – | = | M | m | ||

1110 | . | > | N | n | ||

1111 | / | ? | O | o |

**Previous Topic: **Digital and Analogue Quantities

**Next Topic: **Basic Elements of Digital Electronics

For electronics and programming-related projects visit my YouTube channel.