Lecture 06 - Signed numbers
Goals
- Learn how to represent signed numbers
- Learn the process of converting back and forth between two's compliment and decimal
- Learn about two's compliment addition
negative numbers
We are, of course, missing out on half of all integers -- we have nothing less than 0! we call this unsigned binary
How can we represent signed numbers? If we are working on paper, then we can just put a negative sign in front, but in hardware we don't get a negative sign
there are a collection of different approaches I will tell you about two
- signed magnitude which seems like the obvious way
- two's compliment which is the way we actually do it
signed magnitude
we could just reserve the first bit as a sign bit
this is called signed magnitude
there are a couple of reasons we aren't fond of this
- we get two zeros: 000 (0) and 100 (-0)
- comparisons between two numbers require a lot of extra logic
- addition gets harder because we need extra logic to check which operation we are actually doing
two's compliment
two's compliment is based on a simple process that we use to negate a number
- flip all the bits (run them all through a NOT operation)
- add 1 to the result (ignoring any carry out)
this works in either direction -- making a positive number negative and a negative number positive
In order for this to make any sense, it is essential that we know how many bits our numbers are
Example: the 4-bit binary representation of 6 is 0110 to find the representation of -6
- flip the bits: 1001
- add 1: 1010
-6 = 1010 in 4-bit two's compliment
Why is it called two's compliment?
I was asked about this after class, and it seemed worth sharing. The "two" in two's compliment is actually for an -bit number.
For a 3 bit number, our target is , and we get our compliments by figuring out what sums to 8.
- 001 (1) is complimented by 111 (7) (1+7 = 8)
- 010 (2) is complimented by 110 (6) (1+6 = 8)
- 011 (3) is complimented by 101 (5) (1 + 5 = 8)
comparing two's compliment and signed magnitude
| binary | unsigned | signed magnitude | two's compliment |
|---|---|---|---|
| 000 | 0 | 0 | 0 |
| 001 | 1 | 1 | 1 |
| 010 | 2 | 2 | 2 |
| 011 | 3 | 3 | 3 |
| 100 | 4 | -0 | -4 |
| 101 | 5 | -1 | -3 |
| 110 | 6 | -2 | -2 |
| 111 | 7 | -3 | -1 |
Note that we still end up with the high-order bit being 1 when the number is negative
conversions
We use the same process if we want to convert a two's compliment number to decimal
- if the number is positive
- convert the number like it was unsigned
- if the number is negative
- negate it
- flip all the bits
- add 1
- convert the now positive number like it was unsigned
- negate the result (add a minus sign)
- negate it
Example: finding the value of the 4-bit two's compliment number 1110
- negate it
- flip the bits: 0001
- add 1: 0010
- convert it to decimal: 2
- negate the result: -2
Math with two's compliment
One of the nice features of two's compliment is that because the representation is designed to be cyclic, addition just works
1111
1111 (-1)
+ 0011 ( 3)
----------
0010 ( 2)
Note that we have ignored the carry out of the last column
That is not to say that we can't sum up to numbers we can't represent
0111
0111 (7)
+ 0111 (7)
----------
1110 (-2)
Just as we saw with unsigned numbers -- the result is more positive than we can represent, so we have overflow
This happens in the other direction as well
10
1010 (-6)
+ 1001 (-7)
----------
0011 ( 3)
Here our result is to negative for us to be able to represent the number we call this underflow
somewhat confusingly, we sometimes just refer to "Exceeding our ability to represent the result" generically as overflow.
The key to telling when we have a good result or when we have underflow or overflow is to look at the carry in and the carry out of the leftmost column. If they match, the value is correct, if they don't match then we had underflow or overflow.
Mechanical level
vocabulary underflow
Skills
- convert from decimal to two's compliment
- convert from two's compliment to decimal
- add two two's compliment numbers together
Last updated 04/05/2023