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

前端 未结 6 1899
误落风尘
误落风尘 2020-12-08 02:46

-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 get

6条回答
  •  南方客
    南方客 (楼主)
    2020-12-08 03:42

    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.

提交回复
热议问题