Store and work with Big numbers in C

33,606

Solution 1

Normal types in C can usually only store up to 64 bits, so you'll have to store big numbers in an array, for example, and write mathematical operations yourself. But you shouldn't reinvent the wheel here - you could try the GNU Multiple Precision Arithmetic Library for this purpose.

And as the comments already pointed out, the ^ operation is binary XOR. For exponentiation, you will have to use mathematical functions like pow.

Solution 2

If approximation is OK, you can use floating-point (float or double) numbers. And you need pow, not ^, as the commenters said.

However, for cryptography, approximation doesn't work. You need support for arithmetic with very large integers. GMP provides general multiple-precision arithmetic support. Many cryptographic packages will also have such algorithms in their code, either through a third-party library or built-in; PuTTY has a bignum library for large integers, and OpenSSL probably has something similar.

Basic C data types are not enough.

Solution 3

You could store it in an array of integers. A 64-bit integer is just 2 32-bit integers. A 1024 bit integer could also be seen as 32 32-bit integers.

Share:
33,606
Martin Konecny
Author by

Martin Konecny

Software-developer for Lucova. Some of my favourite answers: http://stackoverflow.com/a/34823421 http://stackoverflow.com/a/30362874 http://stackoverflow.com/a/24007536 http://stackoverflow.com/a/16641452

Updated on October 01, 2020

Comments

  • Martin Konecny
    Martin Konecny over 3 years

    I need help working with very big numbers. According to Windows calc, the exponent

    174^55 = 1.6990597648061509725749329578093e+123 
    

    How would I store this using C (c99 standard)?

    int main(){
      long long int x = 174^55; //result is 153
      printf("%lld\n", x);
    }