0% found this document useful (0 votes)
3 views14 pages

2 Binary Codes

The document discusses the classification of binary codes, including numeric and alphanumeric codes, weighted and non-weighted codes, and various types of binary coded decimal (BCD) codes. It explains the advantages and disadvantages of the 8421 BCD code, including its use in mathematical operations and the complexities of BCD addition and subtraction. Additionally, it covers error detecting and correcting codes, sequential codes, self-complementing codes, cyclic codes, and reflective codes.

Uploaded by

pravin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views14 pages

2 Binary Codes

The document discusses the classification of binary codes, including numeric and alphanumeric codes, weighted and non-weighted codes, and various types of binary coded decimal (BCD) codes. It explains the advantages and disadvantages of the 8421 BCD code, including its use in mathematical operations and the complexities of BCD addition and subtraction. Additionally, it covers error detecting and correcting codes, sequential codes, self-complementing codes, cyclic codes, and reflective codes.

Uploaded by

pravin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

3

BINARY CODES

3.1 CLASSIFICATION OF BINARY CODES


3.1.1 Numeric and Alphanumeric Codes
Binary codes can be classified as numeric codes and alphanumeric codes. Numeric codes are
codes which represent numeric information, i.e. only numbers as a series of 0s and 1s. 8421,
XS-3, Gray code are numeric codes. Numeric codes used to represent the decimal digits are called
Binary Coded Decimal (BCD) codes. 8421, 2421, 5211 are BCD codes. Alphanumeric codes are
codes which represent alphanumeric information, i.e. letters of the alphabet and decimal numbers
as a sequence of 0s and 1s. EBCDIC code and ASCII code are alphanumeric codes.
We are very comfortable with the decimal number system, but digital systems force us to use
the binary system. Although the binary number system has many practical advantages and is
widely used in digital computers, in many cases it is very convenient to work with decimal numbers,
especially when communication between man and machine is extensive. Since most of the numerical
data generated by man are in decimal numbers, to simplify the communication process between
man and machine, several systems of numeric codes have been devised to represent decimal numbers
as a series of BCD codes.
A BCD code is one, in which the digits of a decimal number are encoded—one at a time—into
groups of four binary digits. These codes combine the features of decimal and binary numbers. There
are a large number of BCD codes. In order to represent decimal digits 0, 1, 2,…,9, it is necessary to
use a sequence of at least four binary digits. Such a sequence of binary digits which represents a
decimal digit is called a code word. All BCD codes have at least six unassigned code words.

3.1.2 Weighted and Non-weighted Codes


The BCD codes may be weighted codes or non-weighted codes. The weighted codes are those
which obey the position-weighting principle. Each position of the number represents a specific
86
BINARY CODES 87

weight. For each group of four bits, the sum of the weights of those positions where the binary
digit is 1 is equal to the decimal digit which the group represents. 8421, 2421, 84-2-1 are weighted
codes. Non-weighted codes are codes which are not assigned with any weight to each digit position,
i.e. each digit position within the number is not assigned fixed value. Excess-3(XS-3) code and
Gray code are non-weighted codes. There are several weighted codes.

3.1.3 Positively-weighted and Negatively-weighted Codes


The weighted codes may be either positively-weighted codes or negatively-weighted codes.
Positively-weighted codes are those in which all the weights assigned to the binary digits are
positive. There are only 17 positively-weighted codes. In every positively-weighted code, the first
weight must be 1, the second weight must be either 1 or 2, and the sum of all the weights must be
equal to or greater than 9. The codes 8421, 2421, 5211, 3321 and 4311 are some of the positively-
weighted codes available. Negatively-weighted codes are those in which some of the weights
assigned to the binary digits are negative. The codes 642–3, 631–1, 84–2–1 and 74–2–1 are some
of the negatively-weighted codes.
Table 3.1 shows some of the positively-weighted, negatively-weighted, and non-weighted
codes.

Table 3.1 Binary coded decimal codes

