Skip to content

Swapping the values of two variables

Posted on:June 18, 2024

It turns out that it is possible to swap the values of two variables, in any language (atleast anyone that I know) without using a third auxiliar variable. It is posible thanks to binary operator XOR.

At the end of the day, any value contained in a variable, whether it is a number or a letter, is a sequence of binary values. This allows us to modify its value at this fundamental level, so we can quickly swap its value with that of another variable.

Since JavaScript is the best language in the universe and definitely doesn’t have a lot of drawbacks when doing this, it is the language I will use as an example:

Variables contain integers:

Let’s suppose we have numbers 3 and 2 in variables x and y. We don’t want to deal with how is the binary representation of a float number (actually, any integer in Javascript a signed float number), so, for the purpose of the explanation, we have here a nibble (half byte) for each value:

variabledecimalbinary
x6[0110]
y10[1010]

We can do it with XOR operator (operator ir ^)

let x = 6
let y = 10

y = x ^ y //1
x = x ^ y //2
y = x ^ y //3

x
[10]

y
[6]

Let’s see what happened:

In #1 where are using exclusive or (XOR) on binary values [0110] and [1010], asigning the result to variable y, this means:

y = [0110] ^ [1010] -> [1100]

So in #2 the operation is between value x and XOR result between x and y. This return y, se we can assign it to x:

x = [0110] ^ [1100] -> [1010]

Now x contain y original value, but y is still storing the XOR result instead of x original value. We perform the same operation to fix that:

y = [1010] ^ [1100] -> [0110]

Therefore, y obtain the original value of x.

Variables contains a string:

At the end of the day, each character in a string is no more than a visual representation of an Unicode value.

We can’t perform the opperation as straightforward as with integers, but we can get the numeric values, perform the operation and transform it to characters again.

Let’s do it whith variables m and n, containing 'a' and 'b' respectively. Is as simple as this:

let m = 'a'
let n = 'b'

m = m.charCodeAt(0) ^ n.charCodeAt(0) //1
n = m ^ n.charCodeAt(0) //2
m = m ^ n  //3

m = String.fromCharCode(m) //4
n = String.fromCharCode(n)

m
['b']

n
['a']

In #1 we obtain the numeric value for both characters to be able to perform the XOR operation.

In #2 we have m with the XOR value, but n is still a string, and the numeric value is needed. Then in #3 value for m is replaced from XOR result to numeric value of character in n.

At this point, we have already changed the values, and is just a matter to obtain the letter we are looking for, as in #4.


And that’s it. In other languages you can do the same to interchange memory pointers for example, or anything really.

It’s true that in Javascript you can always simply do: [a,b] = [b,a] instead of this cryptic way to make things impossible to understand, but then, where is the fun?