如何使用gcc __uint128_t号打印? [英] how to print __uint128_t number using gcc?
问题描述
有 PRIu128
,其行为类似于从&LT到
: PRIu64
; inttypes.h>
Is there PRIu128
that behaves similar to PRIu64
from <inttypes.h>
:
printf("%" PRIu64 "\n", some_uint64_value);
或数字转换手动位数:
int print_uint128(uint128_t n) {
if (n == 0) return printf("0\n");
char str[40] = {0}; // log10(1 << 128) + '\0'
char *s = str + sizeof(str) - 1; // start at the end
while (n != 0) {
if (s == str) return -1; // never happens
*--s = "0123456789"[n % 10]; // save last digit
n /= 10; // drop it
}
return printf("%s\n", s);
}
是唯一的选择?
注意 uint128_t
是我自己的类型定义 __ uint128_t
。
Note that uint128_t
is my own typedef for __uint128_t
.
推荐答案
没有有没有在库支持打印这些类型。在C标准的意义上,它们甚至没有扩展整型。
No there isn't support in the library for printing these types. They aren't even extended integer types in the sense of the C standard.
您启动从背面印刷的想法是好的,但你可以使用更大的块。在某些测试中的P99我有一个使用
Your idea for starting the printing from the back is a good one, but you could use much larger chunks. In some tests for P99 I have such a function that uses
uint64_t const d19 = UINT64_C(10000000000000000000);
作为适合10最大功率的 uint64_t中
。
由于小数,这些数字大得无法阅读很快使另一个更简单,选择是打印出来的十六进制。然后,你可以做这样的事情。
As decimal, these big numbers get unreadable very soon so another, easier, option is to print them in hex. Then you can do something like
uint64_t low = (uint64_t)x;
// This is UINT64_MAX, the largest number in 64 bit
// so the longest string that the lower half can occupy
char buf[] = { "18446744073709551615" };
sprintf(buf, "%" PRIX64, low);
获得下半部,然后基本上是相同的用
to get the lower half and then basically the same with
uint64_t high = (x >> 64);
有上半部分
这篇关于如何使用gcc __uint128_t号打印?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!