Decimal digit 8421 2421 5211 5421 6 4 2 –3 8 4 –2 –1 XS-3


0 0000 0000 0000 0000 0000 0000 0011
1 0001 0001 0001 0001 0101 0111 0100
2 0010 0010 0011 0010 0010 0110 0101
3 0011 0011 0101 0011 1001 0101 0110
4 0100 0100 0111 0100 0100 0100 0111
5 0101 1011 1000 1000 1011 1011 1000
6 0110 1100 1010 1001 0110 1010 1001
7 0111 1101 1100 1010 1101 1001 1010
8 1000 1110 1110 1011 1010 1000 1011
9 1001 1111 1111 1100 1111 1111 1100

Unused 1010 0101 0010 0101 0001 0001 0000


bit 1011 0110 0100 0110 0011 0010 0001
combinations 1100 0111 0110 0111 0111 0011 0010
1101 1000 1001 1101 1000 1100 1101
1110 1001 1011 1110 1100 1101 1110
1111 1010 1101 1111 1110 1110 1111

3.1.4 Error Detecting and Error Correcting Codes


Binary codes may also be error detecting codes or error correcting codes. Codes which allow only
error detection are called error detecting codes. Shift counter code, 2-out-of-5, 63210 codes are
error detecting codes. Codes which allow error detection as well as error correction are called
error correcting codes. The Hamming code is a error correcting code.
88 FUNDAMENTALS OF DIGITAL CIRCUITS

3.1.5 Sequential Codes


A sequential code is one, in which each succeeding code word is one binary number greater than
its preceding code word. Such a code facilitates mathematical manipulation of data. The 8421 and
XS-3 codes are sequential. The codes 5211, 2421 and 642–3 are not sequential.

3.1.6 Self-complementing Codes


A code is said to be self-complementing, if the code word of the 9’s complement of N, i.e. of 9 –
N can be obtained from the code word of N by interchanging all the 0s and 1s. Therefore, in a
self-complementing code, the code for 9 is the complement of the code for 0, the code for 8 is the
complement of the code for 1 and so on. The 2421, 5211, 642–3, 84–2–1 and XS-3 are
self-complementing codes. The 8421 and 5421 codes are not self-complementing. The
self-complementing property is desirable in a code when the 9’s complement must be found such
as in 9’s complement subtraction. Self complementing codes have an advantage that their logical
complement is the same as the arithmetic complement. For a code to be self-complementing, the
sum of all its weights must be 9. This is because whatever may be the weights, 0 is to be represented
by 0000 and since in a self-complementing code, the code for 9 is the complement of the code for
0, 9 has to be represented by 1111. There are only four (2421, 5211, 3321, 4311) positively-
weighted self-complementing codes. There are 13 negatively- weighted self-complementing codes.

3.1.7 Cyclic Codes


Cyclic codes are those in which each successive code word differs from the preceding one in only
one bit position. They are also called unit distance codes. The unit distance codes have special
advantages in that they minimize transitional errors or flashing. The Gray code is a cyclic code. It
is often used for translating an analog quantity such as shaft position into a digital form.

3.1.8 Reflective Codes


A reflective code is a binary code in which the n least significant bits for code words 2n through
2n+1 – 1 are the mirror images of those for 0 through 2n – 1. The Gray code is a reflective code.

3.1.9 Straight Binary Code


A straight binary code is used to represent numbers using natural binary form as discussed earlier.
Various arithmetic operations can be performed in this form. It is a weighted code since a weight
is attached to every position.
The classification of codes is shown in Figure 3.1.

3.2 THE 8421 BCD CODE (NATURAL BCD CODE)


The 8421 BCD code is so widely used that it is a common practice to refer to it simply as BCD
code. In this code, each decimal digit, 0 through 9, is coded by a 4-bit binary number. It is also
called the natural binary code because of the 8, 4, 2 and 1 weights attached to it. It is a weighted
code and is also sequential. Therefore, it is useful for mathematical operations. The main
advantage of this code is its ease of conversion to and from decimal. It is less efficient than the
BINARY CODES 89

