Question

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

Was it helpful?

Solution

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.

OTHER TIPS

Most of the previous answers mentioned separate opcodes. That might be true for more complicated operations like multiplication and division, but for simple addition and subtraction that is not how the CPU works.

The CPU keeps data about the result of an instruction in its flags register. On x86 (where I am most familiar) the two most important flags here are the "overflow" and "carry" flags.

Basically the CPU doesn't care if the number is signed or unsigned it treats them both the same. The carry flag is set when the number goes over the highest unsigned value it can contain. The overflow flag is set when it goes over or under the range of an unsigned number. If you are working with unsigned numbers you check the carry flag and ignore the overflow flag. If you are working with signed numbers you check the overflow flag and ignore the carry flag.

Here are some examples:

Unsigned:

1111 (15) + 1111 (15) = 1110 (14)

What you do now is check the carry flag, which in this case contains one giving the final result

1 1110 (30)

Signed:

1111 (-1) + 1111 (-1) = 1110 (-2)

In this case you ignore the carry flag, the overflow flag should be set to zero.

Unsigned:

0111 (7) + 0111 (7) = 1110 (14)

When you check the carry flag it should be zero.

Signed:

0111 (7) + 0111 (7) = 1110 (-2)

In this case the overflow flag would be set meaning that there was an error in the addition.

So in summary the number is only signed or unsigned based on your interpretation of it, the CPU gives you the tools nessecary to distinguish between them, but doesn't distinguish on its own.

The CPU doesn't know if a number is signed or unsigned. When the compiler creates the machine language file, it chooses the correct operation to be executed to make a math operation with that number. If you declared your variable to be of the signed type, for instance, than the operation to be executed in machine language will be one that treats that memory position as a signed value.

In any software of any kind, it is always when you interpret the data that you give it meaning. A byte in memory can be a signed or unsigned number, or a character, or a part of a music file, or a pixel in a picture, etc. What gives it meaning is how you use that byte.

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.

In 2's complement to represent 15 we require 5 bits the range for 2'complemnt is -16 to 15,so the value become 01111 here the MSB bit is 0 so its positive value for -1 it will become 11111

The smallest accessible unit is 1 byte. That is 8 bits. In 8-bit representation 15 is stored as 00001111. Compiler differentiates between positive and negative number from sign bit. MSB is a sign bit. If it is 0 means positive number. If it is 1 means a negative number. MSB of the binary representation of 15 is 0. It means positive number and 00001111 corresponds to +15. 8 bit binary of -1 is 11111111 as its MSB is 1 it is taken ad negative number. The compiler first takes its 2's complement and then display the number with a negative sign. Remember if 8 bits are there to store number then the maximum value you can store in it is ( 2^7)-1 which takes 7 bits in its representation. It means MSB is always zero for a positive number. As in your question if we suppose to take 4 bits to store number then 3 bits are available to store the value as last bit is a reserve to hold the sign. To 3 bits the max value that can be store is (2^3)-1=7. It means 15 cannot be stored in 4bits. Therefore 1111 always taken as -1 by the compiler.

Follow the below link to get access to YouTube channel which has such tricky questions. www.YouTube. com/watch?v=ZxRHOT3pzx4

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top