C语言 在 C 中存储和处理大数字

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2640625/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 05:10:17  来源:igfitidea点击:

Store and work with Big numbers in C

cexponentiation

提问by Martin Konecny

I need help working with very big numbers. According to Windows calc, the exponent

我需要帮助处理非常大的数字。根据 Windows calc,指数

174^55 = 1.6990597648061509725749329578093e+123 

How would I store this using C (c99 standard)?

我将如何使用 C(c99 标准)存储它?

int main(){
  long long int x = 174^55; //result is 153
  printf("%lld\n", x);
}

回答by AndiDog

Normal types in C can usually only store up to 64 bits, so you'll have to store big numbers in an array, for example, and write mathematical operations yourself. But you shouldn't reinvent the wheel here - you could try the GNU Multiple Precision Arithmetic Libraryfor this purpose.

C 中的普通类型通常最多只能存储 64 位,因此您必须将大数字存储在数组中,例如,自己编写数学运算。但是你不应该在这里重新发明轮子 - 你可以为此目的尝试GNU Multiple Precision Arithmetic Library

And as the comments already pointed out, the ^operation is binary XOR. For exponentiation, you will have to use mathematical functions like pow.

正如评论已经指出的那样,该^操作是二进制 XOR。对于求幂,您必须使用数学函数,例如pow.

回答by Michael Ekstrand

If approximation is OK, you can use floating-point (floator double) numbers. And you need pow, not ^, as the commenters said.

如果近似值没问题,您可以使用浮点 (floatdouble) 数。正如评论者所说,您需要pow,而不是^

However, for cryptography, approximation doesn't work. You need support for arithmetic with very large integers. GMP provides general multiple-precision arithmetic support. Many cryptographic packages will also have such algorithms in their code, either through a third-party library or built-in; PuTTY has a bignum library for large integers, and OpenSSL probably has something similar.

但是,对于密码学,近似不起作用。您需要支持非常大的整数算术。GMP 提供通用的多精度算术支持。许多加密包也将通过第三方库或内置库在其代码中包含此类算法;PuTTY 有一个用于大整数的 bignum 库,OpenSSL 可能也有类似的东西。

Basic C data types are not enough.

基本的 C 数据类型是不够的。

回答by Goz

You could store it in an array of integers. A 64-bit integer is just 2 32-bit integers. A 1024 bit integer could also be seen as 32 32-bit integers.

您可以将其存储在整数数组中。一个 64 位整数只是 2 个 32 位整数。1024 位整数也可以看作 32 个 32 位整数。