Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Grokking Signed Overflows

Pat Hawks
September 05, 2014

Grokking Signed Overflows

Pat Hawks

September 05, 2014
Tweet

More Decks by Pat Hawks

Other Decks in Technology

Transcript

  1. Grokking Signed Numbers • When a number is signed, the

    high digit represents the additive inverse of what it typically would • For instance, with a 4 bit signed nibble: 0 0 0 0
 -8 4 2 1 • Instead of the high bit representing 8, with a signed number it would represent negative 8
  2. Grokking Signed Numbers • With our four bit signed nibble,

    7 is the highest integer our signed nibble can represent 0 1 1 1 = 7
 -8 4 2 1 .
 ( 0 + 4 + 2 + 1) .
  3. Grokking Signed Numbers • If we set the -8 place

    only, we get the lowest integer our signed nibble can represent, -8 1 0 0 0 =-8
 -8 4 2 1 .
 (-8 + 0 + 0 + 0) .
  4. Grokking Signed Numbers • If we set the -8 place

    and the 1 place, we add one to negative eight, for a total of -7 1 0 0 1 =-7
 -8 4 2 1 .
 (-8 + 0 + 0 + 1) .
  5. Grokking Signed Numbers • With our four bit signed nibble,

    we can represent all integers from -8 to +7 1000 -8 1001 -7 1010 -6 1011 -5 1100 -4 1101 -3 1110 -2 1111 -1 0000 0 0001 +1 0010 +2 0011 +3 0100 +4 0101 +5 0110 +6 0111 +7
  6. Grokking Signed Numbers • Because of how the numbers are

    arranged, we can do binary addition without worrying about the sign 1101 -3 13
 +0010 +2 2
 ────00 00000
 1111 -1 15
  7. Grokking Signed Overflows • When adding two numbers with a

    different sign bit, the sum will NEVER overflow 1000 -8
 +0001 +1
 ──── .
 1001 -7 No overflow
  8. • Sums will only overflow when both addends have the

    same sign bit. • Adding two positive numbers should never result in a negative number • Adding two negative numbers should never result in a positive number Grokking Signed Overflows
  9. • When adding two signed positive numbers, if the sign

    bit of the sum is set, an overflow has occurred • When adding two negative numbers, if the sign bit of the sum is not set, an overflow has occurred Grokking Signed Overflows
  10. Grokking Signed Overflows Detecting an overflow when adding two signed

    numbers Grokking Signed Overflows by Pat Hawks is licensed under a
 Creative Commons Attribution 4.0 International License