100 likes | 219 Vues
COMP 40: Machine Structure and Assembly Language Programming (Fall 2013). Why does 2’s complement arithmetic work?. Noah Mendelsohn Tufts University Email: noah@cs.tufts.edu Web: http://www.cs.tufts.edu/~noah. Goals for this presentation.
E N D
COMP 40: Machine Structure and Assembly Language Programming (Fall 2013) Why does 2’s complement arithmetic work? Noah Mendelsohn Tufts UniversityEmail: noah@cs.tufts.edu Web: http://www.cs.tufts.edu/~noah
Goals for this presentation • Develop your intuitionabout why 2’s complement arithmetic works
The problem: • I get why: 2 is encoded as 00000010 • I have no clue why: -2 is encoded as 11111110
Modular arithmetic What happens when this goes past 999999? 999999 + 1 = 0 So, what should 0-1 be?
Modular arithmetic – mod 100 0 99 1 98 2 75 50
Modular arithmetic – mod 256 255 0 1 254 2 192 128
Unsigned modular arithmetic – mod 256 00000000 11111111 255 0 00000001 1 254 11111110 00000010 2 192 11000000 128 10000000
Signed modular arithmetic – mod 256 00000000 11111111 00000001 -1 0 1 11111110 -2 00000010 2 -64 11000000 -128 10000000
The 2s complement magic The hardware does the same thing, only the interpretatation is different!