Complements and Codes
Complements and Codes
18B11EC215
Lecture 4
Complements and Codes
1
Outline
■ Complements
■ (r-1)’s (Diminshed Radix) complement
■ Subtraction using r’s complement
■ Subtraction using (r-1)’s complement
■ What are codes used for?
■ Binary Codes
■ BCD Codes
■ Excess-3 Codes
■ Gray Codes
■ Alphanumeric codes
2
Complements[1]
■ Complements are used in digital computers for simplifying the subtraction operation
and for logical manipulation.
(2) The (r-1)’s complement. E.g. 1’s complement for binary and 9’s complement for
decimal.
3
The (r-1)’s Complement[1]
■ Given a positive number N in base r with an integer part of n digits and a
fraction part of m digits, the (r-1)’s complement of N is defined as :
( rn – r-m)10 – N for N ≠ 0
4
Examples
Note: From observation it can be seen that 9’s complement of a decimal number
is formed by simply subtracting every digit from 9.
5
Example
■ The 1’s complement of (101100)2
Here n= 6 and m=0
6
Subtraction using r’s complements[2]
Rules for Subtraction using r’s complement:
Subtraction can be done by adding the minuend X to the r’s complement of the
subtrahend Y. Inspect the result obtained for an end carry.
7
Example[1]
Subtract using 10’s complement.
(a) X-Y (b)Y-X
Given
X = 72532 and Y= 3250
Ans :(a)
Step 1: Make the number of digits in both numbers equal
X = 72532 and Y= 03250
Step 2 :
72532
+ 96750 ( 10’s complement of Y)
1 69282
8
■ (b) Subtract 3250 – 72532
9
Example[1]
Given X= 1010100 and Y= 1000011, perform the subtraction (a) X-Y (b) Y-X using
2’s complement(a)
Ans : (a)
2’s complement for Y is 0111101
So 1010100
+ 0111101
_____________
10010001
10
(b) Y= 1000011 X= 1010100
Ans :
1000011
+ 0101100
_____________
1101111
No end carry, Take 2’s complement of answer and put –ve sign.
11
Subtraction using (r-1)’s complement[2]
Rules for Subtraction using (r-1)’s complement:
Subtraction can be done by adding the minuend X to the (r-1)’s complement
of the subtrahend Y. Inspect the result obtained for an end carry.
12
Example[1]
Subtract using 9’s complement.
(a) X-Y (b)Y-X
Given
X = 72532 and Y= 3250
Ans :(a)
Step 1: Make the number of digits in both numbers equal
X = 72532 and Y= 03250
Step 2 :
72532
+ 96749 ( 9’s complement of Y)
1 69281
+1 end-around carry
69282
Answer : 69282
13
(b) Subtract 3250 – 72532
X=03250 Y=72532
9’s complement for Y is 27467
So 03250
+ 27467
_____________
30717 (no carry)
14
Codes[1,2,3]
■ Code is a systematic arrangement of symbols in a specific manner to perform
operations.
■ Various codes are used to represent data which may be numeric, alphabets or
special characters.
■ Computer and other digital circuits process data in the binary formats.
■ A user must be very careful about the code being used while interpreting
information available in the binary format.
■ Codes are mainly divided into two types :
(i) Weighted Code – Codes in which each bit of the number has fixed
weight. Example BCD Code
(ii) Non weighted Codes- Codes in which no fixed weight is attached to the bits.
Example Gray Code, Excess 3 code.
15
Example
16
Binary Codes[1]
True = 1, False = 0
On = 1 , off = 0
■Two Binary digits (two bits) can take on values of 00, 01, 10, 11.
■Three Binary digits (three bits) can take on values of 000, 001, 010, 011, 100, 101,
110, 111. We can represent 8 values
different values)N bits can take on unsigned decimal values from 0 to 2 N-1.
17
BCD Codes[2]
■ In these codes, decimal digit 0 through 9 are represented by their
natural binary equivalents using four bits and each decimal digit of a
decimal number is represented by this four bits code individually.
■ Only 10 of the 16 possible 4 –bit binary code groups are used, rest of
the 6 code groups are unused(1010, 1011,1100,1101,1110,1111).
18
Example
Convert following numbers into BCD Code.
■(23) is represented by (0010 0011)BCD
10
19
References
[1] M. Morris Mano and Michael D. Ciletti, “Digital Design with an Introduction to the
Verilog HDL,” 5th Edition, Pearson Education,2013.
[2] Reshu Gupta, Amit Gupta ,Atul Kumar Sharma “ Switching Theory(Digital
Electronics)”, Tech India Publication Series, Satya Prakashan, New Delhi.
[3] R. P. Jain, “Modern Digital Electronics,” 4th Edition, Tata McGraw-Hill Education,
2009.
20