I am writing a class called bigInts that deals with bigInts (dynamic arrays of shorts up to 40 digits) and I am having some trouble overloading the operators. I was able to get the addition one working with correct overflow, but subtraction is a different thing altogether. I was just wondering if anyone has any ideas on how to deal with subtraction underflow, I have an idea but since I am only a few months into my c++ journey nothing has been able to work yet.
7654231 - 7891234 = -237003 and the way to get this is to use a carryover flag.
Set carryover to 0, and change as required as the right-to-left calculation proceeds through the array
7 6 5 4 3 2 1
7 8 9 1 2 3 4 subtract
--------------
9 7 6 2 9 9 7 with the final carryover flag value 1
--------------
Now, because the flag is 1, that result is the one's complement of the answer
1 0 0 0 0 0 0 0
0 9 7 6 2 9 9 7 subtract
----------------
0 0 2 3 7 0 0 3 which is -ve because the flag is 1
----------------
Big integer Addition and Subtraction are easy, Multiplication is not too bad but Division is a challenge.
I now see from your other posts and the advice given that the concept of a carry should be no surprise, especially if you had tried the exercise on paper, which is fairly early learning in slightly advanced arithmetic.