Representing Negative Numbers Noah Mendelsohn Tufts University Web: COMP 40: Machine Structure and Assembly Language Programming (Fall 2014)
© 2010 Noah Mendelsohn Goals for this presentation Discuss the representation of variables that can take negative or positive values (or zero, of course!) Develop your intuition about why 2’s complement arithmetic works 2
© 2010 Noah Mendelsohn Representing Negative Numbers
© 2010 Noah Mendelsohn Bits mean nothing on their own… What's this: ? Ideas? –Positive integer 244 –HALT instruction (CPU idle) –Negative integer -12 –Small o with hat (circumflex) –0.957 pixel brightness (scaled by 255) –bit vector With 8 bits we can distinguish 256 choices –If we use some bit patterns for negative numbers… –…then those same patterns can no longer stand for positives! 4 Question…what’s the best representation for negative numbers?
© 2010 Noah Mendelsohn At least three ways to represent negative numbers Use the high order bit as a sign (sign and magnitude) – Flip all the bits for negative (ones’ complement) – Flip all bits then add one (two’s complement) – We usually represent the number 5 as How should we represent -5?
© 2010 Noah Mendelsohn At least three ways to represent negative numbers Use the high order bit as a sign (sign and magnitude) – Flip all the bits for negative (ones’ complement) – Flip all bits then add one (two’s complement) – We usually represent the number 5 as How should we represent -5? These seem obvious and simple… …but there are problems For example, both representations have separate representations of +0 and -0, which makes it hard to implement sequences like: x = (-3); x = x + 5;
© 2010 Noah Mendelsohn At least three ways to represent negative numbers Use the high order bit as a sign (sign and magnitude) – Flip all the bits for negative (ones’ complement) – Flip all bits then add one (two’s complement) – We usually represent the number 5 as How should we represent -5? Two’s complement is trickier to learn… … but it doesn’t have these problems. In fact, two’s complement has many advantages, and almost all modern computers use it!
© 2010 Noah Mendelsohn Odometer Arithmetic Modular Number Systems
© 2010 Noah Mendelsohn The problem: I get why: 2 is encoded as I have no clue why: -2 is encoded as
© 2010 Noah Mendelsohn Modular arithmetic 10 What happens when this goes past ? = 0 So, what should 0-1 be?
© 2010 Noah Mendelsohn Modular arithmetic – mod
© 2010 Noah Mendelsohn Modular arithmetic – mod
© 2010 Noah Mendelsohn Unsigned modular arithmetic – mod
© 2010 Noah Mendelsohn Signed modular arithmetic – mod
© 2010 Noah Mendelsohn The 2s complement magic 15 The hardware does the same thing, only the interpretatation is different!
© 2010 Noah Mendelsohn Polynomials
© 2010 Noah Mendelsohn Binary numbers encode coefficients of polynomials 17 0 x x x x x x x x 2 0 =
© 2010 Noah Mendelsohn Two’s complement numbers as polynomial coefficients x x x x x x x x 2 0 = = -5 Hi order digit subtracted, all others added!
© 2010 Noah Mendelsohn Try it with -1 in two’s complement =