Suppose you have two integers

*a*and

*b*. You want to add them, but you also want to be sure they don't overflow. You're working in a language where overflow is undefined, such as C. You can't use arbitrary precision variables or anything fancy like that, nor can you make use of low-level things like CPU overflow detection. You need to guard against overflow

*mathematically*, without the use of any of those systems. Maybe you're working on a reduced architecture that doesn't provide overflow detection. Maybe your language or library is deficient and lacks an arbitrary precision integer type. It doesn't really matter. The point is, taking away all those "outs", we're left with a somewhat interesting problem.

Here are some assumptions you may make:

- You have a constant called
`INT_MAX`

which is equal to the largest integer which can be represented on your system. You also have another called`INT_MIN`

, which serves a similar purpose for the smallest (most negative) integer. - You're working in two's complement, but overflow is still undefined.
- You
*do not*have the exact number of bits available (but you could figure it out from`INT_MAX`

, so that's not much of a restriction). - You may throw an exception to indicate an overflow condition.