pure binary, in the sense that it requires more bits. For example, the decimal number 14 can be
represented as 1110 in pure binary but as 0001 0100 in 8421 code. Another disadvantage of the
BCD code is that, arithmetic operations are more complex than they are in pure binary. There
are six illegal combinations 1010, 1011, 1100, 1101, 1110 and 1111 in this code, i.e. they are
not part of the 8421 BCD code system. A disadvantage of the 8421 code is that, the rules of
binary addition and subtraction do not apply to the entire 8421 number but only to the individual
4-bit groups.

Figure 3.1 Classification of codes.

3.2.1 BCD Addition


The BCD addition is performed by individually adding the corresponding digits of the decimal
numbers expressed in 4-bit binary groups starting from the LSD. If there is no carry and the sum
term is not an illegal code, no correction is needed. If there is a carry out of one group to the next
group, or if the sum term is an illegal code, then 610 (0110) is added to the sum term of that
group and the resulting carry is added to the next group. (This is done to skip the six illegal
states).
EXAMPLE 3.1 Perform the following decimal additions in the 8421 code.
(a) 25 + 13 (b) 679.6 + 536.8
Solution
(a) 25 0010 0101 (25 in BCD)

+13 +0001 0011 (13 in BCD)
38 0011 1000 (No carry, no illegal code. So, this is the correct sum.)
90 FUNDAMENTALS OF DIGITAL CIRCUITS

(b) 679.6 0110 0111 1001 .0110 (679.6 in BCD)



+ 536.8 +0101 0011 0110 .1000 (536.8 in BCD)
1216.4 1011 1010 1111 .1110 (All are illegal codes)
+0110 +0110 +0110 +.0110 (Add 0110 to each)
0001 0000 0101 .0100 (Propagate carry)
+1  +1  +1  +1 
0001 0010 0001 0110 .0100 (Corrected sum = 1216.4)
1 2 1 6 . 4

3.2.2 BCD Subtraction


The BCD subtraction is performed by subtracting the digits of each 4-bit group of the subtrahend
from the corresponding 4-bit group of the minuend in binary starting from the LSD. If there is no
borrow from the next higher group then no correction is required. If there is a borrow from the
next group, then 610 (0110) is subtracted from the difference term of this group. (This is done to
skip the six illegal states.)
EXAMPLE 3.2 Perform the following decimal subtractions in the 8421 BCD code.
(a) 38 – 15 (b) 206.7 – 147.8
Solution
(a) 38 0011 1000 (38 in BCD)

–15 –0001 0101 (15 in BCD)
23 0010 0011 (No borrow. So, this is the correct difference.)
(b) 206.7 0010 0000 0110 .0111 (206.7 in BCD)

–147.8 –0001 0100 0111 .1000 (147.8 in BCD)
58.9 0000 1011 1110 .1111 (Borrows are present, subtract 0110)
–0110 –0110 –.0110
0101 1000 .1001 (Corrected difference = 58.910)

3.2.3 BCD Subtraction Using 9’s and 10’s Complement Methods


In practice, subtraction is performed by the complement method. Since we are subtracting decimal
digits, we must form the 9’s or 10’s complement of the decimal subtrahend and encode that number
in the 8421 code. The resulting BCD numbers are then added.
EXAMPLE 3.3 Perform the following decimal subtractions in BCD by the 9’s complement
method.
(a) 305.5 – 168.8 (b) 679.6 – 885.9
Solution
(a) 305.5 305.5

–168.8 +831.1 (9’s complement of 168.8)
136.7 136.6
 + 1 (End around carry)
136.7 (Corrected difference)
BINARY CODES 91

305.510 0011 0000 0101 .0101 (305.5 in BCD)



+831.110 +1000 0011 0001 .0001 (9’s complement of 168.8 in BCD)
+1011 0011 0110 .0110 (1011 is an illegal code, add 0110)
+0110
0001 0011 0110 .0110
 +1 (End around carry)
