Presentation is loading. Please wait.

Presentation is loading. Please wait.

Addition, Signed Numbers, and Overflow

Similar presentations


Presentation on theme: "Addition, Signed Numbers, and Overflow"— Presentation transcript:

1 Addition, Signed Numbers, and Overflow
CS/COE 0447 Jarrett Billingsley

2 Class announcements project 1 due this Saturday! (or late on Sunday)
lab 4 is up - due Sunday I posted the lab 3 solution in the examples tab. CS447

3 Binary addition CS447

4 Adding in binary it works the same way as you learned in school except instead of carrying at 1010, you carry at… 102! 1 + 1 = 102 (210) = 112 (310) let's try it. (what are these in decimal?) - they are = 225 CS447

5 Formalizing the algorithm
for each pair of bits starting at the LSB, add the two bits and the carry the low bit of the sum goes into the sum row the high bit of the sum is the carry for the next higher bit this is the grade school algorithm cause it's how you learned to add in grade school. :^) CS447

6 Signed Integers CS447

7 Negation is an operation
"negate" means "change the sign to the opposite" positive becomes negative; and negative becomes positive when programming, how can you negate something? 0 – value -1 * value or just… -value, like in math! it's neg dst, src in MIPS - you will see "dst" or "dest" for "destination" (thing that is changed) and "src" for "source" (where we get the value) in programming all the time CS447

8 The basic idea numbers are fixed-size patterns of bits, and meaning is arbitrary we want to come up with rules which… assign negative values to some bit patterns allow us to do arithmetic correctly how could we represent a sign in binary? where's the sign written in math? +45 -19 + - 0101 1000 how many signs are there? - we write the sign in front…there are only two signs… - so we could represent the sign with one bit, and use the MSB as that sign bit - you have to know how many bits the number is before knowing where the sign bit is. - this is why I've been writing leading 0s. the sign bit is always the MSB – NOT "the first 1 bit" CS447

9 Sign-magnitude -6 -4 -2 +2 +4 +6 -7 -5 -3 -1 +1 +3 +5 +7 1110 1100
this leads us to an intuitive representation: the bits after the sign bit are the distance from 0 (aka magnitude) 1110 -6 1100 -4 1010 -2 0000 0010 +2 0100 +4 0110 +6 1111 -7 1101 -5 1011 -3 1001 -1 0001 +1 0011 +3 0101 +5 0111 +7 1000 what about 1000? what is its distance from 0? we have TWO ZEROES: +0 and -0 - having two zeroes is… a huge pain in the ass (and that's an understatement) - zero is a super common number and having to special-case for it everywhere is not great. sign-magnitude is still used… for floats. but not for ints. CS447

10 A dead end: Ones' complement
in this system, a negative is represented as the NOT of its positive 1001 -6 1011 -4 1101 -2 0000 0010 +2 0100 +4 0110 +6 1000 -7 1010 -5 1100 -3 1110 -1 0001 +1 0011 +3 0101 +5 0111 +7 1111 what about 1111? if you NOT that, what positive number do you get? positive numbers always look the same in every system. we have TWO ZEROES again! - this was borrowed from old mechanical calculators, who borrowed it from older accounting practices - no one uses it, so don't worry about having to learn it. just that no one uses it. because there are two zeroes. (and other issues.) no one uses ones' complement anymore. CS447

11 Two's complement CS447

12 Two's complement: the intuition
the bit patterns will look strange, but here's the idea: MSB (sign) -128s 64s 32s 16s s 4s 2s 1s you can think of the MSB as having a negative value. so what number does this represent? 2's complement integers are universal now. when we say "signed", we mean this. - thanks, previous student Sarah Priller, for sharing this insight - it's = -106 CS447

13 A weird number line -8 -6 -4 -2 +2 +4 +6 -7 -5 -3 -1 +1 +3 +5 +7 1000
in this system, to negate, you NOT and then add 1. 1000 -8 1010 -6 1100 -4 1110 -2 0000 0010 +2 0100 +4 0110 +6 1001 -7 1011 -5 1101 -3 1111 -1 0001 +1 0011 +3 0101 +5 0111 +7 but wait, what about 1000? now we only have one zero, and it's all 0 bits (whew) but the tradeoff is that the number line is lopsided. CS447

14 A number with no positive counterpart
remember, to negate, you NOT and then add 1 so what if we negate 1000 (-8) here? 1 1 1 1000 0111 NOT +0001 you add the sign bit like a normal place. 1000 we negated -8 and got -8. - Honestly, I wish we settled on using this "most-negative-integer" as an "invalid value", like "null for ints" - but we didn't. - and now we always have this weirdo value. CS447

15 A number… circle numbers on the computer wrap around, like on a clock. the same positions are represented by the same bit patterns… 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 unsigned numbers: 1 2 3 4 5 6 7 -8 -7 -6 -5 -4 -3 -2 -1 signed numbers: but the meanings are different. adding goes clockwise; subtracting goes counterclockwise takes us to 3, which is correct… interesting takes us to 3, which is weird, but makes sense in this number circle - this is modular arithmetic: we say = 3 mod 16. what is ? what is ? CS447

16 Range of unsigned vs. signed numbers
how many numbers can you represent with n bits? 2n and the range? 0 to 2n-1 for signed numbers, the range is: -2n-1 to 2n-1-1 that feels kinda awkward, so let's get some intuition… 1 2 3 4 5 6 7 -8 -7 -6 -5 -4 -3 -2 -1 half the numbers are negative. so if you know the positive range, chop it in half. 8 bits unsigned = 0 to 255, so signed is -128 to 127. CS447

17 Two's complement addition
the great thing is: you can add numbers of either sign without having to do anything special! Unsigned Signed 1 1 to binary? 3 +10 13 0011 3 +-6 -3 bit pattern for -6… -8+2 0011 +1010 +1010 1101 1101 is… is… it Just Works™. the actual patterns of bits are the same. so how does the computer "know" whether it's doing signed or unsigned addition? CS447

18 IT DOESN'T CS447

19 It's up to you! remember when we learned lb vs. lbu?
and I said you have to decide which to use? well, there's add vs. addu (and sub vs. subu) the funny thing is: add and addu do the exact same addition the only difference is how they respond to overflow. (as we'll see.) CS447

