get unsigned long long addition carry
Carry can be only 0
or 1
. 1
if there was a wrapping-around and 0
otherwise.
The wrapping-around is happening in case a + b > ULONG_LONG_MAX
is true . Note, this is in mathematical terms, not in terms of C, as if a + b
is actually overflowing, then this will not work. Instead you want to rearrange it to be a > ULONG_LONG_MAX - b
. So the value of carry will be:
carry = a > ULONG_LONG_MAX - b ? 1 : 0;
or any preferred style equivalent.
- Don't forget to include
limits.h
.
As @EugeneSh. observes, the carry is either 0 or 1. Moreover, given that a
and b
both have the same unsigned type, their sum is well defined even if the arithmetic result exceeds the range of their type. Moreover, the (C) result of the sum will be less than both a
and b
when overflow occurs, and greater otherwise, so we can use the fact that C relational operations evaluate to either 0 or 1 to express the carry bit as
carry = (a + b) < a;
That does not require any headers, nor does it depend on a specific upper bound, or even on a
and b
having the same type. As long as both have unsigned types, it reports correctly on whether the sum overflows the wider of their types or unsigned int
(whichever is wider), which is the same as their sum setting the carry bit. As a bonus, it is expressed in terms of the sum itself, which I think makes it clear what's being tested.