DLD03 Complement
-
Upload
shan-anwer -
Category
Documents
-
view
68 -
download
8
Transcript of DLD03 Complement
Digital Logic and Design
(aka DLD: EEE241)
Dr. Aamir M. Khan
PhD Embedded Systems
Lecture 3: N’s Complement
2
How To Represent Signed Numbers
• Plus and minus sign used for decimal numbers:
25 (or +25), -16, etc.
• For computers, desirable to represent everything as bits.
• Three types of signed binary number representations:
signed magnitude, 1’s complement, 2’s complement.
• In each case: left-most bit indicates sign: positive (0) or
negative (1).
3
Consider signed magnitude:
000011002 = 1210
Sign bit Magnitude
100011002 = -1210
Sign bit Magnitude
One’s Complement Representation
• The one’s complement of a binary number involves
inverting all bits.
• 1’s complement of 00110011 is 11001100
• 1’s complement of 10101010 is 01010101
• For an n bit number N the 1’s complement is (2n-1) – N.
• Called diminished radix complement by Mano since 1’s
complement for base (radix 2).
• To find negative of 1’s complement number, take the 1’s
complement.
4
000011002 = 1210 111100112 = -1210
Two’s Complement Representation
• The two’s complement of a binary number involves inverting
all bits and adding 1.
• 2’s comp of 00110011 is 11001101
• 2’s comp of 10101010 is 01010110
• For an n bit number N the 2’s complement is (2n) – N.
• Called radix complement by Mano since 2’s complement for
base (radix 2).
• To find negative of 2’s complement number, take the 2’s
complement.
5
000011002 = 1210 111101002 = -1210
Two’s Complement Shortcuts
• Algorithm 1 – Simply complement each bit and then add 1 to the
result.
• Finding the 2’s complement of (01100101)2 and of its 2’s complement…
N = 01100101 [N] = 10011011
10011010 01100100
+ 1 + 1
--------------- ---------------
10011011 01100101
• Algorithm 2 – Starting with the least significant bit, copy all of the
bits up to and including the first ‘1’ bit and then complementing the
remaining bits.
N = 0 1 1 0 0 1 0 1
[N] = 1 0 0 1 1 0 1 1
6
Finite Number Representation
• Machines that use 2’s complement arithmetic can
represent integers in the range
-2n-1 ≤ N ≤ 2n-1 – 1
where n is the number of bits available for representing
N. Note that
2n-1 – 1 = (011…11)2 and -2n-1 = (100…00)2
• For 2’s complement more negative numbers than
positive.
• For 1’s complement two representations for zero.
• For an n bit number in base (radix) r there are r n
different unsigned values.
(0, 1, …r n-1)
7
Signed Binary Numbers
8
1’s Complement Addition
• Using 1’s complement numbers, adding numbers is easy.
• For example, suppose we wish to add (12)10 + (1)10
• Binary code for 12 is (1100)2 and for 1 is (0001)2.
• But in signed number form, we have 0 at the MSB for +ve numbers.
• (12)10 = +(1100)2 = 011002 in 1’s comp.
• (1)10 = +(0001)2 = 000012 in 1’s comp.
9
Step 1: Add binary numbers Step 2: Add carry to low-order bit
0 1 1 0 0
+ 0 0 0 0 1
--------------
0 0 1 1 0 1
0
--------------
0 1 1 0 1
Add carry
Final Result
Add
1’s Complement Subtraction
• Using 1’s complement numbers, subtracting numbers is also easy.
• For example, suppose we wish to compute (12)10 - (1)10.
• Subtraction will be like: 12 + (-1)
• (12)10 = +(01100)2 = 011002 in 1’s comp.
• (-1)10 = - (00001)2 = 111102 in 1’s comp.
10
0 1 1 0 0
- 0 0 0 0 1
--------------
0 1 1 0 0
+ 1 1 1 1 0
--------------
1 0 1 0 1 0
1
--------------
0 1 0 1 1
Add carry
Final Result
Step 1: Take 1’s complement of 2nd operand Step 2: Add binary numbers Step 3: Add carry to low order bit
1’s comp
Add
2’s Complement Addition
• Using 2’s complement numbers, adding numbers is easy.
• For example, suppose we wish to add (12)10 + (1)10.
• As both numbers are +ve, their binary codes will have 0 at
MSB.
• (12)10 = +(1100)2 = 011002 in 2’s comp.
• (1)10 = +(0001)2 = 000012 in 2’s comp.
11
Step 1: Add binary numbers Step 2: Ignore carry bit (if any)
0 1 1 0 0
+ 0 0 0 0 1
--------------
0 1 1 0 1
Final Result
Add
2’s Complement Subtraction
• Using 2’s complement numbers, follow steps for subtraction
• For example, suppose we wish to compute (12)10 - (1)10.
• Subtraction will be performed as: 12 + (-1)
• Let’s
• (12)10 = + (01100)2 = 011002 in 2’s comp.
• (-1)10 = - (00001)2 = 111112 in 2’s comp.
12
0 1 1 0 0
- 0 0 0 0 1
--------------
0 1 1 0 0
+ 1 1 1 1 1
--------------
1 0 1 0 1 1
Final Result
Step 1: Take 2’s complement of 2nd operand Step 2: Add binary numbers Step 3: Ignore carry bit
2’s comp
Add
Ignore Carry
2’s Complement Subtraction: Example #2
• Let’s compute (13)10 - (5)10.
• (13)10 = + (1101)2 = (01101)2
• (-5)10 = - (0101)2 = (11011)2
• Adding these two 5-bit codes…
• Discarding the carry bit, the sign bit is seen to be zero,
indicating a correct result. Indeed,
(01000)2 = +(1000)2 = +(8)10.
13
0 1 1 0 1
+ 1 1 0 1 1
--------------
1 0 1 0 0 0
carry
2’s Complement Subtraction: Example #3
• Let’s compute (5)10 - (12)10.
• (-12)10 = -(1100)2 = (10100)2
• (5)10 = +(0101)2 = (00101)2
• Adding these two 5-bit codes…
• Here, there is no carry bit and the sign bit is 1. This
indicates a negative result, which is what we expect.
(11001)2 = -(7)10.
14
0 0 1 0 1
+ 1 0 1 0 0
--------------
1 1 0 0 1
Formal Definition of Complement
• Complements are used in digital computers for
simplifying subtraction operation.
• There are two types of Complements
• r’s Complement
• (r-1)’s Complement
where ‘r’ is the base
15
Complements
• In base-10:
• 10’s Complement
• 9’s Complement
• In base-2
• 2’s Complement
• 1’s Complement
16
Remember the rule: r’s complement is +1 (add 1) to the (r-1)’s complement.
10’s Complement
• Given a positive number ‘N’ in base ‘r’ with an integer
part of ‘n’ digits, then r’s complement is defined as
rn - N
• 10’s complement of (52520)₁₀ is
10⁵ - 52520
= 100000 – 52520 = 47480
• Similarly,
10’s complement of 134795 is 865205
17
10’s Complement
• 10’s complement of (0.3267)₁₀ = 10⁰ – 0.3267
= 1 - 0.3267
= 0.6733
• 10’s complement of (25.639)₁₀ = 10² - 25.639
= 74.361
18
0.3267 has no integer part => 100
2’s Complement
• 2’s Complement of (101100)₂ is (2⁶)₁₀ – (101100)₂
= (1000000 – 101100)₂
= 010100
• 2’s Complement of (0.0110)2 is (20)₁₀ – (0.0110)₂
= (1.0000 – 0.0110)₂
= 0.1010
• Rules for binary Subtraction
• 0 - 0 = 0
• 0 - 1 = 1, and borrow 1 from the next more significant bit
• 1 - 0 = 1
• 1 - 1 = 0
19
(26)10 = (64)10 = (1000000)2
(20)10 = (1)10 = (1)2
(r-1)’s Complements
• Given a positive number ‘N’ in base ‘r’ with an integer
part of ‘n’ digits and a fraction part of ‘m’ digits then (r-
1)’s complement is defined as
rn - r-m - N
• Need to accommodate the fraction part through ‘r-m’
• 9’s complement of (52520)₁₀ is (10⁵ – 10⁰ – 52520)
= 100000 – 1 – 52520
= 47479
20
Remember the rule: r’s complement is +1 unit (add 1) to the (r-1)’s complement.
9’s Complement
• 9’s complement of (0.3267)₁₀ is (10⁰ – 10⁻⁴ – 0.3267)
= 0.9999 – 0.3267
= 0.6732
• 9’s complement of (25.639)₁₀ is (10² – 10⁻³ – 25.639)
= 99.999 – 25.639
= 74.360
• Find the 9’s and 10’s complements of 314700.
• 9’s complement = 685299
• 10’s complement = 685300
• To find the 10’s complement of a decimal number leave all leading
zeros unchanged. Then subtract the first non-zero digit from 10 and
all the remaining digits from 9’s.
21
A version of Algorithm 2 on Slide 6, for binary numbers
1’s Complement
• 1’s complement of 110100101 is 001011010
which can be obtained by replacing each one by a zero and each
zero by one.
• 1’s Complement of (101100)2 is (26 - 1)10 – (101100)2
= (1000000 – 1 – 101100)2
= 010011
• 1’s Complement of (0.0110)2 is (20 – 2– 4)10 – (0.0110)2
= (0.1111 – 0.0110)2
= 0.1001
22
Remind it: 20 – 2–4 = rn – n-m
20 – 2–4 = (1-0.0625) = (0.9375)10 = (0.1111)2
Use method of Example 1.3
Exercise
• Find the 1’s and 2’s complements of the binary number
1101001101
• 1’s complement is 0010110010
• 2’s complement is 0010110011
• Find the 1’s and 2’s complements of 100010100
• 1’s complement is 011101011
• 2’s complement is 011101100
23
Subtraction with r’s Complements
• The subtraction of two positive number (M-N), both of
base-r may be done as follows.
1. Add the minuend M to the r’s complement of the
subtrahend N.
2. Inspect the result obtained above for an end carry:
a) If an end carry occurs, discard it and write the answer as it is
b) If an end carry does not occur, then take r’s complement of the
number obtained in step 1, and place a negative sign in front.
24
Exercise
• Using 10’s complement subtract 3250 from 72532
M = 72532
N = 03250
• 10’s complement of N = 96750
72532
+ 96750
= 169282
Answer = 69282
25
Exercise
• Subtract (3250 – 72532) using 10’s Complement.
M = 03250
N = 72532
• 10’s complement of N = 27468
03250
+ 27468
= 030718
26
Exercise
• Using 2’s complement subtract 1000100 from 1010100
M = 1010100
N = 1000100
• 2’s complement of N = 0111100
1010100
+ 0111100
= 10010000
Answer = 10000
27
Exercise
• Subtract (1000100 – 1010100) using 2’s Complement
M = 1000100
N = 1010100
• 2’s complement of N = 0101100
1000100
+ 0101100
= 01110000
28
Subtraction with (r-1)’s Complements
• The subtraction of two positive number (M-N), both of
base-r may be done as follows.
1. Add the minuend M to the (r-1)’s complement of the
subtrahend N.
2. Inspect the result obtained above for an end carry:
a) If an end carry occurs, add 1 to the number.
b) If an end carry does not occur, then take (r-1)’s complement of
the number obtained in step 1, and place a negative sign in
front.
29
Exercise
• Using 9’s complement subtract 3250 from 72532
M = 72532
N = 03250
• 9’s complement of N = 96749
72532
+ 96749
= 169281
• Add this end carry to the above
i.e. 69281 +1
Answer = 69282
30
Exercise
• Subtract (3250 – 72532) using 9’s Complement.
M = 03250
N = 72532
• 10’s complement of N = 27467
03250
+ 27467
= 030717
31
Exercise
• Using 1’s complement subtract 1000100 from 1010100
M = 1010100
N = 1000100
• 1’s complement of N = 0111011
1010100
+ 0111011
= 10001111
• Add this end carry to the above
i.e. 0001111 + 1
Answer = 10000
32
Exercise
• Subtract (1000100 – 1010100) using 1’s Complement
M = 1000100
N = 1010100
• 1’s complement of N = 0101011
1000100
+ 0101011
= 01101111
33
Sample Quiz
• Convert the following into Binary, Hexadecimal & Octal.
• 53.0286
• 106.274
• Write the 10’s Complement of 786.543 & 9’s
Complement of 12345
34