0001 0011 0110 .0111 (Corrected difference = 136.7)
(b) 679.6 679.6

–885.9 +114.0 (9’s complement of 885.9)
–206.3 793.6 (No carry)
There is no carry indicating that the result is negative and is in its 9’s complement form. The
9’s complement of 793.6 is 206.3. So, the result is –206.3.
679.610 0110 0111 1001 .0110 (679.6 in BCD)

+114.010 +0001 0001 0100 .0000 (9’s complement of 885.9 in BCD)
0111 1000 1101 .0110 (1101 is an illegal code, add 0110)
+0110
0111 1000 0011 .0110 (Propagate carry)
+1 
0111 1001 0011 .0110 (No carry; 793.6 in BCD)
There is no carry and, therefore, the result is negative and is in its 9’s complement form. The
9’s complement of 793.6 is 206.3. So the result is –206.3.
EXAMPLE 3.4 Perform the following subtractions in 8421 code using the 10’s complement
method.
(a) 342.7 – 108.9 (b) 206.4 – 507.6
Solution
(a) 342.7 342.7

–108.9 +891.1 (10’s complement of 108.9)
233.8 233.8 (Ignore carry)
342.7 0011 0100 0010 .0111 (342.7 in BCD)

+891.1 +1000 1001 0001 .0001 (10’s complement of 108.9 in BCD)
1011 1101 0011 .1000 (1011 and 1101 are illegal codes,
add 0110 to each.)
+0110 +0110
0001 0011 0011 .1000 (Propagate carry)
+1  +1 
 0010 0011 0011 .1000 (Ignore carry)
0010 0011 0011 .1000 (Corrected difference = 233.8)
92 FUNDAMENTALS OF DIGITAL CIRCUITS

(b) 206.4 206.4



–507.6 +492.4 (10’s complement of 507.6)
–301.2 698.8 (No carry)
No carry. So, the answer is negative and is in 10’s complement form. The 10’s complement
of 698.8 is – 301.2.
206.4 0010 0000 0110 .0100 (206.4 in BCD)

+492.4 +0100 1001 0010 .0100 (10’s complement of 507.6 in BCD)
0110 1001 1000 .1000 (No illegal codes, no carry)
As there is no carry, the result is negative and is in its 10’s complement form. The 10’s
complement of 698.8 is 301.2. So, the corrected difference is –301.2.

3.3 THE EXCESS THREE (XS-3) CODE


The Excess-3 code, also called XS-3, is a non-weighted BCD code. This code derives its
name from the fact that each binary code word is the corresponding 8421 code word plus
0011(3). It is a sequential code and, therefore, can be used for arithmetic operations. It is a
self-complementing code. Therefore, subtraction by the method of complement addition is
more direct in XS-3 code than that in 8421 code. The XS-3 code has six invalid states 0000,
0001, 0010, 1101, 1110 and 1111. The XS-3 code has some very interesting properties when
used in addition and subtraction.

3.3.1 XS-3 Addition


To add in XS-3, add the XS-3 numbers by adding the 4-bit groups in each column starting
from the LSD. If there is no carry out from the addition of any of the 4-bit groups, subtract
0011 from the sum term of those groups (because when two decimal digits are added in XS-3
and there is no carry, the result is in XS-6). If there is a carry out, add 0011 to the sum term of
those groups (because when there is a carry, the invalid states are skipped and the result is in
normal binary).
EXAMPLE 3.5 Perform the following additions in XS-3 code.
(a) 37 + 28 (b) 247.6 + 359.4
Solution
(a) 37 0110 1010 (37 in XS-3)

+28 +0101 1011 (28 in XS-3)
65 1011 0101 (Carry generated)
+1  (Propagate carry)
1100 0101 (Add 0011 to correct 0101 and
–0011 +0011 subtract 0011 to correct 1100)
1001 1000 (Corrected sum in XS-3 = 6510)
BINARY CODES 93

