Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ Big Integer

Tags:

I'm trying to implement a BigInteger Class in C++. But, first of all, I've a base question, how the "base data" can be represented? For example, the most stupid way is to have a fixed (or dynamic) array of char and store each single number of an integer in a char. But, ok, this is a very stupid way, and I'm here for your suggestions.

like image 651
Andrea Avatar asked Dec 22 '10 07:12

Andrea


People also ask

What is big integer C?

The BIGINT data type is a machine-independent method for representing numbers in the range of -2 63-1 to 2 63-1. ESQL/C provides routines that facilitate the conversion from the BIGINT data type to other data types in the C language. The BIGINT data type is internally represented with the ifx_int8_t structure.

How big is a big integer?

A big integer is a binary integer with a precision of 63 bits. The range of big integers is -9223372036854775808 to +9223372036854775807.

How do you store big integers?

Below are the steps: Take the large number as input and store it in a string. Create an integer array arr[] of length same as the string size. Iterate over all characters (digits) of string str one by one and store that digits in the corresponding index of the array arr.

How do you use big numbers in C++?

In C++, we can use large numbers by using the boost library. This C++ boost library is widely used library. This is used for different sections. It has large domain of applications.


1 Answers

There are a bunch of suggestions here for existing implementations: C++ handling very large integers

If you have to implement your own (e.g. for homework), then you have to decide the best way, and how "big" you need to handle. You could use an array of DWORDs, and handle overflowing from one to the next.

Although, for some of the Project Euler stuff, I actually implemented a BigNumber class built on a string. It turned out to be the simplest to implement for +-*/, and scaled to significantly longer numbers than I could get with a few unsigned long longs. And the performance was perfectly adequate for solving those puzzles.

So, you face a tradeoff between ease of implementation and optimal performance. Have fun ;-)

like image 55
Tim Avatar answered Sep 16 '22 17:09

Tim