Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "D75P 34R - HNC Computer Architecture Week 4 Signed Integers © C Nyssen/Aberdeen College 2004 All images © C Nyssen /Aberdeen College unless otherwise."— Presentation transcript:

1

2 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

3 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 -- 1or OFF- - 0 If computers can only store equivalents of 1 and 0, how does it cope with + and - signs?

4 To begin, let’s recap on how simple integers are stored. 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.

5 Let’s look at the same byte, but use it differently. 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.

6 Let’s look at the first bit in more detail... 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!

7 The above number represents +127... …but this represents -127! The range of numbers we can now represent using the same 8 bits now runs from -127 to +127.

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

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

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

11 Counting upwards would give us... … -3 -2 -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.

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

13 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! Firstly, we make up the number 17. Ignore the sign flag for the moment.

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

15 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... What’s this worth?

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

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

18 To see how the problem of the double-zero is resolved using two’s complement, take the smallest possible number in eight bits... …and the biggest one... What are the values of these two numbers?

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

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

21 Here’s our +49... Here’s +16... Reverse all the digits... And add 1 to the LSB to get -16...

22 Add the +49 and the -16 to get this... Convert back to decimal to check…. …and the answer is indeed +33!

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


Download ppt "D75P 34R - HNC Computer Architecture Week 4 Signed Integers © C Nyssen/Aberdeen College 2004 All images © C Nyssen /Aberdeen College unless otherwise."

Similar presentations


Ads by Google