c# – Why is the int variable -2147483648 multiplied by -1 to get -2147483648?

Question:

int a = -2147483648;
int b = a * -1; // -2147483648

The range of 32-bit signed int values ​​is

-2,147,483,648 ~ 2,147,483,647

So we know that the value of b cannot be +2147483648.
However, I don't know the reason for becoming -2147483648.
It seems to be the same not only in C # but also in Java and so on.
Speaking of specifications, it's up to that point, but is there any rational reason?

a = (a * -1) * -1

Is it to make the formula hold?

Answer: Answer:

It is considered that the result is overflowing, not in a mathematical sense, and the value when viewed as a lower 32bit signed int is -2147483648.

If you switch to programmer mode with the calculator tool of Windows10, you can calculate 64bit, so please check it.

If you extend 32bit signed int to 64bit signed int, -2147483648 becomes 0xFFFFFFFF80000000. -1 is 0xFFFFFFFFFFFFFFFF, and when multiplied, the result is 0x0000000080000000.
In 64-bit operation, it is a positive number, 2147483648, but it looks like -2147483648 because the area for storing the answer is only 32bit.

Or, even if it is an operation between 32 bits, multiplying it by 0x80000000 and 0xFFFFFFFF,
The result is 0x7FFFFFFF80000000, but the lower 32 bits are the same 0x80000000 as above.
If you look at it as a 32-bit signed int, it will be -2147483648.

Scroll to Top