07-27-2023, 12:38 AM
We should know how we manage to make x to -x:
1. Flip all bits in x. Like 5 is 0101, and we get `1010` in this step;
2. Add 1 to what we get in last step. This time we get `1010 + 1 = 1011`.
And in the real machine, negative ones are always shown in 2's complement format, so 1011 presents -5(which is `-8 + 2 + 1 = -5`).
Now back to the question, `INT_MIN` in the real machine is `1` with 31 consecutive `0`.
So after the first step, you will get a number which is `0` with 31 consecutive `1` and it is `INT_MAX` in C language.
In the second step, add `1` to what we get from the last step, and the result is `1` with 31 consecutive `0`, which is also `INT_MIN`.
So `INT_MIN = -INT_MIN`
1. Flip all bits in x. Like 5 is 0101, and we get `1010` in this step;
2. Add 1 to what we get in last step. This time we get `1010 + 1 = 1011`.
And in the real machine, negative ones are always shown in 2's complement format, so 1011 presents -5(which is `-8 + 2 + 1 = -5`).
Now back to the question, `INT_MIN` in the real machine is `1` with 31 consecutive `0`.
So after the first step, you will get a number which is `0` with 31 consecutive `1` and it is `INT_MAX` in C language.
In the second step, add `1` to what we get from the last step, and the result is `1` with 31 consecutive `0`, which is also `INT_MIN`.
So `INT_MIN = -INT_MIN`