Computer Science 161.01

Professor Valente

 

Friday September 6th

 

READING:  Section 3.2, pages 44-49.

 

How Can Negative Numbers Be Represented?

 

We’re about to learn three different schemes for representing negative integers as well as zero and positive integers. 

What we’ll find in each of these is that, with 8 bits, we’ll use roughly half of the 256 patterns for negative integers.

Those patterns will typically start with a 1 for the leftmost bit.

The other half (in particular, the patterns that begin on the left with 0) will represent 0 and the positive integers.

 

Method 1:  A Natural(?) Way to Represent Negatives:  The “Sign and Magnitude” Method

 

In decimal, when I write -72 as opposed to +72, I realize that I’m writing the numbers much the same way.

Each uses the same digits 7 and 2, but the sign in front indicates negative or positive. 

In other words, one symbol (+ or -) is used for the sign and the other symbols (7 and 2) for the “magnitude”.

(In mathematics, the “magnitude” is the size of the number, or the number’s absolute value if you prefer.

It gives us the distance that the number is from the number line origin).

 

Let’s carry this idea forward to binary. 

Unfortunately I can only use symbols 0 and 1 as we have seen throughout this course.

But the leftmost symbol can once again indicate the sign:  simply use 0 for positive and 1 for negative!

 

We already know from studying unsigned integers that 72 is represented by 01001000 since 72 = 64 + 8.

In the sign magnitude scheme, this will still represent 72, but -72 will be represented by 11001000.

The left 1 bit indicates that the number represented is negative.  The remaining 7 bits say seventy-two!

 

Exercises:  Represent -7, +23, -46, -1, and +92 using the sign and magnitude method.

Try these and other examples using this familiar site. 

Be sure to select “sign and magnitude” up top and choose the right button for BinaryßDecimal.

 

Exercises:  Convert 01011011 to a decimal integer using the sign and magnitude method.

                   Do the same for 11101010.

Solutions:  01011011 is a positive integer and the remaining 7 bits represent 91.  So +91.

                   11101010 is a negative integer and the remaining 7 bits represent 106.  So -106.

(in each of these I went left-to-right with 7 bits and doubled-or-doubled-and-added-1 accordingly)

 

Try these and other examples using this familiar site. 

Be sure to select “sign and magnitude” up top and choose the left button for BinaryàDecimal.

 

QUESTION:  What decimal integers can be represented by this method using 8 bits?

ANSWER:     Realize that the largest positive number is 01111111, which is +127.

At the other extreme, the largest (in magnitude) negative number is 11111111, which is -127.

That’s only 255 different numbers represented by 256 patterns… hmmm… somewhere 2 patterns

are representing the same number.   See if you can determine which!

 

 

Is The “Sign and Magnitude” Method a Useful One for Computers?

At first glance, the sign-and-magnitude seems like a very reasonable and easy way to represent both

positive and negative integers.  However, if we work with them some, we see that this method is flawed.

For one thing, there are two different patterns that represent zero:  00000000 and 10000000.  Not good.

 

But a more serious flaw exists:  computer arithmetic would just not come out right… now THAT’s a problem!!

For example, try adding the pattern for 4 to the pattern for -3.   You would hope to get the pattern for +1. 

You don’t, because column-by-column addition yields 00000100 + 10000011 = 10000111, which is -7.  Duh!

 

So we will explore other methods beginning with Monday’s notes.