O
Ole Nielsby
Given two longs:
long a = ...;
long b = ...;
What's the simplest/fastest way of performing a simple
operation on longs and detecting overflow?
long c = a + b; // but branch on overflow
One way is to perform the operation with long long:
long long temp = (long long)a + b;
c = (long)temp;
if ((long long)c != temp) goto blast;
but it seems a bit strange that I'd have to use long long
for all computations, to deal with the relatively rare case
of overflow.
long a = ...;
long b = ...;
What's the simplest/fastest way of performing a simple
operation on longs and detecting overflow?
long c = a + b; // but branch on overflow
One way is to perform the operation with long long:
long long temp = (long long)a + b;
c = (long)temp;
if ((long long)c != temp) goto blast;
but it seems a bit strange that I'd have to use long long
for all computations, to deal with the relatively rare case
of overflow.