Download presentation
Presentation is loading. Please wait.
Published byJean Hoover Modified over 9 years ago
1
Representing Negative Numbers Noah Mendelsohn Tufts University Email: noah@cs.tufts.edunoah@cs.tufts.edu Web: http://www.cs.tufts.edu/~noah COMP 40: Machine Structure and Assembly Language Programming (Fall 2014)
2
© 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
3
© 2010 Noah Mendelsohn Representing Negative Numbers
4
© 2010 Noah Mendelsohn Bits mean nothing on their own… What's this: 1111 0100 ? 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?
5
© 2010 Noah Mendelsohn At least three ways to represent negative numbers Use the high order bit as a sign (sign and magnitude) – 1000 0101 Flip all the bits for negative (ones’ complement) – 1111 1010 Flip all bits then add one (two’s complement) – 1111 1011 5 We usually represent the number 5 as 0000 0101 How should we represent -5?
6
© 2010 Noah Mendelsohn At least three ways to represent negative numbers Use the high order bit as a sign (sign and magnitude) – 1000 0101 Flip all the bits for negative (ones’ complement) – 1111 0101 Flip all bits then add one (two’s complement) – 1111 1011 6 We usually represent the number 5 as 0000 0101 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;
7
© 2010 Noah Mendelsohn At least three ways to represent negative numbers Use the high order bit as a sign (sign and magnitude) – 1000 0101 Flip all the bits for negative (ones’ complement) – 1111 0101 Flip all bits then add one (two’s complement) – 1111 1011 7 We usually represent the number 5 as 0000 0101 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!
8
© 2010 Noah Mendelsohn Odometer Arithmetic Modular Number Systems
9
© 2010 Noah Mendelsohn The problem: I get why: 2 is encoded as 00000010 I have no clue why: -2 is encoded as 11111110 9
10
© 2010 Noah Mendelsohn Modular arithmetic 10 What happens when this goes past 999999? 999999 + 1 = 0 So, what should 0-1 be?
11
© 2010 Noah Mendelsohn Modular arithmetic – mod 100 11 1 0 2 50 98 99 75
12
© 2010 Noah Mendelsohn Modular arithmetic – mod 256 12 1 0 2 128 254 255 192
13
© 2010 Noah Mendelsohn Unsigned modular arithmetic – mod 256 13 1 0 2 128 254 255 192 00000001 00000010 10000000 11000000 11111110 11111111 00000000
14
© 2010 Noah Mendelsohn Signed modular arithmetic – mod 256 14 1 0 2 -128 -2 -64 00000001 00000010 10000000 11000000 11111110 11111111 00000000
15
© 2010 Noah Mendelsohn The 2s complement magic 15 The hardware does the same thing, only the interpretatation is different!
16
© 2010 Noah Mendelsohn Polynomials
17
© 2010 Noah Mendelsohn Binary numbers encode coefficients of polynomials 17 0 x 2 7 + 0 x 2 6 + 0 x 2 5 + 0 x 2 4 + 0 x 2 4 + 1 x 2 2 + 0 x 2 1 + 1 x 2 0 = 5 00000101
18
© 2010 Noah Mendelsohn Two’s complement numbers as polynomial coefficients 18 -1 x 2 7 + 1 x 2 6 + 1 x 2 5 + 1 x 2 4 + 1 x 2 3 + 0 x 2 2 + 1 x 2 1 + 1 x 2 0 = -5 11111011 -128 + 64 + 32 + 16 + 8 + 0 + 2 + 1 = -5 Hi order digit subtracted, all others added!
19
© 2010 Noah Mendelsohn Try it with -1 in two’s complement 19 11111111 + 127 = -1 -128
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.