奇怪的Delphi整数乘法行为 [英] Strange Delphi integer multiplication behavior
问题描述
我正在处理一些古老的Delphi代码,遇到了一些我不太了解的东西.
I'm working on some ancient Delphi code and I've come across something which I don't quite understand.
[bla is set to 130245932]
outresult := ((bla * 1103516849) + 12359);
[outresult is equal to -413953101]
两个正数相乘如何得出一个负数?为什么当我将bla变量从方程式中取出而直接使用整数(像这样)
How does multiplying two positive numbers result in a negative number? And why is it that when I take the bla variable out of the equation and just use the integer directly (like this)
outresult := ((130245932 * 1103516849) + 12359);
在应用编译之前我收到一个错误
I receive an error before the app even compiles
[DCC Error] Unit1.pas(60): E2099 Overflow in conversion or arithmetic operation
一些天才将不胜感激.谢谢.
Some genius would be appreciated. Thanks.
推荐答案
好的,我将其作为答案.
Alright, I'll make this an answer.
错误消息应该很清楚.您在这里有整数溢出:
The error message should be pretty clear. You have an integer overflow here:
130245932 * 1103516849
因为130245932 * 1103516849 = 143728580475708268
太大,无法容纳32位整数.
because 130245932 * 1103516849 = 143728580475708268
which is too large to fit into a 32-bit integer.
这篇关于奇怪的Delphi整数乘法行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!