12 | March 2009 | LINUX For YoU | www.
openITis.
com
S.
G.
Ganesh
The Joy of Programming | Guest Column
I
nteger overflow happens because computers use fixed
width to represent integers.
So which are the operations
that result in overflow? Bitwise and...
More

12 | March 2009 | LINUX For YoU | www.
openITis.
com
S.
G.
Ganesh
The Joy of Programming | Guest Column
I
nteger overflow happens because computers use fixed
width to represent integers.
So which are the operations
that result in overflow? Bitwise and logical operations
cannot overflow, while cast and arithmetic operations can.
For example, ++ and += operators can overflow, whereas &&
or & operators (or even << and >> operators) cannot.
Regarding arithmetic operators, it is obvious that operations
like addition, subtraction and multiplication can overflow.
How about operations like (unary) negation, division
and mod (remainder)? For unary negation, -MIN_INT is
equal to MIN_INT (and not MAX_INT), so it overflows.
Following the same logic, division overflows for the
expression (MIN_INT / -1).
How about a mod operation? It
does not overflow.
The only possible overflow case (MIN_
INT % -1) is equal to 0 (verify this yourself—the formula for
% operator is “a % b = a - ((a / b) *
Less