swap(x,y)
Peter Montgomery
pmontgom at sonia.math.ucla.edu
Wed Aug 23 16:51:00 AEST 1989
In article <19211 at mimsy.UUCP> chris at 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 at MATH.UCLA.EDU
More information about the Comp.lang.c
mailing list