Skip to content

Electronics · Ch 10 — Digital Electronics

Digital codes

10.2

Digital codes

Computers and electronic devices need a systematic, precise way to represent information so that every character, letter, digit and symbol is unique and distinguishable. Analog data is therefore processed into digital data that is represented, stored and transmitted as groups of binary bits. A digital code is simply a rule for converting information into such groups of 0s and 1s.

Codes are broadly of three kinds: a binary (digital) code is a group of bits using only 0 and 1; a numeric code represents numbers; and an alphanumeric code represents letters, characters and numerals. Numeric codes are further classified as weighted codes, which obey the positional-weight principle (each bit position carries a fixed weight, e.g. 8-4-2-1, 2-4-2-1, 5-4-2-1), and non-weighted codes, which do not (e.g. Excess-3 and Gray code).

BCD (8-4-2-1) code

In Binary Coded Decimal (BCD) each decimal digit is represented by its own group of four bits. The most common is the 8-4-2-1 BCD code, where the four bit positions carry weights 8, 4, 2 and 1 from left to right (table 10.2.1). Because four bits can form sixteen combinations but only ten are needed, six combinations (1010 to 1111) are invalid in BCD. BCD is less efficient than pure binary but is easy to convert to and from decimal, so it is used in calculators, seven-segment displays and digital watches. For a multi-digit number each decimal digit is coded separately — for example, decimal 42 becomes 0100 0010.

Self-complementing codes and 9's complement

A code is self-complementing when the code of the 9's-complement of a digit is obtained simply by interchanging all the 1s and 0s (taking the 1's complement) of that digit's code. The 9's complement of a decimal digit is found by subtracting it from 9 (table 10.2.2); for a multi-digit number each digit is complemented separately — e.g. the 9's complement of 7825 is 2174.

Excess-3 (XS-3) code

The Excess-3 code is a non-weighted, self-complementing code formed by adding 0011 (binary) — that is, 3 (decimal) — to each 8-4-2-1 BCD code word (table 10.2.3). Its self-complementing property makes it convenient for arithmetic subtraction. Six of its sixteen combinations (0000, 0001, 0010, 1101, 1110, 1111) are invalid.

2-4-2-1 code

The 2-4-2-1 code is a weighted, self-complementing 4-bit code whose bit weights from left to right are 2, 4, 2 and 1 (table 10.2.4). Because two positions share the weight 2, some digits could be coded in more than one way; to preserve the self-complementing property the standard coding is used and six combinations (0101, 0110, 0111, 1000, 1001, 1010) must not be used. It is widely used in arithmetic operations.

Gray code

The Gray code is a non-weighted unit-distance (minimum-change) code: successive numbers differ by only one bit (table 10.2.5). Because it is non-weighted it cannot be used directly for arithmetic, but the single-bit change makes it valuable in shaft-position encoders, analog-to-digital conversion and reducing errors in data transmission.

  • Binary → Gray: copy the MSB unchanged, then each remaining Gray bit is the XOR of two adjacent binary bits. Figure 10.2.1 realizes this with three XOR gates.
  • Gray → Binary: copy the MSB unchanged, then each binary bit is the XOR of the previous binary bit with the current Gray bit. Figure 10.2.2 realizes this with three XOR gates.

Alphanumeric codes

Alphanumeric codes represent digits, alphabets and special characters (at least 10 digits and 26 letters). The three common ones are ASCII, EBCDIC and the five-bit Baudot code. …

Definition 1Digital code

A rule for converting information into groups of binary bits (0s and 1s) so that each character, digit or symbol is uniquely represented, stored and transmitted. A binary/digital code uses only 0 and 1; a numeric code represents numbers; an alphanumeric cod …

Definition 2Weighted and non-weighted codes

A weighted code obeys the positional-weight principle — each bit position carries a fixed weight (e.g. 8-4-2-1, 2-4-2-1, 5-4-2-1). A non-weighted code does not follow any positional weig …

Table 38-4-2-1 BCD code for decimal digits 0 to 9

Column weights 8, 4, 2, 1.

Decimal8421
00000
10001
20010
30011
40100
50101
60110
70111
81000
91001
Definition 4Binary Coded Decimal (BCD)

A code in which each decimal digit is represented by its own separate group of four bits. The common form is 8-4-2-1 BCD. It is easy to convert to and from decimal but less efficient than pure binary; used in calculators, sev …

Definition 5Self-complementing code

