# Formal Formulations

Exchanging Values
via
Three 'Exclusive-Or' Operations

## Introduction

The elementary approach for
exchanging the contents of two memory registers,
A and B,
is to declare a third register, say,
C,
and then:
 Copy the contents of A into C; Copy the contents of B into A; Copy the contents of C into B;
This is a fine solution,
provided that creating another register is cheap.
However, if registers are expensive and CPU cycles are cheap,
then an alternative approach is available.

When three consecutative "Exclusive-Or" operations
are performed on the contents of two memory registers,
the effect is to exchange the contents of the two memory registers.

## Proof

The proof consists for three sections,
one for each of the "Exclusive-Or" Assignments.

# Exclusive Or Demonstration

Enter a value for A, B, and then click Exchange.

 A B A = A xor B; ( B = B ) ( A = A ) B = A xor B; A = A xor B; ( B = B )

Exchanging Values
[ Introduction ][ Theorem ]
[ First Assignment ][ Second Assignment ][ Third Assignment ]