Search code examples
storageunsignedsigned

How computer distinguish an integer is signed or unsigned?


Two's complements is set to make it easier for computer to compute the substraction of two numbers. But how computer distinguish an integer is signed integer or unsigned integer? It's just 0 and 1 in its memory.

For exmaple, 1111 1111 in the computer memory may represent number 255 but also can represent -1.


Solution

  • Signed and unsigned use the same data, but different instructions.

    The computer stores signed and unsigned integers as the same data. I.e. 255 and -1 are the same bits. However, you tell the compiler what type the variable has. If it is signed, the compiler uses signed operators for manipulating the variables (e.g. IDIV) and when unsigned, it uses another instruction (e.g. DIV). So the compiler makes a program which tells the CPU how to interpret the data.