D75P 34R – HNC Computer Architecture
Download
Report
Transcript D75P 34R – HNC Computer Architecture
D75P 34R - HNC Computer
Architecture
Week 4
Signed Integers
© C Nyssen/Aberdeen College 2004
All images © C Nyssen /Aberdeen College unless otherwise stated
Prepared 06/10/04
Up until now, we have been looking at how integer
numbers are stored in binary.
But all the numbers we have used so far have been
unsigned - we have just been assuming, so far, that they
are all positive numbers!
ON -
-1
or
OFF -
-0
If computers can only store equivalents of 1 and 0, how
does it cope with + and - signs?
To begin, let’s recap on how simple integers are stored.
7
2
4
2
2
2
2
2
128 64
32
16
8
4
2
1
1
1
1
1
1
1
1
2
6
2
1
5
3
2
1
0
The above represents an 8-bit byte. If all the bits are set to
1, the integer has a decimal value of 255.
If all the bits are set to 0, that value would be zero.
So we can say that this byte can hold a range of values
from 0 - 255. There are 256 different possible values
within that byte.
Let’s look at the same byte, but use it differently.
6
2
4
2
2
2
2
2
64
32
16
8
4
2
1
1
1
1
1
1
1
1
sign
2
+ or -
5
3
2
1
0
The same 8-bit byte is used to hold the integer, but the
leftmost bit - the most significant bit - has been isolated.
In a moment, we will use that bit to determine whether the
rest of the number is positive or negative.
But as we are now only using 7 bits to store the actual
number, the range will only be from 0 to 127.
Let’s look at the first bit in more detail...
sign
26
25
24
23
22
21
20
+ or -
64
32
16
8
4
2
1
1
1
1
1
1
1
1
This particular bit is sometimes called a sign bit or sign
flag.
When it is set to 0, it means that the rest of the number is
positive.
When set to 1, it means that the rest of the number is
negative.
Be very, very careful not to confuse these values!
+ or -
64
32
16
8
4
2
1
0
1
1
1
1
1
1
1
The above number represents +127...
+ or -
64
32
16
8
4
2
1
1
1
1
1
1
1
1
1
…but this represents -127!
The range of numbers we can now represent using the
same 8 bits now runs from -127 to +127.
This method of representing negative numbers is called
the sign-and-magnitude method. It can be
represented by a number line like this….
It is very simple to implement from both a hardware and
software viewpoint.
It does, however, have one major flaw….
An 8-bit byte, with no sign flag, could hold number
values of 0 - 255 - a total of 256 separate numbers.
An 8-bit byte, with a sign flag - i.e. using 7 bits for the
number and one for the sign - could hold number
values of -127 to +127. A total of 255 separate
numbers!
So where did the missing value go?!
+ or -
64
32
16
8
4
2
1
0
0
0
0
0
0
0
0
What value does the above number represent?
What would happen if we changed the sign to a 1?
The reason we apparently “lost” a value is that 0 is
being counted twice; we have, in effect, a “positive”
and a “negative” zero!
Counting upwards would give us ...
… -3 -2 -1 -0 +0 +1 +2 +3 ...
If we were to count the items, it would give us a “false”
result - there are eight numbers shown above, but only
seven different values!
Whenever we count upwards or downwards and move
from a positive to a negative value, the count will always
be “one out”.
For this reason, sign-and-magnitude is not used much in
modern computer systems.
Another system, based on signand-magnitude, is used instead.
It is called the two’s
complement system, and works
on the complement, or inverse,
of the number.
Let’s look at an example of a 10’s complement first….
If a car speedometer has three figures, the maximum
number it can show will be 999. The complement of that is
1, because 1 added to 999 would reset the numbers to 000.
Likewise, the complement of 762 would be 238.
What would be the 10’s complement of 195?
Finding the 2’s complement of a binary number is actually
very easy. Let’s say we wanted to show -17 in binary, using
our original 8 bits.
It’s very important to stick to the stated number of bits!
+ or -
64
32
16
8
4
2
1
0
0
1
0
0
0
1
Firstly, we make up the number 17. Ignore the sign flag for
the moment.
Now comes the tricky part. As we have a negative number
- i.e. the reverse of a positive one - we are going to reverse
all the bits! So now it will look like this + or -
64
32
16
8
4
2
1
1
1
0
1
1
1
0
We still haven’t taken the problem of our double-zero into
account, though (remember we have crossed from the
positive to the negative side of the numberline). To allow
for this we add 1 to the rightmost - or least significant
bit...
1
1
0
1
1
1
1
+ or -
64
32
16
8
4
2
1
1
1
1
0
1
1
1
1
Finally, put the sign in to show that it’s negative.
You can convert negative binary numbers back to
decimal by doing the same thing in reverse...
+ or -
64
32
16
8
4
2
1
1
0
1
0
0
1
0
1
What’s this worth?
+ or -
64
32
16
8
4
2
1
1
0
1
0
0
1
0
1
This immediately
tells us we are
dealing with a
negative number.
So we know we
have to complement
the rest of it...
The reverse of 0100101 is
1011010
Add 1 to the last digit to get
1011011
Convert back to decimal to get
1+2+8+16+64 = 91
So the above value represents - 91!
What is the value of this number?
+ or -
64
32
16
8
4
2
1
0
0
1
1
0
1
0
0
This tells us we
have a positive
number. We can
leave the rest
alone...
..and just do a simple conversion
to decimal to get
4+16+32 = 52.
This number represents +52!
To see how the problem of the double-zero is resolved
using two’s complement, take the smallest possible
number in eight bits...
+ or -
64
32
16
8
4
2
1
1
1
1
1
1
1
1
1
…and the biggest one...
+ or -
64
32
16
8
4
2
1
0
1
1
1
1
1
1
1
What are the values of these two numbers?
You will now find that our range of numbers available
over the eight bits runs from -128 to +127!
We are back to our 256 possible combinations!
Easy binary subtraction with
2’s complement.
Last week we were struggling
with binary subtractions. It is
a lot easier using 2’s
complement and addition!
Suppose we wanted to subtract 16 from 49…
We could write and calculate this as 49 - 16.
Or we could write and calculate it as 49 + (-16), which
would give us exactly the same result!
All we are doing is working out the complement of 16, and
adding it instead of subtracting.
Here’s our +49...
0
0
1
1
0
0
0
1
0
1
0
0
0
0
0
1
1
1
1
0
0
0
Here’s +16...
0
Reverse all the digits...
1
1
And add 1 to the LSB to get -16...
1
1
1
1
0
0
0
1
1
0
0
0
1
1
1
1
1
0
0
0
0
0
1
Add the +49 and the -16 to get this...
0
0
1
0
0
0
Convert back to decimal to check….
…and the answer is indeed +33!
Summary.
Negative numbers can be shown using either sign-andmagnitude or two’s complement methods.
Both use the most significant bit to show the sign.
0 means positive, 1 means negative!
Sign and Magnitude can give inaccurate results, as the
zero value may be counted in twice.
To find the two’s complement of a number, reverse all the
digits then add one to the least significant bit.
Binary subtraction can be accomplished by adding the
two’s complement of the original number to be
subtracted.