20 Subtracting in binary? CS447

21 Guess what it works exactly like you learned in school, even with 2's complement. 13 -2 11 1101 -0010 -3 -2 -5 1101 -0010 1011 1011 we had to borrow here, but when you borrow, you get 2, not 10. CS447

22 But there's another option
is subtraction really that different from addition? No. x - y = x + (-y). 7 -4 3 7 +-4 3 0111 +1100 = 0011 this is typically how computers do it, since this means we can reuse the same adder circuit. but what about that carry out from the MSB?? oh, we'll get to it… CS447

23 Overflow CS447

24 Breaking the wall we saw something weird a little earlier. when we cross these lines ("go off the end of the number line")… 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 unsigned numbers: 1 2 3 4 5 6 7 -8 -7 -6 -5 -4 -3 -2 -1 signed numbers: we get a number too big to be represented. and it gets wrapped around. - if you go the other way (add a negative), it's still called overflow – underflow is used for like, tiny floating point numbers. this is overflow. what is ? what is 6 + 3? CS447

25 How many bits? 99 +99 198 9999 +9999 19998 if you add two 2-digit decimal numbers, what's the largest number you can get? what about two 4-digit decimal numbers? what about two 4-bit numbers? what's the pattern of the number of digits? if you add two n-digit numbers in any base… the result will have at most n + 1 digits that means if we add two 32-bit numbers… …we might get a 33-bit result! 1111 +1111 11110 CS447

26 Detecting overflow: unsigned addition
the simplest case. 0111 +1100 7 +12 3?? if the sum is smaller than either of the addends, an overflow happened. 1 0011 this is because the result is really 10011, but we only have 4 bits. so that extra bit… goes into the bit bucket ;) - try to prove to yourself that you can't add two numbers and get a sum at least as big as one addend. - (think about the number circle. how "far around" can you get?) CS447

27 Detecting overflow: signed addition
this one's more subtle. is this possible? what are the two ways to cross the line? 1 2 3 4 5 6 7 -8 -7 -6 -5 -4 -3 -2 -1 1 2 3 4 5 6 7 -8 -7 -6 -5 -4 -3 -2 -1 1. add two positives, get a negative 2. add two negatives, get a positive if we add numbers of opposite signs, it's impossible to cross the line. no. the largest positive is 7; the furthest we could get is 6. - another way to think of it is: adding numbers of opposite signs always gives you a number closer to 0. - adding numbers of the same sign gets you further from 0, which is where the danger zone lies. - it's impossible to flip signs twice with a single addition. overflow occurs if we add two numbers of the same sign and get the opposite sign. CS447

28 Detecting overflow: subtraction
"adding the negative" is really doing a signed addition. sorta. you can end up with some weird-looking math that still gives you the correct result. 7-4 = 7+(-4) 14-5 = 14+(-5) 0111 +1100 7 +-4 3 1110 +1011 -2?? +-5 -7?? 0011 1001 addends have opposite signs, so no overflow possible. addends and sum all have same sign, so no overflow happened. what is 1001 interpreted as unsigned? - it's 9, which is the right answer… - think about this like positions on the circle: - 14 and -2 are at the same position. - 9 and -7 are at the same position. - in both interpretations, you are moving left by 5 numbers. CS447

29 Responding to overflow
once we detect the overflow, we have a few options. we could ignore it. in MIPS: addu, subu this is usually a bad idea - your program is almost certainly broken it's also the default in most languages (thanks, C) we could fall on the floor – that is, crash in MIPS add, sub exception can be caught and handled but it's more complex. orrr… we could store that 33rd bit somewhere else CS447

30 Maybe the bit bucket is a real place…
many other architectures do this, MIPS does not. they have a "carry bit" register this can be checked by the program after an add/sub this is very useful for arbitrary precision arithmetic if you want to add 64-bit numbers on a 32-bit machine… it's the same as doing two 32-bit additions in a row, preserving the carry from one addition to the next. addition really is linear-time, once you get above your word size. CS447


Download ppt "Addition, Signed Numbers, and Overflow"

Similar presentations


Ads by Google