Subject: Re: detect arithmetic overflow
To: Christos Zoulas <firstname.lastname@example.org>
From: Steven M. Bellovin <email@example.com>
Date: 08/06/2003 11:16:50
In message <HJ7C63.Fpp@tac.nyc.ny.us>, Christos Zoulas writes:
>In article <Pine.LNX.4.21.0308061116010.7039-100000@Tempo.Update.UU.SE>,
>Johnny Billquist <firstname.lastname@example.org> wrote:
>>On Wed, 6 Aug 2003, David Laight wrote:
>>> > It's even easier than that. For signed addition, just check if the result
>>> > is negative. If so, then you have overflow. For unsigned addition, you'll
>>> > have to test it as you described, though.
>>> errmm -1 + -1 = -2 but hasn't overflowed
>>Argh. My brain is positive. :-)
>>However, the result is also less than both operands... So you just flunked
>>yourself too. :-)
>>Actually, you need the opposite test for negative operands, and if the
>>operands have different sign, you will not get overflow.
>Isn't the two's complement arithmetic rule for overflow that the carry bit
>into the signed bit must equal to the carry bit out of the signed bit?
Let me point folks at "Hacker's Delight", by Henry S. Warrn,
Addison-Wesley, 2002. Note that this is about clever programming
tricks, rather than breaking into computers.
--Steve Bellovin, http://www.research.att.com/~smb