As frequent readers might have observed, I like the concept of the Carry Bit as it allows for efficient implementations of long integer arithmetic, which I would like to use as default integer type for most application development. And unfortunately such facilities are not available in high level languages like C and Java. But it is possible to recover the carry bit from what is available in C or Java, with some extra cost of performance, but maybe neglectable, if the compiler does a good optimization on this. We might assume gcc on a 64-Bit-Linux. It should be possible to do similar things on other platforms.
So we add two unsigned 64-bit integers and to a result
using the typical „long long“ of C. We assume that
. In the same way we assume and with the same kind of conditions for , , or , , , respectively.
Now we have
and we can see that
And we have
is the carry bit.
When looking just at the highest visible bit and the carry bit, this boils down to
This leaves us with eight cases to observe for the combination of , and :
Or we can check all eight cases and find that we always have
So the result does not depend on anymore, allowing to calculate it by temporarily casting , and to (signed long long) and using their sign.
We can express this as „use if and use if „.
An incoming carry bit does not change this, it just allows for , which is sufficient for making the previous calculations work.
In a similar way subtraction can be dealt with.
The basic operations add, adc, sub, sbb, mul, xdiv (div is not available) have been implemented in this library for C. Feel free to use it according to the license (GPL). Addition and substraction could be implemented in a similar way in Java, with the weirdness of declaring signed longs and using them as unsigned. For multiplication and division, native code would be needed, because Java lacks 128bit-integers. So the C-implementation is cleaner.