(b) 247.6 0101 0111 1010 .1001 (247.6 in XS-3)



+359.4 +0110 1000 1100 .0111 (359.4 in XS-3)
607.0 1011 1111 0110 .0000 (Carry generated)
+ 1  +1  (Propagate carry)
1011 0000 0111 .0000
+1 
1100 0000 0111 .0000 (Add 0011 to 0000, 0111, 0000
–0011 +0011 +0011 +.0011 and subtract 0011 from 1100)
1001 0011 1010 .0011 (Corrected sum in XS-3 = 607.010)

3.3.2 XS-3 Subtraction


To subtract in XS-3, subtract the XS-3 numbers by subtracting each 4-bit group of the subtrahend
from the corresponding 4-bit group of the minuend starting from the LSD. If there is no borrow
from the next 4-bit group, add 0011 to the difference term of such groups (because when decimal
digits are subtracted in XS-3 and there is no borrow, the result is in normal binary). If there is a
borrow, subtract 0011 from the difference term (because taking a borrow is equivalent to adding
six invalid states, so the result is in XS-6).
EXAMPLE 3.6 Perform the following subtractions in XS-3 code.
(a) 267 – 175 (b) 57.6 – 27.8
Solution
(a) 267 0101 1001 1010 (267 in XS-3)

–175 –0100 1010 1000 (175 in XS-3)
092 0000 1111 0010 (Correct 0010 and 0000 by adding 0011and
+0011 –0011 +0011 correct 1111 by subtracting 0011)
0011 1100 0101 (Corrected difference in XS-3 = 9210)
(b) 57.6 1000 1010 .1001 (57.6 in XS-3)

–27.8 –0101 1010 .1011 (27.8 in XS-3)
29.8 0010 1111 .1110 (Correct 0010 by adding 0011 and correct
+0011 –0011 –.0011 1110 and 1111 by subtracting 0011)
0101 1110 .1011 (Corrected difference in XS-3 = 29.810)

3.3.3 XS-3 Subtraction Using 9’s and 10’s Complement Methods


In practice, subtraction is performed by the 9’s complement or the 10’s complement method.
EXAMPLE 3.7 Perform the following subtractions in XS-3 code using the 9’s complement
method.
(a) 687 – 348 (b) 246 – 592
Solution
(a) The subtrahend (348) in XS-3 code and its complements are:
9’s complement of 348 = 651
94 FUNDAMENTALS OF DIGITAL CIRCUITS

XS-3 code of 348 = 0110 0111 1011


1’s complement of 348 in XS-3 = 1001 1000 0100
XS-3 code of 687 = 1001 1011 1010
687 687

–348 +651 (9’s complement of 348)
339 338
 +1 (End around carry)
339 (Corrected difference in decimal)
1001 1011 1010 (687 in XS-3)
+1001 1000 0100 (1’s complement of 348 in XS-3)
0010 0011 1110 (Carry generated)
+ 1  +1  (Propagate carry)
 0011 0011 1110
 +1 (End around carry)
0011 0011 1111 (Correct 1111 by subtracting 0011 and
+0011 +0011 –0011 correct both groups of 0011 by adding 0011)
0110 0110 1100 (Corrected difference in XS-3 = 33910)
(b) The subtrahend (592) in XS-3 and its complements are:
9’s complement of 592 = 407
XS-3 code of 592 = 1000 1100 0101
1’s complement of 592 in XS-3 = 0111 0011 1010
XS-3 code of 246 = 0101 0111 1001
246 246

–592 +407 (9’s complement of 592)
–346 653 (No carry)
As there is no carry, the result is negative and is in its 9’s complement form. The 9’s
complement of 653 is 346. So, the result is –346.
0101 0111 1001 (246 in XS-3)
+0111 0011 1010 (1’s complement of 592 in XS-3)
1100 1010 0011 (Propagate carry)
+1 
1100 1011 0011 (Correct 0011 by adding 0011 and correct
–0011 –0011 +0011 1011 and 1100 by subtracting 0011)
1001 1000 0110 (No carry)
The sum is the XS-3 form of 653. There is no carry. So, the result is negative and is in 1’s
complement form. The 1’s complement of the sum is 0110 0111 1001 in XS-3 code (34610). So,
the answer is – 346.
BINARY CODES 95

