被肉体混淆10,然后除以10

get confused by mutlibly by 10 and then divide by 10

本文关键字:然后      更新时间:2023-10-16

我有点困惑,因为当我将int变量乘以10,然后将其除以10,我认为变量值不应该更改,但我得到了不同的结果,我缺少或我应该知道的东西这是代码

#include <iostream>
using namespace std;
int main()
{
   int intVar = 1500000000; //1,500,000,000
   intVar = (intVar * 10) / 10; //result too large
   cout << “intVar = “ << intVar << endl; //wrong answer
   return 0 ;
}

任何帮助解释,请

32位int的范围是-(1 << 32)(1 << 32) - 1

当15亿次乘以10时,它超过了int的上限(约为21亿),并溢出到其他数字,当该数字除以10,您将获得[新]数字的结果除以十。

相关文章: