-1

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).

Alan Birtles
  • 32,622
  • 4
  • 31
  • 60

1 Answers1

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 or long double floating-point, as in printf("%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, two uint32_t numbers can be converted to uint64_t and multiplied to make a 64-bit product which you then split into one uint32_t digit to be kept in the current position and one uint32_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