Large number implementation tips?
reddit.com·4h·
Flag this post

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?

Similar Posts

Loading similar posts...