EXAMPLE 3.8 Perform the following subtractions in XS-3 code using the 10’s complement
method.
(a) 597 – 239 (b) 354 – 672
Solution
(a) 10’s complement of 239 = 761
XS-3 code of 239 = 0101 0110 1100
2’s complement of 239 in XS-3 code = 1010 1001 0100
XS-3 code of 597 = 1000 1100 1010
597 597

–239 +761 (10’s complement of 239)
358 358 (Ignore carry)
358 (Corrected difference in decimal)
1000 1100 1010 (597 in XS-3)
+1010 1001 0100 (2’s complement of 239 in XS-3)
0010 0101 1110 (Propagate carry)
+1  +1 
1 0011 0101 1110 (Ignore carry)
+0011 +0011 –0011 (Correct 1110 by subtracting 0011 and
0110 1000 1011 correct 0101 and 0011 by adding 0011)
(Corrected difference in XS-3 code = 358)
(b) 10’s complement of 672 = 328
XS-3 code of 672 = 1001 1010 0101
2’s complement of 672 in XS-3 code = 0110 0101 1011
XS-3 code of 354 = 0110 1000 0111
354 354

–672 +328 (10’s complement of 672)
–318 682 (No carry)
No carry. So, the result is negative and is in its 10’s complement form. The 10’s complement
of 682 is 318. So, the answer is – 318.
0110 1000 0111 (354 in XS-3)
+0110 0101 1011 (2’s complement of 672 in XS-3)
1100 1101 0010 (Propagate carry)
+1 
1100 1110 0010 (Correct 0010 by adding 0011 and correct
–0011 –0011 +0011 1110 and 1100 by subtracting 0011)
1001 1011 0101 (No carry)
The sum is the XS-3 form of 682. There is no carry. So, the result is negative and is in the 2’s
complement form. The 2’s complement of the sum is 0110 0100 1011 in XS-3 code (31810). So,
the answer is – 318.
96 FUNDAMENTALS OF DIGITAL CIRCUITS

EXAMPLE 3.9 Encode the decimal digits 0, 1, 2, . . ., 9 by means of weighted codes 3321,
4221, 731–2, 631–1, 5311, 74–2–1 and 7421.
Solution
The encoding is shown in Table 3.2.

Table 3.2 Encoded decimal digits

Decimal 3321 4221 7 3 1 –2 6 3 1 –1 5311 7 4 –2 –1 7421


0 0000 0000 0000 0000 0000 0000 0000
1 0001 0001 0010 0010 0001 0111 0001
2 0010 0010 0111 0101 0011 0110 0010
3 0011 0011 0100 0100 0100 0101 0011
4 0101 0110 0110 0110 0101 0100 0100
5 1010 1001 1001 1001 1000 1010 0101
6 1100 1100 1011 1011 1001 1001 0110
7 1101 1101 1000 1010 1011 1000 1000
8 1110 1110 1101 1101 1100 1111 1001
9 1111 1111 1111 1111 1101 1110 1010

3.4 THE GRAY CODE (REFLECTIVE-CODE)


