home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #18 / NN_1992_18.iso / spool / comp / programm / 2438 < prev    next >
Encoding:
Text File  |  1992-08-23  |  1.3 KB  |  38 lines

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