Why does 2’s complement arithmetic work?

1 / 10

# Why does 2’s complement arithmetic work? - PowerPoint PPT Presentation

COMP 40: Machine Structure and Assembly Language Programming (Fall 2013). Why does 2’s complement arithmetic work?. Noah Mendelsohn Tufts University Email: [email protected] Web: http://www.cs.tufts.edu/~noah. Goals for this presentation.

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.

## PowerPoint Slideshow about ' Why does 2’s complement arithmetic work?' - nicki

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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

COMP 40: Machine Structure and

Assembly Language Programming (Fall 2013)

### Why does 2’s complement arithmetic work?

Noah Mendelsohn

Tufts UniversityEmail: [email protected]

Web: http://www.cs.tufts.edu/~noah

Goals for this presentation
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?

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!