|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Integers: signed
magnitude |
|
|
|
|
Signed magnitude |
|
|
|
|
Reserve one particular
bit for the sign. |
|
|
|
Sign bit: If msb is 0,
then positive value, if msb is 1, then negative value. |
|
|
Converting base 10 to
N-bit signed magnitude (SM): |
|
|
|
1. Ignoring sign, convert
base 10 value to binary. |
|
|
|
2. If less than (N-1)
bits, pad rest of bits to (N-1) with 0. |
|
|
|
3. If negative, set msb
to 1. |
|
|
|
Converting N-bit SM to
base 10: |
|
|
|
|
1. Convert lower (N-1)
bits to base 10. |
|
|
|
2. If msb is 1, put minus
sign in front of number. |
|
|
Example: |
|
|
|
|
Convert 3 in base 10 to
4-bit SM: |
|
|
|
|
binary value: 11 |
|
|
|
|
using 3 bits: 011 |
|
|
|
|
with sign bit: 0011 |
|
|
|
|
Convert -3 in base 10 to
4-bit SM: |
|
|
|
|
binary value: 11 |
|
|
|
|
using 3 bits: 011 |
|
|
|
|
with sign bit: 1011 |
|
|
|
|
What about 15? |
|
|
|
|
|
binary value: 1111 |
|
|
|
|
However, this is already
4 bits, and the sign bit needs to be 0 for positive value. |
|
Range of values for SM |
|
|
|
|
|
How many possible values
(i.e., representations)? |
|
|
|
2N
for N bits |
|
|
|
|
|
Maximum value |
|
|
|
|
|
2N-1 - 1 for (N-1) bits |
|
|
|
|
Minimum value |
|
|
|
|
|
-(2N-1 - 1) for (N-1) bits |
|
|
|
Total values, including
0: |
|
|
|
|
2 *(2N-1 -1) + 1 = 2N - 1 |
|
|
|
What happened to the
extra value? |
|
|
|
There are actually 2
zeroes! |
|
|
|
positive zero: N 0's |
|
|
|
|
negative zero: 1 followed
by (N-1) 0's |
|
|
So, total number of representations is 2N, but total number of values is 1 less. |
|
|
Disadvantage of SM: 2
zeroes adds complexity to hardware. |
|
|
Addition |
|
|
|
|
It would be nice if we
could use the same hardware for signed or unsigned addition. |
|
|
However, that is not the
case for signed magnitude. |
|
|
|
Consider adding -1 and -1
in 4-bit SM |
|
|
|
1001 + 1001 = 0010 in 4
bits, since the carry from the msb is lost. |
|
|
This gives a result of
+2, which is incorrect. |
|
|
One
way around this is to ignore the sign bit in doing the addition, |
|
|
then use the sign bit for
the result. |
|
|
However, that doesn't
work if the sign bits are different. |
|
|
The sign bit of the
result will depend on the relative magnitudes. |
|
|
The magnitude of the
result will be the difference in the magnitudes. |
|
|
For example, (-5) + 3 is
-2, but 5 + (-3) is +2. |
|
|
Negation |
|
|
Negating a value means
giving it the opposite sign. |
|
|
This is easy with SM,
since we simply flip the sign bit. |
|
|
negative value: sign bit
1 --> 0 |
|
|
positive value: sign bit
0 --> 1 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|