Important note

I know that this question may seem too simple for you scientists; however, here is the best place that I know to post it.


Suppose we have an int with all bits being 0, except for the last which is 11101000. How does my computer understand to return its two's complement i.e. -24 as value and not its original value i.e. 232?

Edit: Question Details

I have written the following simple C++ code which shows unsafe conversions:

int main()
    double d = 1000;
        int i = d;
        char c = i;
        cout << c;

The code returns -24 for c while the last byte of i which is the value of c is 11101000; Actually -24 is its two's complement, i.e. 00011000.

  • 1
    $\begingroup$ What makes you think the computer understands that? Can you give us more context? Computers don't "understand" anything, so you must be interpreting some behavior you have observed; can you edit your question to explain what that is and the basis for your inferences? $\endgroup$
    – D.W.
    Jun 13 '20 at 21:41
  • 1
    $\begingroup$ Does this answer your question? Signed and unsigned numbers $\endgroup$ Jun 13 '20 at 21:42
  • $\begingroup$ @D.W. --- Thanks, I edited it. $\endgroup$
    – Alish
    Jun 14 '20 at 6:59
  • $\begingroup$ @YuvalFilmus --- I read only the accepted answer, but it doesn't seem to answer my question. $\endgroup$
    – Alish
    Jun 14 '20 at 7:01
  • $\begingroup$ @Alish. Is your question "why is the returned value equal to the the integer represented by the last byte of (the representation of) 1000?" or "How does the computer know to print a the characters $\texttt{-24}$ from the byte 11101000? (or, more generally, any number from its representation in two's complement)" $\endgroup$
    – Steven
    Jun 14 '20 at 7:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.