1 / 30

Addition, Signed Numbers, and Overflow

Addition, Signed Numbers, and Overflow. CS/COE 0447 Jarrett Billingsley. Class announcements. nah. Binary addition. Adding in binary. it works the same way as you learned in school except instead of carrying at 10 10 , you carry at … 10 2 ! 1 + 1 = 10 2 (2 10 )

ssorrell
Download Presentation

Addition, Signed Numbers, and Overflow

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. Class announcements • nah 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) • 1 + 1 + 1 = 112 (310) • let's try it. (what are these in decimal?) 1011 0010 +0010 1111 1110 0001 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? • -1 * value • 0 – value • or just…-value, like in math! • it's negdst, src in MIPS 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? the sign bit is always the MSB– NOT "the first 1 bit" CS447

  9. Sign-magnitude • this leads us to an intuitive representation: • the bits after the sign bit are the distance from 0 (aka magnitude) 1110 1100 1010 0000 0100 0010 0110 1000 1101 1111 1011 1001 0001 0111 0101 0011 what about 1000? what is its distance from 0? -3 -7 -5 -6 -2 -4 -1 0 +1 +2 +6 +4 +5 +3 +7 we have TWO ZEROES: +0 and -0 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 1011 1101 0000 0100 0010 0110 1111 1000 1010 1100 1110 0001 0111 0011 0101 what about 1111? if you NOT that, what positive number do you get? positive numbers always look the same in every system. -7 -3 -5 -2 -6 -4 -1 0 +1 +2 +6 +4 +7 +5 +3 we have TWO ZEROES again! 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) 1001 0110 1001 0110 64s 32s 16s 8s 4s 2s 1s -128s 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. CS447

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

  14. A number with no positive counterpart • remember, to negate, you NOTand then add 1 • so what if we negate 1000 (-8) here? 1 1 1 0111 1000 NOT +0001 you add the sign bit like a normal place. 1000 we negated -8 and got -8. 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… signed numbers: unsigned numbers: 0 0 1 1 15 -1 -2 14 2 2 -3 13 3 3 but the meaningsare different. -4 12 4 4 -5 11 5 5 6 6 -6 10 9 -7 7 7 -8 8 adding goes clockwise; subtracting goes counterclockwise what is -2 + 5? what is 14 + 5? 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 someintuition… 0 1 -1 -2 2 -3 3 -4 4 half the numbers are negative. -5 5 6 so if you know the positive range, chop it in half. -6 -7 7 -8 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 to binary? 1 1 bit pattern for-6 is… -8+2 0011 0011 3 +10 13 3 +-6 -3 +1010 +1010 1101 1101 -8 + 4 + 1 is… 8 + 4 + 1 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. -3 -2 -5 13 -2 11 1101 -0010 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). 1 1 0 0 7 -4 3 0111 +1100 7 +-4 3 = 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")… signed numbers: unsigned numbers: 0 0 1 1 15 -1 -2 14 2 2 we get a number too big to be represented. -3 13 3 3 -4 12 4 4 -5 11 and it gets wrapped around. 5 5 6 6 -6 10 9 -7 7 7 -8 8 this is overflow. what is 14 + 5? 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! • if you add 3 32-bit numbers, you can get a 34-bit result… and so on. 1111 +1111 11110 CS447

  26. Detecting overflow: unsigned addition • the simplest case. 1 1 0 0 0111 +1100 7 +12 3?? if the sum is smaller than either of the addends, an overflow happened. 0011 1 this is because the result is really10011, but we only have 4 bits. so that extra bit… goes into the bit bucket ;) CS447

  27. Detecting overflow: signed addition • this one's more subtle. is this possible? what are the two ways to cross the line? 1. add two positives, get a negative 0 0 1 1 -1 -1 -2 -2 2 2 2. add two negatives, get a positive -3 -3 3 3 -4 -4 4 4 if we add numbers of opposite signs, it's impossible to cross the line. -5 -5 5 5 6 6 -6 -6 no.the largest positive is 7; the furthest we could get is 6. -7 -7 7 7 -8 -8 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. with unsigned subtraction, 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? 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

More Related