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...

Keyboard Shortcuts

Navigation
Next / previous item
j/k
Open post
oorEnter
Preview post
v
Post Actions
Love post
a
Like post
l
Dislike post
d
Undo reaction
u
Recommendations
Add interest / feed
Enter
Not interested
x
Go to
Home
gh
Interests
gi
Feeds
gf
Likes
gl
History
gy
Changelog
gc
Settings
gs
Browse
gb
Search
/
General
Show this help
?
Submit feedback
!
Close modal / unfocus
Esc

Press ? anytime to show this help