d75p 34r - hnc computer architecture week 4 signed integers © c nyssen/aberdeen college 2004 all...

22
D75P 34R - HNC Computer Architecture Week 4 Signed Integers Nyssen/Aberdeen College 2004 images © C Nyssen /Aberdeen College unless otherwise stated ared 06/10/04

Upload: abel-burke

Post on 19-Jan-2016

218 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: D75P 34R - HNC Computer Architecture Week 4 Signed Integers © C Nyssen/Aberdeen College 2004 All images © C Nyssen /Aberdeen College unless otherwise

D75P 34R - HNC Computer Architecture

Week 4Signed Integers

© C Nyssen/Aberdeen College 2004All images © C Nyssen /Aberdeen College unless otherwise statedPrepared 06/10/04

Page 2: D75P 34R - HNC Computer Architecture Week 4 Signed Integers © C Nyssen/Aberdeen College 2004 All images © C Nyssen /Aberdeen College unless otherwise

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?

Page 3: D75P 34R - HNC Computer Architecture Week 4 Signed Integers © C Nyssen/Aberdeen College 2004 All images © C Nyssen /Aberdeen College unless otherwise

To begin, let’s recap on how simple integers are stored.

27 26 25 24 23 22 21 20

128 64 32 16 8 4 2 1

1 1 1 1 1 1 1 1

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.

Page 4: D75P 34R - HNC Computer Architecture Week 4 Signed Integers © C Nyssen/Aberdeen College 2004 All images © C Nyssen /Aberdeen College unless otherwise

Let’s look at the same byte, but use it differently.

sign 26 25 24 23 22 21 20

+ or - 64 32 16 8 4 2 1

1 1 1 1 1 1 1

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.

Page 5: D75P 34R - HNC Computer Architecture Week 4 Signed Integers © C Nyssen/Aberdeen College 2004 All images © C Nyssen /Aberdeen College unless otherwise

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!

Page 6: D75P 34R - HNC Computer Architecture Week 4 Signed Integers © C Nyssen/Aberdeen College 2004 All images © C Nyssen /Aberdeen College unless otherwise

+ 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.

Page 7: D75P 34R - HNC Computer Architecture Week 4 Signed Integers © C Nyssen/Aberdeen College 2004 All images © C Nyssen /Aberdeen College unless otherwise

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….

1 byte, unsigned-255 -127 0 +127 +255

1 byte including sign

Page 8: D75P 34R - HNC Computer Architecture Week 4 Signed Integers © C Nyssen/Aberdeen College 2004 All images © C Nyssen /Aberdeen College unless otherwise

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?!

Page 9: D75P 34R - HNC Computer Architecture Week 4 Signed Integers © C Nyssen/Aberdeen College 2004 All images © C Nyssen /Aberdeen College unless otherwise

+ 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!

Page 10: D75P 34R - HNC Computer Architecture Week 4 Signed Integers © C Nyssen/Aberdeen College 2004 All images © C Nyssen /Aberdeen College unless otherwise

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.

Page 11: D75P 34R - HNC Computer Architecture Week 4 Signed Integers © C Nyssen/Aberdeen College 2004 All images © C Nyssen /Aberdeen College unless otherwise

Another system, based on sign-and-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?

Page 12: D75P 34R - HNC Computer Architecture Week 4 Signed Integers © C Nyssen/Aberdeen College 2004 All images © C Nyssen /Aberdeen College unless otherwise

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.

Page 13: D75P 34R - HNC Computer Architecture Week 4 Signed Integers © C Nyssen/Aberdeen College 2004 All images © C Nyssen /Aberdeen College unless otherwise

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

Page 14: D75P 34R - HNC Computer Architecture Week 4 Signed Integers © C Nyssen/Aberdeen College 2004 All images © C Nyssen /Aberdeen College unless otherwise

+ 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?

Page 15: D75P 34R - HNC Computer Architecture Week 4 Signed Integers © C Nyssen/Aberdeen College 2004 All images © C Nyssen /Aberdeen College unless otherwise

+ 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 is1011010Add 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!

Page 16: D75P 34R - HNC Computer Architecture Week 4 Signed Integers © C Nyssen/Aberdeen College 2004 All images © C Nyssen /Aberdeen College unless otherwise

+ or - 64 32 16 8 4 2 1

0 0 1 1 0 1 0 0

What is the value of this number?

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!

Page 17: D75P 34R - HNC Computer Architecture Week 4 Signed Integers © C Nyssen/Aberdeen College 2004 All images © C Nyssen /Aberdeen College unless otherwise

+ or - 64 32 16 8 4 2 1

1 1 1 1 1 1 1 1

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

0 1 1 1 1 1 1 1

…and the biggest one...

What are the values of these two numbers?

Page 18: D75P 34R - HNC Computer Architecture Week 4 Signed Integers © C Nyssen/Aberdeen College 2004 All images © C Nyssen /Aberdeen College unless otherwise

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!

Page 19: D75P 34R - HNC Computer Architecture Week 4 Signed Integers © C Nyssen/Aberdeen College 2004 All images © C Nyssen /Aberdeen College unless otherwise

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.

Page 20: D75P 34R - HNC Computer Architecture Week 4 Signed Integers © C Nyssen/Aberdeen College 2004 All images © C Nyssen /Aberdeen College unless otherwise

0 0 1 1 0 0 0 1Here’s our +49...

0 0 1 0 0 0 0

Here’s +16...

Reverse all the digits...

1 1 0 1 1 1 1

1 1 1 1 0 0 0 0

And add 1 to the LSB to get -16...

Page 21: D75P 34R - HNC Computer Architecture Week 4 Signed Integers © C Nyssen/Aberdeen College 2004 All images © C Nyssen /Aberdeen College unless otherwise

0 0 1 1 0 0 0 1

1 1 1 1 0 0 0 0

Add the +49 and the -16 to get this...

0 0 1 0 0 0 0 1

Convert back to decimal to check….

…and the answer is indeed +33!

Page 22: D75P 34R - HNC Computer Architecture Week 4 Signed Integers © C Nyssen/Aberdeen College 2004 All images © C Nyssen /Aberdeen College unless otherwise

Summary.

Negative numbers can be shown using either sign-and-magnitude 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.