In 7.8.3. of the C# Specification regarding the Remainder operator it states the following:
If the left operand is the smallest int or long value and the right operand is -1, a System.OverflowException is thrown.
Therefore int.MinValue % -1
would result in an OverflowException. I am trying to understand why?
In two's complement arithmetic, data types have a 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 16-bit signed integer.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With