Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!wasatch!cs.utexas.edu!uunet!virtech!cpcahil From: cpcahil@virtech.UUCP (Conor P. Cahill) Newsgroups: comp.lang.c Subject: Re: swap(x,y) Message-ID: <1061@virtech.UUCP> Date: 22 Aug 89 22:50:46 GMT References: Organization: Virtual Technologies Inc Lines: 17 In article , tg1e+@andrew.cmu.edu (Timothy R. Gottschalk) writes: > To swap two variables x,y in C without using a temporary variable: > x += y; > y = x - y; > x -= y; The problem with this method is that it will be easy to overflow the variables in the first statement. For example if x and y are short (16 bit) integers and x contains 18,000 and y contains 19,000 the x will overflow to negative territory on the first assignment and throw off the rest of the equations. If x & y are large enough the overflow could just be lost. -- +-----------------------------------------------------------------------+ | Conor P. Cahill uunet!virtech!cpcahil 703-430-9240 ! | Virtual Technologies Inc., P. O. Box 876, Sterling, VA 22170 | +-----------------------------------------------------------------------+