When i was coding i got a problem like: i need to output number which is 2^64 (also 3^64 and others). I have tried unsigned long long int, but it's still not enough (it contains 2^64-1). If anyone know how i can output numbers from 64bits to 128bits, just let me know (btw this task i'm trying to do on C).
Asked
Active
Viewed 208 times
-1
-
1That could be an XY-problem. Do you have a problem statement and your proposed solution? – Eugene Sh. Sep 15 '21 at 17:51
-
c++ has `__int128` but for c you might have to write your own custom logic – Rishabh Deep Singh Sep 15 '21 at 17:52
-
2@RishabhDeepSingh `__int128` is a gcc extension and works in c or c++ – Alan Birtles Sep 15 '21 at 17:53
-
4^64 is outside 128-bit integer range. `2^64, 4^64` are available as `double`. What is the real problem? – chux - Reinstate Monica Sep 15 '21 at 17:53
-
@AlanBirtles didn't knew it works on c as well. But it doesn't has output operator so you might to define it yourself. something like https://stackoverflow.com/questions/25114597/how-to-print-int128-in-g – Rishabh Deep Singh Sep 15 '21 at 17:55
-
A list of arbitrary precision libraries can be found here: https://en.wikipedia.org/wiki/List_of_arbitrary-precision_arithmetic_software – mevets Sep 15 '21 at 18:05
-
https://en.wikipedia.org/wiki/GNU_Multiple_Precision_Arithmetic_Library – 0___________ Sep 15 '21 at 18:06
-
2^64=10000...(64 0s) in binary. From there it's just a matter of converting to decimal while storing it as a string. Same for 3^64, 4^64 etc, but instead of binary use base 3, base 4 etc – DarkAtom Sep 15 '21 at 18:10
-
Please provide enough code so others can better understand or reproduce the problem. – Community Sep 22 '21 at 10:09
1 Answers
1
First, compile this program:
#include <stdint.h>
int main(void)
{
return (int128_t) 0;
}
If the compiler prints an error message about the identifier int128_t
, then your compiler probably does not support integers wider than 64 bits, regardless of what the type name is.
In that case, you have at least three choices:
- If you do not need exact results, you can use
double
orlong double
floating-point, as inprintf("%g\n", pow(2, 64));
. - You can write your own code to implement 128-bit arithmetic from smaller types. For short, simple programs, you can use the same techniques taught in elementary school for multiplying and adding numbers digit by digit, just using a type like
uint32_t
as a “digit” instead of a single decimal digit. Or you can implement them using single decimal digits, if you prefer. (For example, twouint32_t
numbers can be converted touint64_t
and multiplied to make a 64-bit product which you then split into oneuint32_t
digit to be kept in the current position and oneuint32_t
digit to be carried to the next position.) - Use an arbitrary-precision library, such as the GNU Multiple Precision Arithmetic Library. This will require downloading and configuring additional software.

Eric Postpischil
- 195,579
- 13
- 168
- 312