1

I understand in a relative sense how the two's complement would return the negative. If I add 1011 and 0101, I get 0000 (with the last carried 1 dropped). But, what if the computer was only presented with n = -11 and stored it as 0101, how would it differentiate this from 5 (also 0101). Not a terribly important question, just curious.

1 Answer 1

2

Your calculation doesn't seem to work because you are only looking at 4 bits. A 4-bit signed integer can only have the range of -8 to 7 -(2^3) to 2^3 - 1. The leftmost bit is reserved for the 'sign' (if it's set, it's negative). So your hypothetical computer wouldn't be able to store -11.

In a 32-bit signed integer (which is what we have in our CS50 appliance), the range is −2,147,483,648 to 2,147,483,647, from −(2^31) to 2^31 − 1

So 11 would be:

0000 0000 0000 0000 0000 0000 0000 1011

making -11:

1111 1111 1111 1111 1111 1111 1111 0101

If you had an 8-bit integer, 11 would be:

0000 1011

and -11:

1111 0101

So I think what confused you is that you only dealt with 4 bits, and then tried to represent an integer that was too big to be represented in a signed int of 4 bits.

1
  • So, a sign flag is always used? I guess that's what confused me. I watched the more comfortable section from week 1 and it seemed like the flag idea was discarded in favor of the two's complement. So if they work together, it makes perfect sense to me. Thanks!
    – Benjamin T
    Commented Mar 1, 2015 at 21:56

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .