100 likes | 233 Views
This presentation by Noah Mendelsohn of Tufts University aims to enhance your understanding of 2’s complement arithmetic. It addresses why certain numbers are represented in binary form, exploring the foundations of signed and unsigned modular arithmetic. You’ll learn about modular calculations, such as how adding 1 to 999,999 results in 0, and how numbers like -1 and -2 are encoded in 2’s complement. The goal is to develop intuition about the mechanics behind these systems, clarifying common misconceptions through practical examples.
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!