Integer Overflow

M

Michael Press

Right. We've argued that because it happens to be true. See C99
6.2.5p9:

A computation involving unsigned operands can never overflow,
because a result that cannot be represented by the resulting
unsigned integer type is reduced modulo the number that is one
greater than the largest value that can be represented by the
resulting type.

Informally, it's reasonable to refer to what happens when the result
of an unsigned operation won't fit in the target type as "overflow".
The point is that the standard very specifically *doesn't* refer
to it as "overflow".

If you don't like the way the standard uses, or doesn't use,
the term "overflow", that's fine. I'm not sure I like it myself.
All I'm doing is pointing out what the standard actually says.

There is little to like or dislike. The usage and
definitions are well established and effective.
Overflow only matters for 2's complement arithmetic.

C is carry out of the high bit of a word.
R is carry into the high bit of a word.
O is overflow.
O = C ^ R

Consider four bit words.
_
ADD 1000 1111 -> 0111 C R V
_
ADD 0100 0100 -> 1000 C R V
_ _ _
ADD 0100 0011 -> 0111 C R V
_
ADD 1100 1100 -> 1000 C R V
_
NEG 1000 -> 1000 C R V

When the bit patterns are interpreted as unsigned integers
only carry matters.
 
G

gepe

Normally it's inappropriate for general-purpose logic code to have to
worry about overflow.

The real answer is to join the campaign for 64 bit ints. Searchable
arrays of real data aren't going to go above 64 bits any time soon, so
the problem is eliminated.

Computations with 64 bit ints can overflow as computations
with 32 bit ints.

Georg Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top