Negative numbers are stored as 2's complement in memory, how does the CPU know if it's negative or positive?

MCG picture MCG · Oct 7, 2011 · Viewed 15.3k times · Source

-1 can be represented in 4 bit binary as (2's complement) 1111

15 is also represented as 1111.

So, how does CPU differentiate between 15 and -1 when it gets values from memory?

Answer

Peter vdL picture Peter vdL · Oct 7, 2011

The CPU doesn't care whether a byte holds -1 or 15 when it moves it from one place to another. There's no such thing as a "signed move" (to a location of the same size - there is a signed move for larger or smaller destinations).

The CPU only cares about the representation when it does arithmetic on the byte. The CPU knows whether to do signed or unsigned arithmetic according to the op-code that you (or the compiler on your behalf) chose.