home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.programming
- Path: sparky!uunet!gatech!darwin.sura.net!haven.umd.edu!news.umbc.edu!gmuvax2!scoile
- From: scoile@gmuvax2.gmu.edu (Steve Coile)
- Subject: Re: Detecting Integer Overflow/Underflow
- Message-ID: <1992Aug23.133620.6154@gmuvax2.gmu.edu>
- Organization: George Mason University, Fairfax, Va.
- References: <1992Aug21.182206.15853@progress.com> <HUDGENS.92Aug22113851@sun13.SCRI.FSU.EDU> <1572@idacrd.UUCP>
- Date: Sun, 23 Aug 1992 13:36:20 GMT
- Lines: 27
-
- David desJardins writes:
- >
- >unsigned int adduint (unsigned int a, unsigned int b, int &overflow) {
- > /* a and b are unsigned integers, WORDSIZE is the length of each. The
- > return value is the sum of a and b, and the value of overflow is 1
- > if overflow occurred, or zero otherwise. */
- >
- > unsigned int sum;
- > unsigned int temp;
- >
- > sum = a + b;
- > temp = ((a ^ sum) & (a ^ b)) ^ b;
- > *overflow = temp >> (WORDSIZE - 1);
- > return sum;
- >}
-
- Could someone please, PLEASE explain what is going on with
-
- ((a ^ sum) & (a ^ b)) ^ b
-
- What is the justification for doing this (i.e. how in the WORLD would someone
- figure this out that THIS is the way to do it?????)
-
- Thanks!
-
- Steve "Stevers!" Coile scoile@gmuvax[{.BITNET|[2].gmu.edu}]
- "Maturity is knowing when and where to act immature"
-