The Gray code is a non-weighted code, and is not suitable for arithmetic operations. It is not a
BCD code. It is a cyclic code because successive code words in this code differ in one bit position
only, i.e. it is a unit distance code. It is the most popular of the unit distance codes. It is also a
reflective code, i.e. it is both reflective and unit distance. The n least significant bits for 2n through
2n+1 – 1 are the mirror images of those for 0 through 2n – 1. An N-bit Gray code can be obtained by
reflecting an N – 1 bit code about an axis at the end of the code, and putting the MSB of 0 above
the axis and the MSB of 1 below the axis. Reflection of Gray codes is shown in Table 3.3. Another
property of the Gray code is that the Gray-coded number corresponding to the decimal number
2n – 1 for any n differs from Gray-coded 0 in one bit position only. This property places the Gray
code for the largest N-bit binary number at unit distance from 0. It is easier to determine the pattern
assignment corresponding to an arbitrary number, or decode an arbitrary number Gray code pattern
using the conversions to and from binary. In fact, one reason for the popularity of the Gray code is
its ease of conversion to and from binary. Gray codes are used in instrumentation and data acquisition
systems where linear or angular displacement is measured. They are also used in shaft encoders,
I/O devices, A/D converters and other peripheral equipment.
Consider a rotating disk that provides an output of its position in 3-bit binary (Figure 3.2).
When the brushes are on the black part, they output a 1, and when they are on a white sector they
output a 0. Suppose the disk is coded in binary; consider now what happens when the brushes are
on the 111 sector and almost ready to enter the 000 sector. If one brush were slightly ahead of the
other, say the 4’s brush, the position would be indicated by a 011 instead of a 111 or 000. Therefore,
a 180° error in disk position would result. Since it is physically impossible to have all the brushes
precisely aligned, some error would always be present at the edges of the sectors. If the disks were
coded in gray, a similar error would make the disk to be read with a very small error. For example
BINARY CODES 97

if the brushes are on the sector 010 and almost ready to enter the 110 sector and if the 4’s brush is
slightly ahead, the position would be indicated by 110 instead of 010 resulting in a very small
error. Figure 3.2 illustrates this operation.

Table 3.3 Reflection of Gray codes

Gray Code Decimal 4-bit binary


1-bit 2-bit 3-bit 4-bit
0 00 000 0000 0 0000
1 01 001 0001 1 0001
11 011 0011 2 0010
10 010 0010 3 0011
110 0110 4 0100
111 0111 5 0101
101 0101 6 0110
100 0100 7 0111
1100 8 1000
1101 9 1001
1111 10 1010
1110 11 1011
1010 12 1100
1011 13 1101
1001 14 1110
1000 15 1111

Figure 3.2 Position indicating system.

3.4.1 Binary-to-Gray Conversion


If an n-bit binary number is represented by Bn Bn–1. . .B1 and its Gray code equivalent by
Gn Gn–1. . .G1, where Bn and Gn are the MSBs, then the Gray code bits are obtained from the binary
code as follows.
98 FUNDAMENTALS OF DIGITAL CIRCUITS

Gn = Bn Gn–1 = Bn ≈ Bn–1 Gn–2 = Bn–1 ≈ Bn–2 … G1 = B2 ≈ B1

where the symbol ≈ stands for the Exclusive OR (X-OR) operation explained below.
The conversion procedure is as follows:
1. Record the MSB of the binary as the MSB of the Gray code.
2. Add the MSB of the binary to the next bit in binary, recording the sum and ignoring the
carry, if any, i.e. X-OR the bits. This sum is the next bit of the Gray code.
3. Add the 2nd bit of the binary to the 3rd bit of the binary, the 3rd bit to the 4th bit, and so
on.
4. Record the successive sums as the successive bits of the Gray code until all the bits of the
binary number are exhausted.
Another way to convert a binary number to its Gray code is to Exclusive OR (i.e. to take the
modulo sum of) the bits of the binary number with those of the binary number shifted one position
to the right. The LSB of the shifted number is discarded and the MSB of the Gray code number is
the same as the MSB of the original binary number.
EXAMPLE 3.10 Convert the binary 1001 to the Gray code.
Solution
(a) Binary 1 0 0 1
ΩΩ ΩΩ ΩΩ ΩΩ
Gray 1 1 0 1
(b) Binary 1001
Shifted binary 100
Gray 1101
Method I. As shown in (a), record the 8’s bit ‘1’ (MSB) of the binary as the 8’s bit of the Gray
code. Then add the 8’s bit of the binary to the 4’s bit of the binary (1 + 0 = 1). Record the sum
as the 4’s bit of the Gray code. Add the 4’s bit of the binary to the 2’s bit of the binary (0 + 0 =
0). Record the sum as the 2’s bit of the Gray code. Add the 2’s bit of the binary to the 1’s bit of
the binary (0 + 1 = 1). Record the sum as the 1’s bit of the Gray code. The resultant Gray code
is 1101.
Method II. As shown in (b), write the given binary number and add it to the same number
shifted one place to the right. Record the MSB of the binary, i.e. 1 as the MSB of the Gray code.
Add the subsequent columns (0 + 1 = 1; 0 + 0 = 0; 1 + 0 = 1) and record the corresponding sums as
the subsequent significant bits of the Gray code. Ignore the bit shifted out. The resultant Gray code
is 1101.

