(really about xor)

Frank Adams franka at mmintl.UUCP
Sat Feb 20 11:50:24 AEST 1988


We are considering performing an exchange of two variables A and B via a
function f, using the steps:

	A := f(A,B)
	B := f(B,A)
	A := f(A,B)

I asserted that in order for this to work, the functions f1: x,y -> x,f(x,y)
and f2: x,y -> f(x,y),y must both be invertible.  I will now prove this.

In what follows, a and b are the initial values of A and B, respectively.
This means, in particular, that the final value of A is b, and the final
value of B is a.  Since B is last updated at the second assignment, we get

	a = f(b, f(a,b))

This demonstrates that f2 is invertible -- the inverse is x,y -> f(y,x),y.

Substituting the final value of B into the last assignment, we conclude that:

	b = f(f(a,b), a)

This shows that the inverse of f1 is x,y -> x,f(y,x).  Q.E.D.

The same result will follow if we switch the order of the arguments in any
or all of the assignments.

The invertibility of f1 and f2 is not, in general, sufficient to make this
procedure perform a swap.  However, it turns out that for Boolean values,
the only two functions with this property are exclusive or and equivalence,
and both of these do, in fact, work.
-- 

Frank Adams                           ihnp4!philabs!pwa-b!mmintl!franka
Ashton-Tate          52 Oakland Ave North         E. Hartford, CT 06108



More information about the Comp.lang.c mailing list