Negative Numbers I

Download Report

Transcript Negative Numbers I

Two’s Complement
Two’s Complement

A system used to represent a negative number in
binary

Positive numbers start with a 0
Negative numbers start with a 1

On a cars Mileometer if you went
backwards this would happen:
0
9
0
9
0
9
0
9
0
9
0
9
0
9
0
1
2
3
9
8
7
6
Two’s Complement is like a
Speedometer…….
128 64 32 16 8
0
0
0
0
Positive MSB
(Most Significant
Bit) indicated by 0
0
4 2 1
0
1
1
= 3
Two’s Complement is like a
Speedometer…….
128 64 32 16 8
0
0
0
Positive MSB
indicated by 0
0
0
4 2 1
0
1
0
= 2
Two’s Complement is like a
Speedometer…….
128 64 32 16 8
0
0
0
Positive MSB
indicated by 0
0
0
4 2 1
0
0
1
= 1
Two’s Complement is like a
Speedometer…….
128 64 32 16 8
0
0
0
Positive MSB
indicated by 0
0
0
4 2 1
0
0
0
= 0
Two’s Complement is like a
Speedometer…….
-128 64 32 16 8
1
1
1
Negative MSB
indicated by 1
1
1
4 2 1
1
1
1
= -1
Two’s Complement is like a
Speedometer…….
-128 64 32 16 8
1
1
1
Negative MSB
indicated by 1
1
1
4 2 1
1
1
0
= -2
Two’s Complement is like a
Speedometer…….
-128 64 32 16 8
1
1
1
Negative MSB
indicated by 1
1
1
4 2 1
1
0
1
= -3
How can you convert a negative two’s
complement number to denary?
Example: Convert two’s complement 10010110
-128
1
64
0
32 16
0 1
This would be:
-128 + 16 + 4 + 2 = -106
8
0
4
1
2
1
1
0
Example 2
Convert two’s complement 11011011
-128
1
64
1
32 16
0 1
8
1
4
0
2
1
This would be:
-128 + 64 + 16 + 8 + 2 + 1 = -37
1
1
Activity
Convert the following numbers to denary:
1.
2.
3.
4.
Two’s complement 00110011
Two’s complement 11001100
Two’s complement 10111101
Binary (no +\- MSB at the start) 11110101
How can you convert a negative denary
number into two’s complement?
Step 1
pretend negative denary number is positive and
convert it into binary e.g. -52 becomes 52
128
0
64
0
32 16
1 1
8
0
4
1
2
0
1
0
How can you convert a negative denary
number into two’s complement?
Step 2
Starting from the right, leave all digits alone, up
to and including the first 1, then change all the
other digits around (0 to 1) or (1 to 0)
126 64 32 16
8 4
2
1
0 0
1 1
0 1 0
0
1
1
0
0
1
1
0
0
Alternative Method:
Create positive binary number as before:
128
64
32 16
8 4
0
0
1 1
0 1
Next change all 1’s and 0’s around:
128
64
32 16
8
4
1
1
0 0
1
0
Then add 1 to the number:
128
64
32 16
8
4
1
1
0 0
1
0
+ 0
0
0 0
0
0
= 1
1
0 0
1
1
2
0
1
0
2
1
1
1
2
1
0
0
1
1
1
0
Addition in Binary
128
64
32
16
8
4
2
1
0 1 0 0 1 1 0 1
+0 0 1 1 1 0 1 1
Denary
77
59
1 0 0 0 1 0 0 0
1
1
1
1
1
1
136
Addition Exercise


Add 47 to 159 in binary
Add 12 to 47 in binary
Subtraction in Binary

Rather than subtracting, convert the number
that is to be subtracted into a negative number
then add them together as before
Subtraction in Binary
e.g.
15-12
12 = 00001100
-12 = 11110100
15 = 00001111
00001111
+ 11110100
(1) 00000011
The carry of 1 is ignored
Subtraction Exercises
1.
2.
Subtract 23 from 123 in binary
Subtract 14 from 32 in binary
Rules:





Positive numbers have a 0 as the MSB
Negative numbers have a 1 as the MSB
Even numbers have 0 as the LSB (Least
Significant Bit)
Odd numbers have 1 as the LSB
-1 is always represented by a 1 in every bit
regardless of number of bits