<分区>
在 7.8.3 中。关于 Remainder 运算符的 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。我想了解为什么?
<分区>
在 7.8.3 中。关于 Remainder 运算符的 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 位有符号整数的有效范围。
关于c# - 为什么 int.MinValue % -1 会导致 OverflowException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33928048/