World's most popular travel blog for travel bloggers.

[Solved]: How are Signed integers different from unsigned integers once compiled

, , No Comments
Problem Detail: 

How are Signed integers different from unsigned integers once compiled?

I already know about twos compliment and the like but my question is how can you tell the difference when looking at 8 bit integers when in binary eg 10000001 if this is a signed integer it would be equal to -127 but if it is unsigned it would be equal to 129. So my question is how can you tell the difference when presented with just 8 binary bits.

And also how are signed integers different from a compilers perspective, and how can a CPU tell the difference while performing arithmetic on them

Asked By : ununpentium-299

Answered By : David Richerby

For simple types, you can't tell by looking at the bits in memory. Because the source specifies that the data is of a particular type, the compiler generates code that interprets the bits in memory as representing data of that particular type. In particular, in cases where different CPU instructions are needed (e.g., integer vs floating point), the compiler will generate instructions appropriate for the datatype in question. Note that, by the magic of two's complement, signed and unsigned integers are added, subtracted and multiplied by the same instructions. (That is, if you compute $z=x\times y$ believing that $x$ and $y$ are unsigned and I then tell you that they're actually signed, all you need to do is interpret $z$ as being signed and you have the right answer.)

Depending on the language, objects may or may not have some sort of tag that says what class the object instantiates. But the same considerations as above apply: there's no physical difference in memory between the tag and any other datatype that happens to have the same number of bits.

Best Answer from StackOverflow

Question Source : http://cs.stackexchange.com/questions/30046

3.2K people like this

 Download Related Notes/Documents

0 comments:

Post a Comment

Let us know your responses and feedback