How to handle Big Integer in C++

Oct 11, 2012 at 2:34pm
I want to handle Big Integer (greater than 2^63) in C++. I know that, I need to use string to perform it. But I have no idea about how to write a program which can handle a big integer. Can anyone please help me about handling big integer?
Last edited on Oct 11, 2012 at 2:35pm
Oct 11, 2012 at 2:47pm
Do you want to write it yourself, or do you just need to use it? Most people just use one of the existing bignum libraries.
Oct 11, 2012 at 3:03pm
I don't know is C++ does not support any bignum libraries or not. So I want to write it by myself.
Oct 11, 2012 at 3:12pm
There are many bignum libraries for C++. If you want to write it by yourself, good for you.

Get a bit of paper.
Write out a big number.
Now write out another big number underneath it.
Add them together.

See how you added them together? Now program that. This is your addition code.

Repeat for subtraction, division and multiplication.
Oct 11, 2012 at 4:14pm
I know how to handle big number addition, subtraction, multiplication and division. But I am frustrate to handle just only one big number with a size up to 2^63. I understand that 1st I need to declare a string variable, than work with the size of the variable. I need to convert the string variable value into integer and finally save the results of my desire work in a string variable. But how can I convert the value of a string into integer (no need to use atoi() function)? How can I convert the results of my work from integer to string?
Oct 11, 2012 at 4:15pm
Just one example is the MAPM library which I've used.
http://www.tc.umn.edu/~ringx004/mapm-main.html

The main body of the code is written in C, but that really makes no difference, as there's a C++ interface.

To use it you do have to download the source and build the library. Once that's done (a one-off task) you simply include one header file in your program, and add the lib to the linker files.

I'm not claiming this is the best, merely that I've used it and it works well.

This library can handle integers or floating-point values, as well as having many standard mathematical functions (sin, log etc).
Last edited on Oct 11, 2012 at 4:18pm
Oct 11, 2012 at 4:37pm
But how can I convert the value of a string into integer (no need to use atoi() function)?


Don't. You already said that you cannot store the value as an integer, so converting it to an int would be pointless.

Work on them one digit at a time, and convert each letter to a number as you work on it, and then convert it back again to a letter for the answer string after the calculation is done.

To convert a single char to a number, subtract 48. To convert a single digit number to a char, add 48.
Last edited on Oct 11, 2012 at 4:39pm
Oct 11, 2012 at 4:51pm
Googling "bignum C++" would be productive.
Topic archived. No new replies allowed.