This should be an easy question... 128-bit blues.
-
Last time I did hardcore C was a while back. Before the 128-bit days. Ok, cool. But, I got a silly question when it comes to printing a 128-bit integer. You see online examples saying just do a `long long` cast and call it a day bro, but then they use small numbers. Which obviously works for them because it's a small number. But, I figure hey, I'll try it for poops and giggles. As you'd might expect the number is never correct.
#include
int main()
{
__uint128_t u128 = 34028236692093846346337460743176821145LL;
printf("%llu\n", (unsigned long long)u128);return 0;
}
The above don't do it. Now, I could bit shift to get around any limits, but I ultimately need the output formatted with comma separators, so doing bit logic would cause issues when putting humpty dumpty back together again. So, um... anyone know how to print a 128-bit number in C? Preferably portable C.
Jeremy Falcon
-
Last time I did hardcore C was a while back. Before the 128-bit days. Ok, cool. But, I got a silly question when it comes to printing a 128-bit integer. You see online examples saying just do a `long long` cast and call it a day bro, but then they use small numbers. Which obviously works for them because it's a small number. But, I figure hey, I'll try it for poops and giggles. As you'd might expect the number is never correct.
#include
int main()
{
__uint128_t u128 = 34028236692093846346337460743176821145LL;
printf("%llu\n", (unsigned long long)u128);return 0;
}
The above don't do it. Now, I could bit shift to get around any limits, but I ultimately need the output formatted with comma separators, so doing bit logic would cause issues when putting humpty dumpty back together again. So, um... anyone know how to print a 128-bit number in C? Preferably portable C.
Jeremy Falcon
As far as I know, the C standard does not specify anything about 128 bit integers. _uint128_t is a GCC extension, also supported by clang. Maybe MSVC too? As far as I know, there's no printf format code for 128 bit integers either. See here for a possible solution: [https://stackoverflow.com/questions/11656241/how-to-print-uint128-t-number-using-gcc\](https://stackoverflow.com/questions/11656241/how-to-print-uint128-t-number-using-gcc)
"A little song, a little dance, a little seltzer down your pants" Chuckles the clown
-
As far as I know, the C standard does not specify anything about 128 bit integers. _uint128_t is a GCC extension, also supported by clang. Maybe MSVC too? As far as I know, there's no printf format code for 128 bit integers either. See here for a possible solution: [https://stackoverflow.com/questions/11656241/how-to-print-uint128-t-number-using-gcc\](https://stackoverflow.com/questions/11656241/how-to-print-uint128-t-number-using-gcc)
"A little song, a little dance, a little seltzer down your pants" Chuckles the clown
Yeah that's what I was afraid of. If it wasn't for the formatting, I'd just slice it up as two 64s to be portable for printf. I may just have to roll my own formatting for something I'm working on. I mean, I could just forget about 128-bit support, but no cool points for that. :laugh: Thanks btw.
Jeremy Falcon
-
Last time I did hardcore C was a while back. Before the 128-bit days. Ok, cool. But, I got a silly question when it comes to printing a 128-bit integer. You see online examples saying just do a `long long` cast and call it a day bro, but then they use small numbers. Which obviously works for them because it's a small number. But, I figure hey, I'll try it for poops and giggles. As you'd might expect the number is never correct.
#include
int main()
{
__uint128_t u128 = 34028236692093846346337460743176821145LL;
printf("%llu\n", (unsigned long long)u128);return 0;
}
The above don't do it. Now, I could bit shift to get around any limits, but I ultimately need the output formatted with comma separators, so doing bit logic would cause issues when putting humpty dumpty back together again. So, um... anyone know how to print a 128-bit number in C? Preferably portable C.
Jeremy Falcon
Have you looked at GMP? It's not for the faint of heart but it should do what you want - Formatted Output Strings (GNU MP 6.3.0)[^]
Mircea
-
Have you looked at GMP? It's not for the faint of heart but it should do what you want - Formatted Output Strings (GNU MP 6.3.0)[^]
Mircea
Never even heard of it. :omg: Does it handle localization? If so, you're totally my hero.
Jeremy Falcon
-
Never even heard of it. :omg: Does it handle localization? If so, you're totally my hero.
Jeremy Falcon
It should. Per above reference:
Quote:
The decimal point character (or string) is taken from the current locale settings on systems which provide localeconv (see Locales and Internationalization in The GNU C Library Reference Manual). The C library will normally do the same for standard float output.
Mircea
-
It should. Per above reference:
Quote:
The decimal point character (or string) is taken from the current locale settings on systems which provide localeconv (see Locales and Internationalization in The GNU C Library Reference Manual). The C library will normally do the same for standard float output.
Mircea
Schweet. I'll have to check it out. Thanks man.
Jeremy Falcon
-
Schweet. I'll have to check it out. Thanks man.
Jeremy Falcon
One is glad to be of service.
Mircea