3.4.2 Gray-to-Binary Conversion


If an n-bit Gray number is represented by GnGn–1 . . . G1 and its binary equivalent by BnBn–1 . . . B1,
then binary bits are obtained from Gray bits as follows:

Bn = Gn Bn–1 = Bn ≈ Gn–1 Bn–2 = Bn–1 ≈ Gn–2 … B1 = B2 ≈ G1


BINARY CODES 99

The conversion procedure is:


1. The MSB of the binary number is the same as the MSB of the Gray code number; record
it.
2. Add the MSB of the binary to the next significant bit of the Gray code, i.e. X-OR them;
record the sum and ignore the carry.
3. Add the 2nd bit of the binary to the 3rd bit of the Gray; the 3rd bit of the binary to the 4th
bit of the Gray code, and so on, each time recording the sum and ignoring the carry.
4. Continue this till all the Gray bits are exhausted. The sequence of bits that has been
written down is the binary equivalent of the Gray code number.
EXAMPLE 3.11 Convert the Gray code 1101 to binary.
Solution
The conversion is done as shown below:
Gray 1 1 0 1
ΩΩ ΩΩ ΩΩ ΩΩ
Binary 1 0 0 1
Record the 8’s bit (MSB) of the Gray code as the 8’s bit of the binary. Add the 8’s bit of
the binary to the 4’s bit of the Gray code (1 + 1 = 10) and record the sum (0) as the 4’s bit of
the binary (ignore the carry bit). Add the 4’s bit of the binary to the 2’s bit of the Gray code
(0 + 0 = 0) and record the sum as the 2’s bit of the binary. Add the 2’s bit of the binary to the
1’s bit of the Gray code (0 + 1 = 1) and record the sum as the 1’s bit of the binary. The
resultant binary number is 1001.
EXAMPLE 3.12
(a) Convert the following into the Gray number.
(i) 3 A 716 (ii) 5 2 78 (iii) 6 5 210
(b) Convert the Gray number 1 0 1 1 0 0 1 0 into
(i) hex (ii) octal (iii) decimal
Solution
(a) To convert the given number in any system into Gray number first convert it into binary
and then convert that binary number into the Gray code using the normal procedure.
(i) 3 A 716 = 0 0 1 1, 1 0 1 0, 0 1 1 12 = 1 0 0 1 1 1 0 1 0 0 (Gray)
(ii) 5278 = 1 0 1, 0 1 1, 0 1 12 = 1 1 1 1 1 0 1 1 0 (Gray)
(iii) 65210 = 1 0 1 0 0 0 1 1 0 02 = 1 1 1 1 0 0 1 0 1 0 (Gray)
(b) To convert the given Gray number into any other number system, first convert the given
Gray number into a binary number and then convert that binary number into the required
number system.
1 0 1 1 0 0 1 0 (Gray) = 1 1 0 1 1 1 0 02 = DC16 = 3348 = 22010

3.4.3 The XS-3 Gray Code


In a normal Gray code, the bit patterns for 0 (0000) and 9 (1101) do not have a unit distance
between them. That is, they differ in more than one position. In XS-3 Gray code, each decimal

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy