Why int.MinValue% -1 raises and OverflowException

B 7.8.3. the C # spec regarding the Remainder statement, it states the following:

If the left operand is the smallest int or long, and the right operand is -1, a System.OverflowException is thrown.

Therefore, int.MinValue % -1 will throw an OverflowException. I'm trying to understand why?

+5
source share
1 answer

In two-parameter arithmetic, data types range from (-2 ** n) to (2 ** n - 1) (where "n" is 1 less than the number of bits in the data type).

For example, a 16-bit signed integer has a valid range from -32768 (-2 ** 15) to 32767 (2 ** 15 - 1).

-32768 / -1 = +32768, which exceeds the valid range for a signed 16-bit integer.

+6
source

Source: https://habr.com/ru/post/1236859/


All Articles