Representing numbers
- Easiest class is just natural numbers
- Just convert to binary
- We usually have a fixed size (8, 16, 32, 64 bits)
- unsigned int
- How about negative numbers
- Sign magnitued
- Just what we do in regular life
- -4, -9, +8 (or 8)
- Use the first bit to represent the sign
- fixed bit size again, choose 8
- 00001001 is 9
- 10001001 is -9
- Format (-1)s*magnitued
- SMMM MMMM
- What is 0000 0000, how about 1000 0000
- How do we add sign-magnitude numbers?
- Two's compliment.
- With two decimal digits we can represen 0 - 99
- (With two binray digits we can represent 0-3)
- With sign magnitude we can shift this range, but math becomes hard
- There are other equally vialble systems where the math is easier.
- REMEMBER WE ARE WORKING ON REPRESENTATIONS HERE
- We need to have an encoding that is efficient
- Easy to convert from/to
- Easy to perform math in
- represents as many numbers as possible
- One way to move the range is to represent everything in two's compliment
- Positive integers just give n bit binary representation
- Negative integers, give n bit positive representation, flip the bits and add one.
- in 6 bits, 13 = 001101
- -13 = 001101+1 = 110011
- Notice 001101 + 110011 = 1000000
- Try -6, 4 and math on these two.
- Look at the posiblities in 3 bits.
- Overflow (in three bits signed, add 2+3)
- There are other methods (excess, one's compliment) but we will let these for another class
- Real numbers
- Scientific notation
- 4.243 x 10 4
- 3.61 x 10 -2
- Can represent very large or very small numbers
- exponent, sign, mantissa
- adding in scientific notation.
- in 5 decimal digits, add the two above numbers
- floating point roundoff error, representation error
- Computer representation [+|-] d.d*[[+|-]ed+