big integer in c++

How to implement big integer in c++?? e.g for operations like 10^12 modulo 1000000007


10^12 fits in a long long.


There is no in built library for implementing Big Integer in C++.The max value of integer that fits in a c++ datatype(unsigned long long) is 2^64-1(ie 18446744073709551615).

So if you want to evaluate number bigger than this and you are a c++ coder,
then only way is to store the value in form of character array and perform operation (addition,subtraction,multiplication),on each digits like you did in elementary high school.

But if you are a java coder ,then you are quite lucky,java Maths.BigInteger class has such features and most of the coder use it these days(and i would also recommend you to use it).Python has also got such feature.Hope this helps!


For modulo operation utilise the theorems: (a%m+b%m)%m=(a+b)%m and (a%mb%m)%m=(ab)%m. If exponent is too high you can do repeated squaring, a^n=(a^(n/2))^2 (n is even).

1 Like