18

Introduction Were learnin bout numberrrrs! O_O >_< ^_^

Embed Size (px)

Citation preview

Page 1: Introduction Were learnin bout numberrrrs! O_O >_< ^_^
Page 2: Introduction Were learnin bout numberrrrs! O_O >_< ^_^

Introduction

• We’re learnin’ ‘bout numberrrrs!

O_O >_< ^_^

Page 3: Introduction Were learnin bout numberrrrs! O_O >_< ^_^

Okay, so this is how you add…

• The general premise of addition is adding numbers and carrying when necessary.

• Carrying is the important part• Like, so important that you could fail epically if

you don’t.• Just wait.

Page 4: Introduction Were learnin bout numberrrrs! O_O >_< ^_^

Adding some more.

Start on the right side as normal

1+1 = 2; since this cannot be expressed with one digit, we carry a 1 (just like normal addition)

1+0+0=10+1 = 11+1=2, carry again1+0 = 1

1 0 0 11 1 0 1 +-----------

11

1 1 10 0

Page 5: Introduction Were learnin bout numberrrrs! O_O >_< ^_^

Subtracting…

• It’s like James and me. I borrow from him all the time. The difference is that I give him some back, sometimes.

• And apparently this “pays back” as well.

Page 6: Introduction Were learnin bout numberrrrs! O_O >_< ^_^

Subtracting some more.

Start on the right side as normal

0<1, so borrow 1010-1=1 (in binary)Pay back 11+1=10, 1 < 10, so borrow

1011-10 = 1Pay back 11-1 = 0

1 1 00 1 ---------------

10 1

(10)

1+

(10)

11+

Page 7: Introduction Were learnin bout numberrrrs! O_O >_< ^_^

Addition of Hex Values

• Hex is base 16, uses 0-9 and A-F.• Same process as any other base: carry over

when the current column capacity has been exceeded.

Page 8: Introduction Were learnin bout numberrrrs! O_O >_< ^_^

Hex Addition

• 5+D = 5+14 = 1910

• 1910 = 16+3 = (10 + 3)16

• This means we carry a 1 into the next column

• A+F+1= 10+15+1 = 2610

• 2610 = 16 + 10 = (10 + A)

• This means we carry a 1

A 5+ F D----------------------

1

1 A 3

Page 9: Introduction Were learnin bout numberrrrs! O_O >_< ^_^

Integer Representation

• A byte has 8 bits, which allows for 256 variations (different numbers) [28]

• If we want positive and negative though, we use the Most Significant Bit (MSB) to determine sign (1 = negative, 0 = positive)

• This lets us go from -127 to 127• Notice a number is lost in the range (+/- 0),

meaning only 255 distinct numbers possible

Page 10: Introduction Were learnin bout numberrrrs! O_O >_< ^_^

Questions!?

• What range of numbers can be gotten from 6 bits?(unsigned)

• What is 100000 unsigned? Signed?• 011111 unsigned? Signed?

Page 11: Introduction Were learnin bout numberrrrs! O_O >_< ^_^

Representing Negative Numbers Using Two complement

• Another method involves setting the MSB to be -128 and making the rest positive to get a range of negative numbers.

• 10000000 base 2 = -128• 10000001 base 2 = -127 • Etc.• To go from positive to negative, just flip the bits and add 1

base 2:• 8 base 10 = 00001000• -8 base 10 = 11110111+1 = 11111000 base 2

Page 12: Introduction Were learnin bout numberrrrs! O_O >_< ^_^

Practice

• Convert to Two’s Complement:-10001000

-000001

-1010

Page 13: Introduction Were learnin bout numberrrrs! O_O >_< ^_^

Representing Real Numbers

• Two ways of representing:– Fixed point or floating point– Fractional and Whole parts separated by a Radix point.

• With fixed point between bits 3 and 4, making bits 3, 2, and 1 negative, max range is from -15.875 to 15.875

• Everything after the decimal place gains a negative exponent 0.1 base 2 = 2-1

, 0.001 base 2 = 2-3

Page 14: Introduction Were learnin bout numberrrrs! O_O >_< ^_^

(cont.)

• This can be created with two’s complement, which places the fixed point between 4 and 5.

• Fixed point just doesn’t have a lot of range with a given set of bits.

Page 15: Introduction Were learnin bout numberrrrs! O_O >_< ^_^

Floating Point Representation

• Until recently, floating point has been a pain. IEEE 734 created a standard for them.

• However, it’s hard and junk apparently, so they won’t tell us about it.

• BUT. They explicitly reference wikipedia. Such a terrible source.

• The mantissa is basically the decimalised version of the number.

Page 16: Introduction Were learnin bout numberrrrs! O_O >_< ^_^

Floating Point Algorithmic example

• Convert 12.5 to normalised binary format.1. Convert 12.5 to binary: 1100.12. Move the point four to the left: 0.11001

(mantissa)3. That four becomes the exponent, write it in

binary: 01004. Write the number as mantissa * 2exponent

5. 0.11001 * 20100

Page 17: Introduction Were learnin bout numberrrrs! O_O >_< ^_^

Advantages of Integer and Floating Point

• Integers can be stored accurately• Floating point allows more numbers to be

represented• Floating points allows fractional values (kinda

ties in with the above)

Page 18: Introduction Were learnin bout numberrrrs! O_O >_< ^_^

Truncation, Underflow, and Overflow Errors

• Truncation cuts off anything after the radix point. Happens when you try to put a decimal/”real” number in an integer slot.

• Overflow basically is trying to put a number that is too big into a slot… like when you enter 2309472349072309472097429579347530453 into something. This can also occur when the mantissa of floating point or exponent are to large to be represented in the number of bits.

• Underflow is when a number to be stored is too small. The underflow case for floating point can be the same as the overflow case if the exponent is too large to be represented in both cases.