CS251 - Computer Organization
So we have positive integers and addition...
How can we handle negative numbers in binary?
What about doing it just like with decimal numbers?
10102 = 1010
-10102 = -1010
Are there any problems with that?
Sure, the - sign is not a 1 or a 0...
Can it be?
Sure:
0 -> Positive (no minus sign)
0 1010 2 = 10 10
1 -> Negative (minus sign)
1 1010 2 = -10 10
This is called Sign-Magnitude Representation
If 0010 0101 2 = 37 10 then how do I represent -37 using sign-magnitude representation?
Just flip the MSB. (In a computer where the bits are hardware we can't just add another bit so we use the MSB.)
1010 0101 2 = -37 10
In sign-magnitude representation flipping the MSB is equivalent to multiplying by -1 in base 10.
What is the base 10 value of 1001 0110 2
Do we have enough information to answer that question?
No, we need to know if it is in Unsigned Representation or Sign-Magnitude Representation!
In unsigned: 1001 0110 2 = 150 10
In sign-magnitude: 1001 0110 2 =
-1 * 0001 0110 2 =
-1 * (16 + 4 + 2) = -1 * 22 10 = -22
Is 1011 0110 2 a positive or negative sign-magnitude number?
The MSB = 1 therefore it is a negative sign-magnitude number.
What is its base 10 value?
1011 0110 2 = -1 * 0011 0110 2 = -1 * (32 + 16 + 4 + 2) = -1 * 54 = -54
Is 0101 0011 2 a positive or negative sign-magnitude number?
The MSB = 0 therefore it is a positive sign-magnitude number.
What is its value?
0101 0011 2 = 64 + 16 + 2 + 1 = 83
Addition with sign-magnitude numbers:
Can we add sign-magnitude numbers?
We've definitely got some problems with addition of sign-magnitude numbers.
In the second example there was a carry-out and we got the wrong answer.
In the third example there was no carry-out and we still got the wrong anwer.
Are either of these a case of Overflow?
No, because neither result requires more bits to represent than are available.
However, they illustrate a problem with the sign-magnitude representation.
Addition is not straight forward.
Lack of a carry-out from the MSB is not sufficient to indicate a correct result in signed addition!
Let's look at another interesting property of sign-magnitude complement numbers.
Is 1000 0000 a positive or negative sign-magnitude number?
Negative (MSB = 1)
What is its value?
-0 ?!?!
So there are two representations of 0, (0 and -0).
Is this a problem?
It seems so...
Problems with sign-magnitude representation:
Problems adding positive and negative values.
So if we are going to use sign-magnitude we would need a new method of doing addition.
Two different representations of 0.
On the other hand sign-magnitude representation is really easy to understand.
Sign-magnitude Conversions:
Sign-magnitude to Base 10
If the sign-magnitude number is positive:
Convert it as if it were unsigned.
If the sign-magnitude number is negative:
Flip the MSB.
Convert the result as if it were unsigned.
Multiply the base 10 value by -1.
Base 10 to Sign-magnitude
If the base 10 number is positive:
Convert it as if it were unsigned.
If the base 10 number is negative:
Multiply the base 10 number by -1.
Convert the result as if it were unsigned.
Set the MSB to 1.
Note: This may change the number if there are not enough bits.
Any other ideas of how to represent the negative of a number?
One possibility is One's Complement Representation
Positive numbers are represented just like in unsigned and sign-magnitude representations
0110 2 = 6 10
To get the negative of a number flip all of the bits of its unsigned representation.
1001 2 = -6 10
In one's complement flipping the bits of a number is equivalent to multiplying by -1 in base 10.
The MSB of a one's complement number indicates its sign:
MSB = 0 -> Positive.
MSB = 1 -> Negative.
What is the base 10 value of 1011 2:
Depends on the representation:
Unsigned: 1010 2 = 8 + 2 = 10 10
Sign-magnitude: 1010 2 = -1 * 0010 2 = -1 * 2 = -2 10
One's Complement: 1010 2 = -1 * 0101 2 = -1 * (4 + 1) = -5 10
What is the base 10 value of 0111 2:
Does it depend on the representation?
Not really, since it is a positive number in all of the representations.
0111 2 = 4 + 2 + 1 = 7 10
Addition with one's complement numbers:
Can we add one's complement numbers?
Here's that pesky negative zero again!
Is two zeros going to cause us problems...?
Consider the calculation 2 + (-2) + 2 = 2
There is a carry-out but it shouldn't be overflow.
2 can be represented with 4 bits in one's complement.
Problems with one's complement representation:
Incorrect answers when negative 0 (1111) appears as an intermediate result.
On the other hand, it gets most additions correct.
One's Complement Conversions
One's Complement to Base 10
If the one's complement number is positive:
Convert it as if it were unsigned.
If the one's complement number is negative:
Flip each of the bits.
Convert the result as if it were unsigned.
Multiply the base 10 value by -1.
Base 10 to One's Complement
If the base 10 number is positive:
Convert it as if it were unsigned.
If the base 10 number is negative:
Multiply the base 10 number by -1.
Convert the result as if it were unsigned.
Flip each of the bits.
Two's Complement Representation:
Positive numbers are represented just like in unsigned:
0101 2 = 5 10
To get the negative of a number flip the bits and add 1:
In two's complement flipping the bits and adding 1 is equivalent to multiplying by -1 in base 10.
Again, the MSB of a two's complement number represents the sign of the number.
MSB = 0 -> Positive.
MSB = 1 -> Negative.
Does two's complement have two representations of 0 (0 & -0)?
Let's see:
In an 8 bit computer the overflow bit would be lost...
Thus, there is only one 0 in two's complement representation.
This is a good thing.
But does it fix all of our problems?
Addition with two's complement numbers:
Can we add two's complement numbers?
It appears as if things are good with two's complement.
All additions work correctly.
This is the representation used by modern computers.
Two's Complement Conversions
Two's Complement to Base 10
If the two's complement number is positive:
Convert it as if it were unsigned.
If the two's complement number is negative:
Flip each of the bits.
Add one to the binary representation.
Convert the result as if it were unsigned.
Multiply the base 10 value by -1.
Base 10 to Two's Complement
If the base 10 number is positive:
Convert it as if it were unsigned.
If the base 10 number is negative:
Multiply the base 10 number by -1.
Convert the result as if it were unsigned.
Flip each of the bits.
Add one to the binary representation.
Subraction of two's complement numbers:
Can we perform subtraction of binary numbers?
Sure: A - B = A + (-B)
To subtract 2 two's complement numbers:
Add the first number to -1 * the second number.
Thus, there is no need for a subtraction operation
As long as we can compute -1 * a number!
Range of representable numbers:
A couple of our examples have thrown away overflow bits.
We assumed a computer can't dynamically add more bits to its numbers.
Thus, it is important to know the size of numbers that can be represented with some number of bits.
N Bit Sign-magnitude:
1 bit for the sign
n-1 bits for the number, which is unsigned.
Thus, the maximum number is 2(n-1)-1, which can be positive or negative.
The total # of bit patterns using n bits is 2n.
Two of them represent 0 thus there are 2n - 1 distinct values.
N Bit One's Complement:
2n-1 numbers with MSB = 0 (00...00 is zero).
2n-1 - 1 positive numbers.
2n-1 numbers with MSB = 1 (11...11 is -zero).
2n-1 - 1 negative numbers.
N Bit Two's Complement:
2n-1 numbers with MSB = 0 (00...00 is zero).
2n-1 - 1 positive numbers.
2n-1 numbers with MSB = 1.
2n-1 negative numbers.
The number of negative numbers is one more than the number of positive numbers. (See homework...)
Overflow when adding two's complement numbers:
Overflow results when there are insuffient bits to represent the result:
With unsigned addition a carry out from the MSB was sufficient to indicate overflow.
From the examples, this is obviously not the case when adding two's complement numbers.
Overflow in two's complement:
Example #3: -7 + -5 = -13 < -8 = -(24-1)
Thus, the correct result -13 can not be represented in two's complement using 4 bits.
Example #4: 6 + 3 = 9 > 7 = 24-1 - 1 = 7
Thus, the correct result 9 can not be represented in two's complement using 4 bits.
More in the homework...