Lect 03
Lect 03
°2’s Complement
1
How To Represent Signed Numbers
2
Complement
° It has already been studied that
° subtracting one number from another is the same as making
one number negative and just adding them.
° We know how to create negative numbers in the binary
number system.
° How to perform 2’s complement process.
° How the 2’s complement process can be use to add (and
subtract) binary numbers.
° Digital electronics requires frequent addition and subtraction
of numbers. You know how to design an adder, but what about
a subtracter?
° A subtracter is not needed with the 2’s complement process.
The 2’s complement process allows you to easily convert a
positive number into its negative equivalent.
° Since subtracting one number from another is the same as
making one number negative and adding, the need for a
subtracter circuit has been eliminated. 3
3-Digit Decimal
A bicycle odometer with only
three digits is an example of a
fixed-length decimal number 999
forward (+)
system. 998
997
The problem is that without a
negative sign, you cannot tell a
+998 from a -2 (also a 998). Did 001
you ride forward for 998 miles 000
or backward for 2 miles? 999
998
Note: Car odometers do not work
this way.
002
backward (-)
001
4
Negative Decimal
5
3-Digit Decimal (Examples)
3 003 6 006
+ 2 + 002 + (-3) + 997
5 005 3 1003
Disregard
Overflow
7
Complement
8
10’s Complement Examples
Example #1
-003
Complement Digits
996
+1 Add 1
997
Example #2
-214
Complement Digits
785
+1 Add 1
786 9
One’s Complement Representation
10
Two’s Complement Representation
11
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 =01100101
[N] =10011011
12
Finite Number Representation
13
1’s Complement Addition
° Using 1’s complement numbers, adding numbers
is easy.
° For example, suppose we wish to add +(1100)2
and +(0001)2.
° Let’s compute (12)10 + (1)10.
• (12)10 = +(1100)2 = 011002 in 1’s comp.
• (1)10 = +(0001)2 = 000012 in 1’s comp.
0 1 1 0 0
Add + 0 0 0 0 1
Step 1: Add binary numbers --------------
Step 2: Add carry to low-order bit 0 0 1 1 0 1
Add carry 0
--------------
Final
Result
0 1 1 0 1
14
1’s Complement Subtraction
° Using 1’s complement numbers, subtracting
numbers is also easy.
° For example, suppose we wish to subtract
+(0001)2 from +(1100)2.
0 1 1 0 0
° Let’s compute (12)10 - (1)10.
- 0 0 0 0 1
• (12)10 = +(1100)2 = 011002 in 1’s comp.
--------------
• (-1)10 = -(0001)2 = 111102 in 1’s comp.
1’s comp
0 1 1 0 0
Step 1: Take 1’s complement of 2nd operand Add + 1 1 1 1 0
Step 2: Add binary numbers
Step 3: Add carry to low order bit
--------------
1 0 1 0 1 0
Add carry 1
Final
--------------
Result 0 1 0 1 1
15
2’s Complement Addition
° Using 2’s complement numbers, adding numbers
is easy.
° For example, suppose we wish to add +(1100)2
and +(0001)2.
° Let’s compute (12)10 + (1)10.
• (12)10 = +(1100)2 = 011002 in 2’s comp.
• (1)10 = +(0001)2 = 000012 in 2’s comp.
0 1 1 0 0
Add + 0 0 0 0 1
Step 1: Add binary numbers --------------
Step 2: Ignore carry bit Final 0 0 1 1 0 1
Result
Ignore
16
2’s Complement Subtraction
° Using 2’s complement numbers, follow steps for
subtraction
° For example, suppose we wish to subtract
+(0001)2 from +(1100)2.
0 1 1 0 0
° Let’s compute (12)10 - (1)10.
- 0 0 0 0 1
• (12)10 = +(1100)2 = 011002 in 2’s comp.
--------------
• (-1)10 = -(0001)2 = 111112 in 2’s comp.
2’s comp
0 1 1 0 0
Step 1: Take 2’s complement of 2nd operand Add + 1 1 1 1 1
Step 2: Add binary numbers
Step 3: Ignore carry bit
--------------
Final
Result 1 0 1 0 1 1
Ignore
Carry
17
2’s Complement Subtraction: Example #2
18
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…
0 0 1 0 1
+ 1 0 1 0 0
--------------
1 1 0 0 1
° 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.
19
1’s Complement
20
2’s Complement
21
Overflow
° Overflow conditions
° Add two positive numbers to get a negative number
° Add two negative numbers to get a positive number
22
Two’s Complement Number System
-8 -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7
0
- 1 1
0000
1111 0001 2
- 2
1110 0010
- 3 3
1101 0011
- 4 4
1100 0100
5
1011 0101
- 5
1010 0110
- 6 6
1001 1000 0111
- 7 - 8 7
23
Summary
24
Summary
25