unsigned_int64 的意外行为;
unsigned__int64 difference;
difference=(64*33554432);
printf ("size %I64u \n", difference);
difference=(63*33554432);
printf ("size %I64u \n", difference);
第一个 # 太大了。第二个数字是正确答案。从62改为63是如何引起这样的变化的呢?
第一个值为 18446744071562067968 第二个值是 2113929216
抱歉,值是 64 和 63,而不是 63 和 62。
unsigned__int64 difference;
difference=(64*33554432);
printf ("size %I64u \n", difference);
difference=(63*33554432);
printf ("size %I64u \n", difference);
the first # is ridiculously large. The second number is the correct answer. How does changing it from 62 to 63 cause such a change?
First value is 18446744071562067968
Second value is 2113929216
Sorry the values were 64 and 63, not 63 and 62.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
除非另有限定,否则整数文字的类型为
int
。我假设在您所在的平台上,int
是 32 位的。因此计算(64*33554432)
溢出并变为负值。然后,您将其转换为unsigned __int64
,因此现在它会翻转回一个非常非常大的正整数。瞧:
Unless qualified otherwise, integer literals are of type
int
. I would assume that on the platform you're on, anint
is 32-bit. So the calculation(64*33554432)
overflows and becomes negative. You then cast this to aunsigned __int64
, so this now gets flipped back to a very very large positive integer.Voila:
在 gcc 上它工作得很好并且在两种情况下都给出了正确的数字。
尺码 2113929216
size 2080374784
这可能是 printf 的一个错误吗?
您使用的是 MSVC 或类似的吗?尝试逐步通过调试器并在每次评估后检查差异。如果数字看起来正确,那么可能只是 printf 问题。然而,在linux上的gcc下它是正确的。
On gcc it works fine and gives out the correct number in both cases.
size 2113929216
size 2080374784
Could it be a bug with printf?
Are you using MSVC or similar? try stepping it through the debugger and inspect difference after each evaluation. If the numbers look right there then it might just be a printf problem. However, under gcc on linux it's correct.