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

前端 未结 6 1928
误落风尘
误落风尘 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:41

    At the compiler level, the differentiation is based on data type. If the data type is int, then 4 bytes is allocated to that variable (in C). So 15 in 2's complement is 00000000 00000000 00000000 00000000 00001111 while -1 is 11111111 11111111 11111111 11111111 . The compiler then converts this to the corresponding opcode of the CPU. The CPU executes this opcode and at this level everything is in the form of 1s and 0s.

提交回复
热议问题