1 / 10

Why does 2’s complement arithmetic work?

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.

nicki
Télécharger la présentation

Why does 2’s complement arithmetic work?

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. Goals for this presentation • Develop your intuitionabout why 2’s complement arithmetic works

  3. The problem: • I get why: 2 is encoded as 00000010 • I have no clue why: -2 is encoded as 11111110

  4. Odometer ArithmeticModular Number Systems

  5. Modular arithmetic What happens when this goes past 999999? 999999 + 1 = 0 So, what should 0-1 be?

  6. Modular arithmetic – mod 100 0 99 1 98 2 75 50

  7. Modular arithmetic – mod 256 255 0 1 254 2 192 128

  8. Unsigned modular arithmetic – mod 256 00000000 11111111 255 0 00000001 1 254 11111110 00000010 2 192 11000000 128 10000000

  9. Signed modular arithmetic – mod 256 00000000 11111111 00000001 -1 0 1 11111110 -2 00000010 2 -64 11000000 -128 10000000

  10. The 2s complement magic The hardware does the same thing, only the interpretatation is different!

More Related