【问题标题】:Why does this return 0? [duplicate]为什么返回 0? [复制]
【发布时间】:2018-06-25 06:44:54
【问题描述】:
using System;
namespace test_warmup
{
    class Program
    {
        static void Main(string[] args)
        {
            int test = -1110835200;
            test = test * 1700397056;
            Console.WriteLine(test);
        }
    }
}

【问题讨论】:

    标签: c#


    【解决方案1】:

    -1110835200 * 1700397056 = -1888860903781171200

    以十六进制显示,即-0x1a3694fc_00000000

    在 C# 中,int is only 32-bits,因此结果被截断为 0。

    换句话说,该乘法的结果太大而无法放入您分配给它的变量中,并且适合的部分全为零。

    【讨论】:

    • 您可以通过简单地将类型更改为 long 来演示这种差异。 long test = -1110835200;
    猜你喜欢
    • 2017-06-13
    • 2011-01-09
    • 2012-08-09
    • 2019-12-15
    • 2018-04-14
    • 2017-02-10
    • 1970-01-01
    • 2021-12-30
    • 2012-02-04
    相关资源
    最近更新 更多