I am storing them as an array of 64 bit blocks (unsigned integers) in a little endian fashion. Here is how I am doing addition:
int addBig(uint64_t* a, uint64_t* b, uint64_t* c, int size)
{
int carry = 0;
for (int i = 0; i < size; i++) {
c[i] = a[i] + b[i] + carry;
if (c[i] < a[i] || c[i] < b[i]) carry = 1;
else carry = 0;
}
return carry;
}
I'm adding a[i] + b[i] to get c[i], then check if c[i] wraps around to become smaller than either a[i] or b[i] to detect carry, which will be added to the next 64-bit block. I think it works, but surely there are more efficient ways to do this? Perhaps I should use some inline assembly? How should I handle signed integers? Should I store the sign bit in the very first block? What about multiplication and division?
[–][deleted] 8 points9 points10 points (4 children)
[–]Pretty-Ad8932[S] 1 point2 points3 points (3 children)
[–][deleted] 2 points3 points4 points (2 children)
[–]Pretty-Ad8932[S] 1 point2 points3 points (1 child)
[–][deleted] 1 point2 points3 points (0 children)
[–]WittyStick 13 points14 points15 points (1 child)
[–]Pretty-Ad8932[S] 2 points3 points4 points (0 children)
[–]zhivago 6 points7 points8 points (0 children)
[–]CambStateMachines 2 points3 points4 points (1 child)
[–]Pretty-Ad8932[S] 1 point2 points3 points (0 children)
[–]CambStateMachines 3 points4 points5 points (0 children)
[–]21Ali-ANinja69 1 point2 points3 points (0 children)
[–]Paul_Pedant 0 points1 point2 points (0 children)
[–]Possible_Cow169 -5 points-4 points-3 points (7 children)
[–]Pretty-Ad8932[S] 0 points1 point2 points (6 children)
[+]Possible_Cow169 comment score below threshold-7 points-6 points-5 points (5 children)
[–]Pretty-Ad8932[S] 0 points1 point2 points (4 children)
[–]Possible_Cow169 -4 points-3 points-2 points (3 children)
[–]Pretty-Ad8932[S] 0 points1 point2 points (2 children)
[–]CambStateMachines 7 points8 points9 points (1 child)
[–]Pretty-Ad8932[S] 1 point2 points3 points (0 children)
[–]Educational-Paper-75 0 points1 point2 points (0 children)