Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!iuvax!rutgers!usc!orion.cf.uci.edu!uci-ics!ucla-cs!math.ucla.edu!sonia!pmontgom From: pmontgom@sonia.math.ucla.edu (Peter Montgomery) Newsgroups: comp.lang.c Subject: Re: swap(x,y) Message-ID: <1609@sunset.MATH.UCLA.EDU> Date: 23 Aug 89 06:51:00 GMT References: <1524@l.cc.purdue.edu> <19211@mimsy.UUCP> Reply-To: pmontgom@math.ucla.edu (Peter Montgomery) Followup-To: comp.lang.misc Organization: UCLA Mathematics Department Lines: 30 In article <19211@mimsy.UUCP> chris@mimsy.UUCP (Chris Torek) writes: >The xor-trick is more often seen in the `swap registers in a tight >situation' situation than the add/subtract trick (it has the advantage >of working on two-address machines, as well). Furthermore, the xor trick also preserves +0 and -0 on one's complement machines, while the add/subtract trick does not. Swapping is really just another instance of the recently discussed problem of functions returning multiple values (e.g., quotient/remainder, cosine/sine, function value/derivative, maximum/minimum, less than flag/equality flag/greater than flag, upper bound/lower bound, sign/exponent/mantissa, character read/EOF flag/error indicator): modern languages should allow the user to write something like (x, y) := (y, x); Here "(y,x)" denotes "the two expressions y and x" while "(x,y)" denotes "the two variables (or array elements, or lvalues) x and y". The language semantics should stipulate that all expressions on the right side and all addresses (or register locations) of variables on the left side will be determined first (order determined by the compiler), and then the assignments will be made (again in a compiler-determined order). A consequence is that variables on the left side may not be aliased to each other unless (as here) the corresponding right sides will necessarily be equal. -------- Peter Montgomery pmontgom@MATH.UCLA.EDU