Why does -INT_MIN = INT_MIN in a signed, two's complement representation?

后端 未结 7 2057
一生所求
一生所求 2020-11-27 21:59

I still haven\'t found a reason why the lowest signed negative number doesn\'t have an equivalent signed positive number? I mean in a 3 digit binary number for simplicity 10

相关标签:
7条回答
  • 2020-11-27 22:39

    So how do we know two's complement 1000 is -4 1000 0000 is -128 and so on? We have no original positive number

    Your mistake is thinking that we need a two's complement representation of the positive number in order to compute the two's complement representation of the negative number.

    The process for finding the two's complement of a negative number is:

    Start out with the normal, non-two's complement representation of the absolute value of the number to be represented. So for -4, take the non-two's complement representation of |-4|, 100.

    Flip all the bits: 100 -> 011 (or ...11111011 with the ones continuing indefinitely to the left).

    Add one: 011 -> 100 (or ...11111100)

    Now truncate to the number of bits you're using (this eliminates the carry bit or the infinite string of 1s). As a result, 100 is the 3-bit, two's complement representation of -4.

    To go the other way, take the two's complement representation (100) flip the bits (011) and add one (100) you now have the non-two's complement representation of |-4|. 1*2^2 + 0*2^1 + 0*2^0 = 4. Therefore we know that representation we started off with, 100, is the 3-bit, two's complement representation of -4.

    0 讨论(0)
提交回复
热议问题