Sign Magnitude
Objectives
We would like to :
- Discuss the sign magnitude representation of negative numbers.
- Discuss mathematics involved with this system.
Notes
- Once we learn subtraction, we naturally fall to the need to represent negative numbers.
- How do we represent negative integers in real life?
- The system we use is called sign-magnitude.
- Think number line.
- A sign tells us if we are to the left or right of 0
- The magnitude tells us how far.
- This is a natural extension of idea a positive integer is a distance on the right away from 0.
- But is this a good system?
- We need a side-trip to discuss representation
- With 1 bit we can represent two things
- a 0 could be false, a 1 could be true
- A 0 could be night, a 1 could be day.
...
- With two bits we have four different values
- 00, 01, 10, 11
- Therefore with two bits we can represent 4 different objects.
- How about three bits?
- In general, each time we add a bit, we double the number of values.
- with n bits we have 2n values, so we can represent 2n different things.
- Remember the inverse of exponents is logs.
- If ab = c
- loga(ab) = b
- or loga(c) = b
- By the way, logb(a) = logc(a)/logc(b)
- Frequently we don't have access to log2(n)
- So we will take $\frac{log_{10}(n)}{log_{10}(2)}$
- Or we will take $\frac{ln(n)}{ln(2)}$
- Remember $ln(n) = log_e(n)$
- We will need this in algorithms, so some day I might prove it to you.
- Floors and ceilings
- remember ⌈ n ⌉ is the integer greater than or equal to n
- So ⌈ 5 ⌉ = 5
- So ⌈ 6.0001 ⌉ = 7
- remember ⌊ n ⌋ is the integer less than or equal to n
- So ⌊ 6.0001 ⌋ = 6
- So ⌊ 2 ⌋ = 2
- Floor means truncate
- So if we have k objects, we need ⌈ log2k⌉ bits.
- So if we need to represent 16 things, we need log2 16 = 4 bits.
- And if we need to represent 20 things, we need ⌈log220⌉ ≈ ⌈4.3219⌉ = 5 bits.
- This explains why many things we do in computing are powers of two, or multiples of powers of two.
- So if we have 8 bits to represent a number
- We will need one of them to represent the sign.
- And we will have seven to represent the magnitude
- So we can represent
- 0 through 11111112 or 28-1
- and -0 through -1111111 or -28 -1
- If we assume that 0 is + and 1 is -
- what is 00000000?
- What is 10000000?
- Well, that is not good.
- What do we do about -0 in real life? (we ignore it)
- But that is harder to do with hardware.
- In general if we have n bits to represent a number
- What is the range for unsigned numbers?
- What is the range for signed numbers?
- Do we lose anything by adding the sign?
- How do we do math on signed numbers?
- 23 + 15 , add, answer positive
- 23 - 15 , subtract, answer is positive
- 23 + -15, change to subtraction, answer positive
- 23 - -15, change to addition, answer positive
- -23 + 15, change to subtraction, answer negative
- -23 - 15, change to addition, answer negative
- -23 + -15, addition, answer negative
- -23 - -15, subtraction, answer negative
- What about the hard case?
- 15 + 23, add, answer positive.
- 15 - 23, , change to -23 + 15, answer negative.
- 15 + -23, , change to -23 + 15, answer negative.
- 15 - -23, , change to 23 + 15, answer positive.
- -15 + 23, , change to 23 - 15, answer positive.
- -15 - 23, , change o -23 + -15, answer negative.
- -15 + -23, ,change to -23 + -14, answer negative.
- -15 - -23, , change to 23 - 15, answer positive.
- This is bad, we have to compare signs, sometimes magnitudes and decide which computation to do based on the result.
- Looking ahead, we could do this in hardware, but it would be complex.
- We would need an adder and a subtractor.
- We might need to find a better representation.