Slide 1

Slide 1 text

Grokking Signed Overflows Detecting an overflow when adding two signed numbers

Slide 2

Slide 2 text

Grokking Signed Numbers

Slide 3

Slide 3 text

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

Slide 4

Slide 4 text

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) .

Slide 5

Slide 5 text

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) .

Slide 6

Slide 6 text

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) .

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

Grokking Signed Overflows

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

• 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

Slide 12

Slide 12 text

• 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

Slide 13

Slide 13 text

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