Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement big int in C++

I'd like to implement a big int class in C++ as a programming exercise—a class that can handle numbers bigger than a long int. I know that there are several open source implementations out there already, but I'd like to write my own. I'm trying to get a feel for what the right approach is.

I understand that the general strategy is get the number as a string, and then break it up into smaller numbers (single digits for example), and place them in an array. At this point it should be relatively simple to implement the various comparison operators. My main concern is how I would implement things like addition and multiplication.

I'm looking for a general approach and advice as opposed to actual working code.

like image 584
oneself Avatar asked Nov 06 '08 16:11

oneself


People also ask

What is BigInt in 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 is Big integer implemented?

To create a new data type of big integers following concepts are being implemented: C++ strings in that we can store our numbers in the form of characters (in reverse order for efficiency purposes) such that using strings we can store very big numbers also.

How do you handle large integers in C++?

Handling large 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

A fun challenge. :)

I assume that you want integers of arbitrary length. I suggest the following approach:

Consider the binary nature of the datatype "int". Think about using simple binary operations to emulate what the circuits in your CPU do when they add things. In case you are interested more in-depth, consider reading this wikipedia article on half-adders and full-adders. You'll be doing something similar to that, but you can go down as low level as that - but being lazy, I thought I'd just forego and find a even simpler solution.

But before going into any algorithmic details about adding, subtracting, multiplying, let's find some data structure. A simple way, is of course, to store things in a std::vector.

template< class BaseType > class BigInt { typedef typename BaseType BT; protected: std::vector< BaseType > value_; }; 

You might want to consider if you want to make the vector of a fixed size and if to preallocate it. Reason being that for diverse operations, you will have to go through each element of the vector - O(n). You might want to know offhand how complex an operation is going to be and a fixed n does just that.

But now to some algorithms on operating on the numbers. You could do it on a logic-level, but we'll use that magic CPU power to calculate results. But what we'll take over from the logic-illustration of Half- and FullAdders is the way it deals with carries. As an example, consider how you'd implement the += operator. For each number in BigInt<>::value_, you'd add those and see if the result produces some form of carry. We won't be doing it bit-wise, but rely on the nature of our BaseType (be it long or int or short or whatever): it overflows.

Surely, if you add two numbers, the result must be greater than the greater one of those numbers, right? If it's not, then the result overflowed.

template< class BaseType > BigInt< BaseType >& BigInt< BaseType >::operator += (BigInt< BaseType > const& operand) {   BT count, carry = 0;   for (count = 0; count < std::max(value_.size(), operand.value_.size(); count++)   {     BT op0 = count < value_.size() ? value_.at(count) : 0,         op1 = count < operand.value_.size() ? operand.value_.at(count) : 0;     BT digits_result = op0 + op1 + carry;     if (digits_result-carry < std::max(op0, op1)     {       BT carry_old = carry;       carry = digits_result;       digits_result = (op0 + op1 + carry) >> sizeof(BT)*8; // NOTE [1]     }     else carry = 0;   }    return *this; } // NOTE 1: I did not test this code. And I am not sure if this will work; if it does //         not, then you must restrict BaseType to be the second biggest type  //         available, i.e. a 32-bit int when you have a 64-bit long. Then use //         a temporary or a cast to the mightier type and retrieve the upper bits.  //         Or you do it bitwise. ;-) 

The other arithmetic operation go analogous. Heck, you could even use the stl-functors std::plus and std::minus, std::times and std::divides, ..., but mind the carry. :) You can also implement multiplication and division by using your plus and minus operators, but that's very slow, because that would recalculate results you already calculated in prior calls to plus and minus in each iteration. There are a lot of good algorithms out there for this simple task, use wikipedia or the web.

And of course, you should implement standard operators such as operator<< (just shift each value in value_ to the left for n bits, starting at the value_.size()-1... oh and remember the carry :), operator< - you can even optimize a little here, checking the rough number of digits with size() first. And so on. Then make your class useful, by befriendig std::ostream operator<<.

Hope this approach is helpful!

like image 196
mstrobl Avatar answered Sep 23 '22 00:09

mstrobl