I have started writing a bignum library, with a vector of shorts to represent the value, a print function, and negative number support. However, I cannot find a good way to implement long addition, like this:
123
+123
----
246
The latest code I have that doesn't give a segfault is this:
void add(unsigned long long b)
{
for(long long i=v.size()-1;i>=0;--i)
{
if((b+v[i])<10)
v[i]+=b;
else // Carry
{
if(i==0) // 1st digit
{
v.push_front(1); // Can't be more than 1
}
else
v[i-1]++; // Increment digit to the left
}
}
}
, but addition with a carry is not correct (10+1 is 21)
EDIT: It is implemented as a class