Swap

In this post I be  three popular techniques to swap the values of two variables.

swap

Swap

To swap two variables means to interchange their values. In this post we will see three methods to swap the contents of two variables, viz. using a third variable, using addition/subtraction operator and XOR swap.

Swap the contents of two variables using a third variable

This is the simplest and the most intuitive way to swap two variables. An analogy would make this clear without actually writing a single line of code. Let’s say we have two glasses filled with water and orange juice. In order to interchange the contents of two glasses a third glass would be required to hold the contents of one of the glass temporarily before the final interchange can take place.

The Java program to swap the contents of two variables using a third variables would be as follows:

class Swap{
    public static void main( String args[] ){
        int a, b, temp;
        a=1;
        b=2;
        System.out.println("a = " + a + ", b = " + b );
        temp = a;
        a = b;
        b = temp;
        System.out.println("a = " + a + ", b = " + b );
    }
}

The output of the above program would be as follows:

a = 1, b = 2
a = 2, b = 1

Swap the contents of two variables without using a third variable

The content of two variables can also be swapped without using the third variable by clever use of addition and subtraction operators ( sometimes called ‘additive swap’). Let’s assume that the two integer variables  storing the values 1 and 2 respectively.

 A B
 initial value 1 2
b = a + b 1 3
a = b – a 2 3
b = b – a 2 1

The Java program to swap the contents of two variables without using a third variables would be as follows:

class Swap{
    public static void main( String args[] ){
        int a, b;
        a=1;
        b=2;
        System.out.println("a = " + a + ", b = " + b );
        b = a + b;
        a = b - a;
        b = b - a;
        System.out.println("a = " + a + ", b = " + b );
    }
}

The output of the above program would be as follows:

a = 1, b = 2
a = 2, b = 1

Swap the contents of two variables using XOR swap

Let me first give a recap of XOR operator before we dwell into the details of swapping the contents two variables using XOR.

XOR is a logical operation that outputs true whenever both inputs differ (one is true, the other is false). In Java XOR can performed on the individual bits of any variable by using bitwise XOR (^).

XOR Truth Table
Input Output
A B
0 0 0
0 1 1
1 0 1
1 1 0

The procedure to swap using XOR swap is as follows:

A B
initial value 0001 0010
a = a ^ b 0011 0010
b = a ^ b 0011 0001
a = a ^ b 0010 0001

The idea behind this is that when you XOR two values, the resultant is actually a combination of both the values, meaning that if we XOR either of two values with the result we can get back the other one.  The Java implementation to swap the content of two integer variables using XOR swap is as follows:

class Swap{
    public static void main( String args[] ){
        int a, b;
        a=1;
        b=2;
        System.out.println("a = " + a + ", b = " + b );
        a = a ^ b;
        b = a ^ b;
        a = a ^ b;
        System.out.println("a = " + a + ", b = " + b );
    }
}

Pros and cons of the three swapping techniques

Swapping contents of two variables using a third is usually the preferred choice because it’s intuitive and works in all cases. The only disadvantage is that it requires extra memory.

Swapping the contents of two variables using the additive technique is avoided in practise because it doesn’t work in all the cases. This method fails to produce correct results in case of overflow and underflow conditions, meaning , that if the sum or the difference of the two variables is such that it is more or less than what could be stored in the concerned data type, you will get incorrect results.

Swapping the contents of two variables using the XOR technique doesn’t suffer from any deficiency, it’s fast and it works in all the cases. The only disadvantage is that is not very intuitive.