A code in which the code word of the 9's complement of a decimal digit is obtained by simply interchanging all the 1s and 0s (the 1's complement) of that digit's code word. Excess-3 and 2-4 …

Table 69's complement of each decimal digit

The 9's complement of a digit is 9 minus that digit.

Decimal9's complement
09
18
27
36
45
54
63
72
Table 7Excess-3 code for decimal digits 0 to 9

Excess-3 = 8-4-2-1 BCD + 0011.

DecimalBCD (8421)Excess-3
000000011
100010100
200100101
300110110
401000111
501011000
601101001
701111010
810001011
910011100
Formula 8Excess-3 conversion rule

Excess-3 code = 8-4-2-1 BCD + 0011 (binary) = decimal digit + 3. Add 0011 to each BCD nibble to obtain its Excess-3 code word. Because 3 is added to every digit, Excess-3 is non-weighted and self-complementing: e.g. decimal 5 → BCD 0101 → add 0011 → Excess-3 1000. This shift is …

Table 92-4-2-1 weighted self-complementing BCD code

Column weights 2, 4, 2, 1 (left to right).

Decimal2421
00000
10001
20010
30011
40100
51011
61100
71101
81110
91111

Check: e.g. 5 = 1011 = 2+0+2+1 and 9 = 1111 = 2+4+2+1. The six combinations 0101, 0110, 0111, 1000, 1001, 1010 must not be used. …

Table 10BCD and Gray code for decimal digits 0 to 9

Successive Gray codes differ by exactly one bit.

DecimalBCDGray
000000000
100010001
200100011
300110010
401000110
501010111
601100101
701110100
Definition 11Gray code

A non-weighted, unit-distance (minimum-change) code in which successive values differ by only one bit. It cannot be used for arithmetic but is used in shaft-position encoders, analog-to-digital conversion and …

Figure 12Binary-to-Gray code conversion circuit using three exclusive-OR gates.
Fig. 12 — Binary-to-Gray code conversion circuit using three exclusive-OR gates.

Drawn by us to help you understand the concept clearly, and verified to make sure it's accurate. For exams, practice from your textbook's own diagram.

Figure 10.2.1: the four binary bits enter on the left; the MSB passes straight through to the Gray output, and each remaining Gray bit is produced by an XOR gate combining two adjacent binary bits. Illustra …

Figure 13Gray-to-binary code conversion circuit using three exclusive-OR gates.
Fig. 13 — Gray-to-binary code conversion circuit using three exclusive-OR gates.

Drawn by us to help you understand the concept clearly, and verified to make sure it's accurate. For exams, practice from your textbook's own diagram.

Figure 10.2.2: the Gray bits enter on the left; the MSB passes straight through to the binary MSB, and each further XOR combines the running binary result with the next Gray bit. Illustrated …

Formula 14Gray code conversion rules

Binary to Gray: the MSB is copied, then each Gray bit gi=bi⊕bi−1g_i = b_i \oplus b_{i-1} (XOR of adjacent binary bits). Gray to binary: the MSB is copied, then each binary bit bi=bi−1⊕gib_i = b_{i-1} \oplus g_i (XOR of the previous …

Definition 15Alphanumeric codes (ASCII, EBCDIC, Baudot)

Codes that represent digits, alphabets and special characters (at least 10 digits and 26 letters). The three common ones are ASCII (7-bit, 128 groups), EBCDIC (8-bit, 256 character …

Table 16Partial listing of the ASCII code with binary, octal, hexadecimal and decimal values

A sample of the 7-bit ASCII code.

Character7-bit binaryOctalHexDecimal
line feed000 10100120A10
return000 11010150D13
blank (space)010 00000402032
$010 01000442436
(010 10000502840
)010 10010512941
*010 10100522A42
+010 10110532B43
,010 11000542C44
-010 11010552D45
/010 11110572F47
0011 00000603048
9011 10010713957
=011 11010753D61
A100 00011014165
Z101 10101325A90
a110 00011416197
z111 10101727A122
Table 17BCD (6-bit) and EBCDIC (8-bit) codes for sample characters

Each character is split into a zone part and a digit part. EBCDIC zones: A-I = 1100, J-R = 1101, S-Z = 1110, digits = 1111.

CharacterBCD zone-digit (6-bit)EBCDIC zone-digit (8-bit)
A11 00011100 0001
C11 00111100 0011
P10 01111101 0111
U01 01001110 0100
Z01 10011110 1001
100 00011111 0001
900 10011111 1001