12,702,969 members (31,903 online)
alternative version

207.4K views
39 bookmarked
Posted

# The Stupid XOR Trick

, 13 Aug 2002 CPOL
 Rate this:
Explaining the phenomenon of x^=y^=x^=y;

## Introduction

As a result of many, sometimes insulting, reactions on my signature, I decided to write an article on this topic. The mentioned signature looks like this:

int x=1, y=5;
x^=y^=x^=y; // what's the content of x and y now?

Well, the reactions range from "that's not even valid C/C++ code" to "that works only with X=1 and y=5" and "you better learn C". To all these unbelievers and of course everyone else who doesn't know this "trick"; may this article enlighten you :)

## What it's meant to do

Simply exchange the values of two integer variables without using a temporary variable. It gives unpredictable results with other variable types than integer types.

The symbol ^ is the XOR operator used in C/C++. See the article from PJ Arends on Bitwise Operators for a full introduction to bits and related operators.

The questionable code fragment x^=y^=x^=y; can be expanded into x XOR y XOR x XOR y. Since C/C++ resolves expressions like this from right to left, we can split it into the steps performed by the compiler:

x ^= y; // the right-most expression
y ^= x; // the middle expression
x ^= y; // the left-most expression


Which can be further expanded into

x = x ^ y; // x = 1 XOR 5  -> x = 4
y = y ^ x; // y = 5 XOR 4 = 1  -> y = 1
x = x ^ y; // x = 4 XOR 1 = 5  -> x = 5

Voila, here we are x=5 and y=1. That's what we wanted to achieve. And all this without using a temporary variable like in int t = x; x = y; y = t; which most programmers use. I confess that my method is confusing to most people because it's a little known method. As an additional benefit, the compiler can translate it into 3 simple XOR assembler statements.

## Sample

For all who still don't believe it, here is a sample program to prove  that it works (with any integer number). The sample can be compiled in Ansi and Unicode and is compatible with VC6 and VC7. It is not using any library except the standard C Runtime Library.

#include <stdio.h>
#include <stdlib.h>
#include <tchar.h>

int _tmain(int argc, TCHAR* argv[])
{
if(argc != 3)
{
_tprintf(
_T("\nUsage: StupidXORTricks.exe intvalue1 intvalue2\n"));
}
else
{
int x = _ttoi(argv[1]);
int y = _ttoi(argv[2]);

_tprintf(_T("x = %d, y = %d\n"), x, y);
_tprintf(_T("Performing x^=y^=x^=y...\n"));

x^=y^=x^=y;

_tprintf(_T("x = %d, y = %d\n"), x, y);

}
return 0;
}


Finally, I hope that I don't get any more responses to my sig. :)

## Share

 Retired Brazil
No Biography provided

## You may also be interested in...

 First PrevNext
 My vote of 1 .:floyd:.23-Feb-14 2:27 .:floyd:. 23-Feb-14 2:27
 Re: My vote of 1 Andreas S. Franci Gonçalves23-Feb-14 5:08 Andreas S. Franci Gonçalves 23-Feb-14 5:08
 Re: My vote of 1 .:floyd:.23-Feb-14 13:21 .:floyd:. 23-Feb-14 13:21
 See this. Nibu thomas7-Nov-05 18:27 Nibu thomas 7-Nov-05 18:27
 Re: See this. toxcct27-Feb-06 0:42 toxcct 27-Feb-06 0:42
 Re: See this. Nibu thomas27-Feb-06 0:43 Nibu thomas 27-Feb-06 0:43
 Re: See this. toxcct27-Feb-06 0:45 toxcct 27-Feb-06 0:45
 Re: See this. Nibu thomas27-Feb-06 0:48 Nibu thomas 27-Feb-06 0:48
 Re: See this. toxcct27-Feb-06 0:51 toxcct 27-Feb-06 0:51
 Re: See this. Nibu thomas27-Feb-06 0:52 Nibu thomas 27-Feb-06 0:52
 Re: See this. toxcct27-Feb-06 0:54 toxcct 27-Feb-06 0:54
 Re: See this. Nibu thomas27-Feb-06 0:56 Nibu thomas 27-Feb-06 0:56
 Trick subject to compiler (eek, gcc 3.x) Anonymous17-Aug-05 15:18 Anonymous 17-Aug-05 15:18
 A tiny "proof" for the trick, just for fun Humpzlopogas15-Nov-03 18:37 Humpzlopogas 15-Nov-03 18:37
 Re: A tiny "proof" for the trick, just for fun SaurweinAndreas16-Nov-03 5:07 SaurweinAndreas 16-Nov-03 5:07
 Re: A tiny "proof" for the trick, just for fun .:floyd:.23-Feb-14 2:14 .:floyd:. 23-Feb-14 2:14
 Re: Old trick Mr.Prakash1-Dec-03 17:43 Mr.Prakash 1-Dec-03 17:43
 Re: Old trick Mr.Prakash1-Dec-03 22:16 Mr.Prakash 1-Dec-03 22:16
 x ^= y ^= x ^= y is undefined. Period. stickboy20-May-03 18:57 stickboy 20-May-03 18:57
 Re: x ^= y ^= x ^= y is undefined. Period. SaurweinAndreas21-May-03 5:29 SaurweinAndreas 21-May-03 5:29
 Re: x ^= y ^= x ^= y is undefined. Period. stickboy21-May-03 18:36 stickboy 21-May-03 18:36
 Re: x ^= y ^= x ^= y is undefined. Period. SaurweinAndreas22-May-03 1:03 SaurweinAndreas 22-May-03 1:03
 Re: x ^= y ^= x ^= y is undefined. Period. stickboy22-May-03 8:11 stickboy 22-May-03 8:11
 Last Visit: 31-Dec-99 19:00     Last Update: 24-Jan-17 7:38 Refresh 12345 Next »