为什么 int.MinValue % -1 会导致 OverflowException
Why does int.MinValue % -1 cause and OverflowException
在 7.8.3 中。关于余数运算符的 C# 规范说明如下:
If the left operand is the smallest int or long value and the right
operand is -1, a System.OverflowException is thrown.
因此 int.MinValue % -1
将导致 OverflowException。我想知道为什么?
在二进制补码运算中,数据类型的范围从 (-2**n) 到 (2**n - 1)(其中 'n' 比数据中的位数少 1类型)。
例如,16 位有符号整数的有效范围为 -32768 (-2**15) 到 32767 (2**15 - 1)。
-32768 / -1 = +32768 超出了 16 位有符号整数的有效范围。
在 7.8.3 中。关于余数运算符的 C# 规范说明如下:
If the left operand is the smallest int or long value and the right operand is -1, a System.OverflowException is thrown.
因此 int.MinValue % -1
将导致 OverflowException。我想知道为什么?
在二进制补码运算中,数据类型的范围从 (-2**n) 到 (2**n - 1)(其中 'n' 比数据中的位数少 1类型)。
例如,16 位有符号整数的有效范围为 -32768 (-2**15) 到 32767 (2**15 - 1)。
-32768 / -1 = +32768 超出了 16 位有符号整数的有效范围。