显示非常大的数字

发布于 2024-12-12 07:11:53 字数 184 浏览 0 评论 0原文

我们知道4294967295是unsigned int中最大的数字,如果我把这个数字乘以它自己,那么如何显示它呢?我已经尝试过:

long unsigned int NUMBER = 4294967295 * 4294967295;

但仍然得到 1 作为答案。

As we know that 4294967295 is the largest number in unsigned int if I multiply this number by itself then how to display it? I have tried:

long unsigned int NUMBER = 4294967295 * 4294967295;

but still getting 1 as answer.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

云醉月微眠 2024-12-19 07:11:54

乘法溢出。

#include <stdio.h>
int main()
{
    unsigned int a = 4294967295;
    unsigned int b = 4294967295;

    // force to perform multiplication based on larger type than unsigned int
    unsigned long long NUMBER = (unsigned long long)a * b;
    printf("%llu\n", NUMBER);
}

The multiplication overflows.

#include <stdio.h>
int main()
{
    unsigned int a = 4294967295;
    unsigned int b = 4294967295;

    // force to perform multiplication based on larger type than unsigned int
    unsigned long long NUMBER = (unsigned long long)a * b;
    printf("%llu\n", NUMBER);
}
明明#如月 2024-12-19 07:11:54

您在问题中声明您知道 max int 等于 4294967295。这意味着如果您使用 unsigned int,则无法存储大于该数字的数字。

在 64 位 unix 系统上未签名时,C long 最多可存储 18,446,744,073,709,551,615 [source] 所以你只需要在你的号码后面加上 UL 后缀: 4294967295UL

如果您使用的不是 64 位 UNIX 系统,那么您应该使用 long long unsigned int 并带有 LL 后缀

You state in your question that you know max int is equal to 4294967295. That means that you can't store a number larger than that if you are using unsigned int.

C longs store up to 18,446,744,073,709,551,615 when unsigned on a 64 bit unix system [source] so you need only suffix your numbers with UL : 4294967295UL

If you aren't using a 64-bit unix system then you should use long long unsigned int and suffix with LL

赢得她心 2024-12-19 07:11:54

是的,这是溢出。如果您使用c,则没有任何简单的方法可以进行如此大的数字乘法,正如我所知。也许你需要自己写一份。事实上,有些语言本来就支持这样的功能。

Yes, it's an overflow. If you are using c, there isn't any easy way to do such big number multiply as i knew. Maybe you need write one by yourself. In fact some language support such features originally.

苍景流年 2024-12-19 07:11:53

你正在溢出。考虑十六进制的乘法:

0xffffffff * 0xffffffff == 0xfffffffe00000001
                                     ^^^^^^^^
                                     only the last 32 bits are returned

解决方案是使用更大的类型,例如long long unsigned

long long unsigned int NUMBER = 4294967295ULL * 4294967295ULL;

后缀ULL表示unsigned long long

查看它在线运行:ideone

You are getting an overflow. Consider the muplication in hexadecimal:

0xffffffff * 0xffffffff == 0xfffffffe00000001
                                     ^^^^^^^^
                                     only the last 32 bits are returned

The solution is to use a larger type such as long long unsigned:

long long unsigned int NUMBER = 4294967295ULL * 4294967295ULL;

The suffix ULL means unsigned long long.

See it working online: ideone

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文