【发布时间】:2011-02-14 09:38:46
【问题描述】:
我正在编写一个 bignum 库,我想使用高效的数据类型来表示数字。特别是整数表示数字,long(如果严格是整数大小的两倍)用于加法和乘法时的中间表示。
我将使用一些 C99 功能,但尝试符合 ANSI C。
目前我的 bignum 库中有以下内容:
#include <stdint.h>
#if defined(__LP64__) || defined(__amd64) || defined(__x86_64) || defined(__amd64__) || defined(__amd64__) || defined(_LP64)
typedef uint64_t u_w;
typedef uint32_t u_hw;
#define BIGNUM_DIGITS 2048
#define U_HW_BITS 16
#define U_W_BITS 32
#define U_HW_MAX UINT32_MAX
#define U_HW_MIN UINT32_MIN
#define U_W_MAX UINT64_MAX
#define U_W_MIN UINT64_MIN
#else
typedef uint32_t u_w;
typedef uint16_t u_hw;
#define BIGNUM_DIGITS 4096
#define U_HW_BITS 16
#define U_W_BITS 32
#define U_HW_MAX UINT16_MAX
#define U_HW_MIN UINT16_MIN
#define U_W_MAX UINT32_MAX
#define U_W_MIN UINT32_MIN
#endif
typedef struct bn
{
int sign;
int n_digits; // #digits should exclude carry (digits = limbs)
int carry;
u_hw tab[BIGNUM_DIGITS];
} bn;
由于我还没有编写将 bignum 写入十进制的程序,因此我必须分析中间数组并打印每个数字的值。但是我不知道 printf 使用哪个转换说明符。最好我想将十六进制编码的数字写入终端。
根本问题是,我想要两种数据类型,一种是另一种的两倍,然后使用标准转换说明符进一步将它们与 printf 一起使用。如果 int 是 32 位并且 long 是 64 位,那将是理想的,但我不知道如何使用预处理器来保证这一点,并且当需要使用诸如 printf 之类的仅依赖于标准类型的函数时,我不再知道要做什么使用。
【问题讨论】: