gpt4 book ai didi

c# - -1 * int.MinValue 给出错误

转载 作者:太空宇宙 更新时间:2023-11-03 17:15:38 25 4
gpt4 key购买 nike

当我写这段代码的时候

Console.WriteLine(-1 * int.MinValue);

int a = -1 * -2147483648;

我得到一个错误

The operation overflows at compile time in checked mode.

是否有专门针对此值的编译时检查?还是实时计算所有书面表达?

最佳答案

编译器能够在编译时而不是运行时执行(乘法)计算,所以它确实如此。

由于 -1 * int.MinValue(-1 * -2,147,483,648,因此为 2,147,483,648)大于 int.MaxValue(2,147,483,647),因此会出现编译错误。

要证明它不是特殊的外壳,请尝试:

Console.WriteLine(-2 * 1073741826);

你会得到同样的行为。

关于c# - -1 * int.MinValue 给出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8256361/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com