gpt4 book ai didi

c - 两个长数相乘

转载 作者:太空狗 更新时间:2023-10-29 15:44:58 26 4
gpt4 key购买 nike

我尝试通过 C 程序乘以数字,即 1000010000 + 1。但是我没有得到正确的输出。

printf("%lld",(100000)*(100001));

我已经在不同的编译器上尝试了上面的代码,但我得到的是相同的 1410165408 而不是 10000100000

最佳答案

好吧,让我们相乘

  int64_t a = 100000;
int64_t b = 100001;
int64_t c = a * b;

我们会得到(二进制)

     1001010100000011010110101010100000 /* 10000100000 decimal */

但是如果你把它转换成int32_t

  int32_t d = (int32_t) c;

您将只得到最后 32 位(丢弃最前面的 10):

       01010100000011010110101010100000 /* 1410165408 decimal */

最简单的方法可能是将两个常量都声明为 64 位 值(LL 后缀代表 long long):

  printf("%lld",(100000LL)*(100001LL));  

关于c - 两个长数相乘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43321953/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com