Professional Documents
Culture Documents
10 Subtraction
10 Subtraction
Subtraction (lvk)
0 1101 = +1310
1 1101 = -1310
01002 = 410
0 0100 = +410
1 0100 = -410
Subtraction (lvk)
+3
+ -6
-2
7
4
6
9
7
8
because
Subtraction (lvk)
5
6
3
2
13
4
7
6
17
7
9
8
0 1101 = +1310
1 0010 = -1310
01002 = 410
0 0100 = +410
1 1011 = -410
Subtraction (lvk)
1 = 0, and 1 - 1 = 0
Subtraction (lvk)
0011
+
0010
0 0101
(+7)
+ (-4)
0101
+
0
0101
(+3)
(+3)
+ (+2)
(+5)
Subtraction (lvk)
Twos complement
0 1101 = +1310
1 0010 = -1310
1 0011 = -1310
01002 = 410
0 0100 = +410
1 1011 = -410
1 1100 = -410
Subtraction (lvk)
1 00000
- 0 0 1 0 0 (+410)
1 1 1 0 0 (-410)
You can complement all of the bits to the left of the rightmost 1.
01101
= +1310
10011
= -1310
00100
= +410
11100
= -410
Often, people talk about taking the twos complement of a number. This is a
confusing phrase, but it usually means to negate some number thats already in
twos complement format.
Subtraction (lvk)
Subtraction (lvk)
To further convince you that this works, lets try adding two negative
numbers1101 + 1110, or (-3) + (-2) in decimal.
Adding the numbers gives 11011:
1 1 01
+ 1110
1 1 01 1
Dropping the carry out (1) leaves us with the answer, 1011 (-5).
Subtraction (lvk)
10
Subtraction (lvk)
11
Decimal
S.M.
1s comp.
2s comp.
7
6
5
4
3
2
1
0
-0
-1
-2
-3
-4
-5
-6
-7
-8
0111
0110
0101
0100
0011
0010
0001
0000
1000
1001
1010
1011
1100
1101
1110
1111
0111
0110
0101
0100
0011
0010
0001
0000
1111
1110
1101
1100
1011
1010
1001
1000
0111
0110
0101
0100
0011
0010
0001
0000
1111
1110
1101
1100
1011
1010
1001
1000
Subtraction (lvk)
12
Smallest
Largest
Unsigned
Signed
Magnitude
Ones
complement
Twos
complement
0000 (0)
1111 (15)
1111 (-7)
0111 (+7)
1000 (-7)
0111 (+7)
1000 (-8)
0111 (+7)
Signed
Ones
Twos
In general, withUnsigned
n-bit numbers
including thecomplement
sign, the ranges
are:
Magnitude
complement
Smallest
Largest
0
2n-1
-(2n-1-1)
+(2n-1-1)
Subtraction (lvk)
-(2n-1-1)
+(2n-1-1)
-2n-1
+(2n-1-1)
13
Subtraction (lvk)
14
Example solution
Since the sign bit is 1, this is a negative number. The easiest way to
find the magnitude is to convert it to a positive number.
(a) signed magnitude format
The most important point here is that a binary number has different
meanings depending on which representation is assumed.
Subtraction (lvk)
15
2s Complement
16
Subtraction
2s Complement
17
Subtraction
(9)
(10)
(19)
Subtraction (lvk)
18
Signed overflow
With twos complement and a 4-bit adder, for example, the largest
representable decimal number is +7, and the smallest is -8.
What if you try to compute 4 + 5, or (-4) + (-5)?
01 00
+ 01 01
01 001
(+4)
(+5)
(-7)
1 1 00
+ 1 01 1
1 01 1 1
(-4)
(-5)
(+7)
Subtraction (lvk)
19
The easiest way to detect signed overflow is to look at all the sign bits.
01 00
+ 01 01
01 001
(+4)
(+5)
(-7)
1 1 00
+ 1 01 1
1 01 1 1
(-4)
(-5)
(+7)
Subtraction (lvk)
20
Sign extension
21
hand.
0111 in twos complement is actually 00000111
1011 in twos complement is actually 11111011
1011 + 1100 = 10111.
This is overflow. But can you see it? Look again:
11111011 + 11111100 = 11110111
The sign of the result (1) is different than bit 3 (0).
We cannot represent the result in 4 bits
The sign of the result is extended to infinity
Check this every time in your answers and you will always see the
precision of the result (number of significant bits) very clearly.
Sign-extend inputs to infinity
Do the math on an infinite number of bits (conceptually)
Truncate the result to the desired output type
Subtraction (lvk)
22
23
Subtraction (lvk)
24
25
Small differences
The only differences between the adder and subtractor circuits are:
The subtractor has to negate B3 B2 B1 B0.
The subtractor sets the initial carry in to 1, instead of 0.
Its not too hard to make one circuit that does both addition and subtraction.
Subtraction (lvk)
26
An adder-subtractor circuit
X 1 = X
Subtraction (lvk)
27
Subtraction summary
Subtraction (lvk)
28
A: 101001
B: 010110
C: 101010
D: 111001
29
Subtraction
A: 10011110
B: 11100010
C: 00011110
D: 11100001
30
Subtraction