2 Binary Codes
2 Binary Codes
BINARY CODES
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.
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.
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
fi
–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
fi
–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.
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.
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.