EE2022 – Lecture 3 Signed Numbers And Basic Arithmetic

advertisement
EE2022 – Lecture 3
Signed Numbers
And
Basic Arithmetic
Signed Binary Numbers
Clearly there is a need to find some way of
representing both positive and negative numbers
using a binary representation.
There are two ways in which this is commonly
done:
Sign-Magnitude Representation
Two’s Complement Representation
Multiple Interpretations
The confusion lies in multiple interpretations
of a number that “looks” the same:
110102 = 2610 – Unsigned
110102 = -1010 – Sign-Magnitude
110102 = -610 – Two’s Complement
Clearly there are significantly different
interpretations, even though it’s the “same”
number!
Signed Magnitude
Signed-magnitude representations are the
easiest interpretations to learn.
The first bit is always the sign bit:
MSB=0 – The number is positive
MSB=1 – The number is negative
So, an 8-bit number can represent values
(assuming integers) from -127 to +127.
More Formally
So, an 8-bit signed-magnitude number can be
thought of as:
S D7 D6 D5 D4 D3 D2 D1 D0
sign
magnitude
This works fine, but there are two problems:
“positive” and “negative” zero.
Special handling of the sign bit.
Two’s Complement Numbers
Signed-magnitude numbers are pretty easy to
understand because they are rather intuitive.
However, they can lead to complications in
design because of special conditions.
Two’s-Complement numbers are a little
more tricky to understand, but they can
dramatically simplify design.
The Two’s Complement
A two’s-complement is created by taking
the complement of a number and adding
one to it. For example:
01101 = 1310 – Original Number
10010
– 1’s Complement (inversion)
10011 = -1310 – 2’s Complement of 1310
Interpreting 2’s Complement
Numbers
One rule is the same as sign-magnitude:
The first bit is the sign bit
“0” means positive
“1” means negative
“Converting” between binary and decimal:
If the msb = 0, just convert as normal.
If the msb = 1, take the 2’s complement, then
convert and add a minus sign to the result.
Some Examples
Given the following 2’s complement
numbers, convert them to decimal:
01101 = ?
11010 = ?
11111 = ?
00000 = ?
10000 = ?
Properties of 2’s Complement
There is only one representation for zero.
Adding a number and it’s complement yields
a result of zero:
01101 + 10011 = 00000 (and a carry out)
There are more negative numbers than
positive:
8-bits represents -128 to +127.
Addition and subtraction is simplified.
Adding and Subtracting
Consider the following examples:
810 + 210 = 010002 + 000102 = 010102 = 1010
-810 + 210 = 110002 + 000102 = 110102 = -610
810 - 210 = 010002 + 111102 = 001102 = 610
-810 + -210 = 110002 + 111102 = 101102 = -1010
Confusion Alert!!!!
There are some important subtleties in the
following statements:
“Take the two’s complement of xxxx.”
“Assuming a system uses 2’s complement
numbers, subtract xxxx from yyyy.”
“Assuming a system uses 2’s complement
numbers, what is the decimal value of xxxx +
yyyy?”
Download