为什么我的 .net Int64 的行为就像 Int32 一样?
我在 .net 程序中目睹了一种奇怪的行为:
Console.WriteLine(Int64.MaxValue.ToString());
// displays 9223372036854775807, which is 2^63-1, as expected
Int64 a = 256*256*256*127; // ok
Int64 a = 256*256*256*128; // compile time error :
//"The operation overflows at compile time in checked mode"
// If i do this at runtime, I get some negative values, so the overflow indeed happens.
为什么我的 Int64 的行为就好像它们是 Int32 的一样,尽管 Int64.MaxValue 似乎确认它们正在使用 64 位?
如果相关的话,我使用的是 32 位操作系统,并且目标平台设置为“任何 CPU”
I'm witnessing a strange behavior in a .net program :
Console.WriteLine(Int64.MaxValue.ToString());
// displays 9223372036854775807, which is 2^63-1, as expected
Int64 a = 256*256*256*127; // ok
Int64 a = 256*256*256*128; // compile time error :
//"The operation overflows at compile time in checked mode"
// If i do this at runtime, I get some negative values, so the overflow indeed happens.
Why do my Int64's behaves as if they were Int32's, although Int64.MaxValue seems to confirm they're using 64 bits ?
If it's relevant, I'm using a 32 bit OS, and the target platform is set to "Any CPU"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的 RHS 仅使用
Int32
值,因此整个操作是使用Int32
算术执行的,然后是Int32
结果晋升为长。改成这样:
一切都会好起来的。
Your RHS is only using
Int32
values, so the whole operation is performed usingInt32
arithmetic, then theInt32
result is promoted to a long.Change it to this:
and all will be well.
使用:
L 后缀表示 Int64 字面量,无后缀表示 Int32。
你写的:
意思是:
Use:
the L suffix means Int64 literal, no suffix means Int32.
What your wrote:
means: