You can define a carry by virtue of the fact that if you overflow by adding two numbers, the result will always be less than either of the other two values.
In other words, if a + b less than a , it overflows. This is for positive values ββof a and b , of course, but what you will almost certainly use for the bignum library.
Unfortunately, wrapping introduces an additional complication in that adding the highest possible value plus wrapping one will give you the same value you started from. Therefore, you should treat this as a special case.
Something like:
carry = 0 for i = 7 to 0: if a[i] > b[i]: small = b[i], large = a[i] else: small = a[i], large = b[i] if carry is 1 and large is maxvalue: c[i] = small carry = 1 else: c[i] = large + small + carry if c[i] < large: carry = 1 else carry = 0
In fact, you can also consider using all the bits in the elements of the array.
I have implemented libraries in the past where the maximum βdigitβ is less than or equal to the square root of the highest value that it can hold. Thus, for 8-bit (octet) digits, you store values ββfrom 0 to 15 - thus, multiplying two digits and adding the maximum carry will always correspond to an octet, which makes overflow detection controversial, although at the cost of some memory.
Similarly, 16-bit digits will have a range from 0 to 255, so that it will not overflow on 65536.
In fact, I sometimes limited it to more than that, ensuring that the value of the artificial wrapping is ten (so that the octet will contain from 0 to 9, the 16-bit digits will be from 0 to 99, the 32-bit digits from 0 to 9999, etc. d.
It's a little more wasteful in space, but makes converting to and from text (like printing your numbers) incredibly easy.