  # Interchanging 2 variables without the use of a third    ## Category: Programming/PHP

There are several ways Ive seen over the years of interchanging 2 variables. Ill show you here how you can do it with bitwise operations. This method is most efficient when used in combination with assembler. In C++ it is looked at more like a curiosity than a real method by most. However if you would want to make your C++ code harder to read, then this can be helpful.

### A byte of theory

Before seeing how we can interchange two variables we will have to learn the use of the XOR logical operator. It is sometimes called exclusive OR, or exclusive disjunction. It is true when one of the operands (the numbers between which we are using XOR) is true, but not both, while its close brother the OR operator is true even when both are true.

 a b a XOR b 0 0 0 0 1 1 1 0 1 1 1 0

You can remember it more easily if you think of it like adding binary numbers without carry. So: 0+0=0 ; 0+1=1 ; 1+0=1 ; 1+1=0 carry 1. So by ignoring the carry we obtain the XOR logical function.

### So how can we use it?

Let’s say that we have two variables say a=5 and b=3. The steps we would have to take to switch the two variables (registers) are shown in the table below:

 a 0101 b 0011 a = a XOR b 0110 b = a XOR b 0101 a = a XOR b 0011

i)    In assembler ( asm )

Let’s suppose that a and b are in two of the CPU registers: AL and respectively BL.

The coding sequence looks like this:

;AL contains 0101, BL contains 0011

XOR AL, BL            ; AL will receive the result of the operation: 0110
XOR BL, AL            ; BL will contain 0101, the starting value of AL
XOR AL, BL            ; after this XOR with BL, AL will receive 0011

Note: As you can see the XOR operator in ASM delivers the result of the operation in the first register you mention.

ii)    In C/C++

In C++ the XOR operator is represented by the operator "^" (without the quotes). So the coding sequence for C++ is:

main(){

int a=5;
int b=3;

a = a ^ b;    // a will contain a xor b = 6
b = a ^ b;    // b will receive 6 xor 3 = 5
a = a ^ b;    // a will become 6 xor 5 = 3

printf("a=%d b=%d", a,b);
}

By using the operator ^= we could make our code harder to read :

main(){

int a=5;
int b=3;

a ^= b;    // a will contain a xor b = 6
b ^= b;    // b will receive 6 xor 3 = 5
a ^= b;    // a will become 6 xor 5 = 3

printf("a=%d b=%d", a,b);
}

In the end I would like to mention another interesting use of the XOR function especially for ASM. If we want to empty a register (let’s say AL) we would use "XOR AL, AL". So by XORing a number with itself we obtain 0 because 0 xor 0 = 0 and 1 xor 1 = 0.

Posted by: Nitro on September 16, 2006 at 09:30.   Other ways
There are other methods for this:

a = a * b
b = a / b
a = a / b

Posted by Gerard on September 16, 2006 at 10:45 AM.

Re: Other ways
There are indeed other ways as well, but they use more processor cycles usually. Xor is efficient simply because it uses a single processor cycle and can't cause any range overflow. Also, the method above is efficient because it uses a single operation, which makes it easier to integrate into specialised microprocessors for example.

Another method with similar performaces can be using:

a = a + b
b = a - b
a = a - b

But then again, this needs two operations (sum and difference - which can be made with negation and another sum), but it still uses more cycles than a simple xor.

Posted by Indy on September 16, 2006 at 12:32 PM.

Not Other Ways
The two other ways noted above presuppose that variable a has sufficient bits available to store the sum or product of a and b.

The original method does not have this restriction.

Posted by Mike on October 27, 2006 at 01:39 PM.

Add and subtract just have to be invertible no actually capable of preserving the intermediate range
title says it all

Posted by Anon Ymous on October 31, 2006 at 11:32 PM.

Type
Actually, it's:

a ^= b;
b ^= a;
a ^= b;

Posted by Larry on December 19, 2009 at 07:27 PM.

post o post
currently, actually no idea

Posted by want to say ya on August 25, 2010 at 08:59 AM.

## Feeds 