A fascinating task. :)
I assume you need integers of arbitrary length. I propose the following approach:
Consider the binary character of type "int". Think about how to use simple binary operations to emulate what circuits do on your CPU when they add things. If you are interested in more detail, consider reading this wikipedia article on semi-qualifiers and incremental adders . You will do something similar to this, but you can go to such a low level, but being lazy I thought that I would simply refuse and find an even simpler solution.
But before going into any algorithmic details about adding, subtracting, multiplying, we find some data structure. An easy way, of course, is to store things in std :: vector.
template< class BaseType > class BigInt { typedef typename BaseType BT; protected: std::vector< BaseType > value_; };
You might want to consider whether you want to make a vector of a fixed size and if it pre-selects it. The reason is that for various operations you have to go through each element of the vector - O (n). You might want to know how complicated the operation is, and fixed n does just that.
But now to some algorithms for working by numbers. You can do this at a logical level, but we will use this magic power of the processor to calculate the results. But what we take from the logical illustration of Half- and FullAdders is how it relates to hyphenation. As an example, consider how to implement the + = operator. For each number in BigInt <> :: value_, you must add them and see if the result leads to some form of transfer. We will not do this in different ways, but rely on the nature of our BaseType (whether it be long or int or short or any other): it overflows.
Of course, if you add two numbers, the result should be more than more than one of these numbers, right? If it is not, the result overflows.
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;
Another arithmetic operation is similar. Hell, you could even use stl-functors std :: plus and std :: minus, std :: times and std :: dives, ... but remember to carry over. :) You can also implement multiplication and division using its pros and cons, but itβs very slow, because it will recount the results that you already calculated in previous calls plus and minus at each iteration. There are many good algorithms for this simple task, use wikipedia or the Internet.
And, of course, you must implement standard operators such as operator<< (just shift each value in value_.size()-1 to the left for n bits, starting with value_.size()-1 ... oh and remember the transfer :), operator< - you can even optimize a bit here by checking the rough number of size() digits first. And so on. Then make your class useful by making friends with std :: ostream operator<< .
We hope that this approach will be useful!