11,581,982 members (74,680 online)

# The Stupid XOR Trick

, 13 Aug 2002 CPOL 196.6K 39
 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 1:27 .:floyd:. 23-Feb-14 1:27
 Re: My vote of 1 Andreas S. Franci Gonçalves23-Feb-14 4:08 Andreas S. Franci Gonçalves 23-Feb-14 4:08
 Re: My vote of 1 .:floyd:.23-Feb-14 12:21 .:floyd:. 23-Feb-14 12:21
 See this. Nibu thomas7-Nov-05 17:27 Nibu thomas 7-Nov-05 17:27
 Re: See this. toxcct26-Feb-06 23:42 toxcct 26-Feb-06 23:42
 Re: See this. Nibu thomas26-Feb-06 23:43 Nibu thomas 26-Feb-06 23:43
 Re: See this. toxcct26-Feb-06 23:45 toxcct 26-Feb-06 23:45
 Re: See this. Nibu thomas26-Feb-06 23:48 Nibu thomas 26-Feb-06 23:48
 Re: See this. toxcct26-Feb-06 23:51 toxcct 26-Feb-06 23:51
 Re: See this. Nibu thomas26-Feb-06 23:52 Nibu thomas 26-Feb-06 23:52
 Re: See this. toxcct26-Feb-06 23:54 toxcct 26-Feb-06 23:54
 Re: See this. Nibu thomas26-Feb-06 23:56 Nibu thomas 26-Feb-06 23:56
 Trick subject to compiler (eek, gcc 3.x) Anonymous17-Aug-05 14:18 Anonymous 17-Aug-05 14:18
 A tiny "proof" for the trick, just for fun Humpzlopogas15-Nov-03 17:37 Humpzlopogas 15-Nov-03 17:37
 Re: A tiny "proof" for the trick, just for fun SaurweinAndreas16-Nov-03 4:07 SaurweinAndreas 16-Nov-03 4:07
 Re: A tiny "proof" for the trick, just for fun .:floyd:.23-Feb-14 1:14 .:floyd:. 23-Feb-14 1:14
 Re: Old trick Mr.Prakash1-Dec-03 16:43 Mr.Prakash 1-Dec-03 16:43
 Re: Old trick Mr.Prakash1-Dec-03 21:16 Mr.Prakash 1-Dec-03 21:16
 x ^= y ^= x ^= y is undefined. Period. stickboy20-May-03 17:57 stickboy 20-May-03 17:57
 Re: x ^= y ^= x ^= y is undefined. Period. SaurweinAndreas21-May-03 4:29 SaurweinAndreas 21-May-03 4:29
 Re: x ^= y ^= x ^= y is undefined. Period. stickboy21-May-03 17:36 stickboy 21-May-03 17:36
 Re: x ^= y ^= x ^= y is undefined. Period. SaurweinAndreas22-May-03 0:03 SaurweinAndreas 22-May-03 0:03
 Re: x ^= y ^= x ^= y is undefined. Period. stickboy22-May-03 7:11 stickboy 22-May-03 7:11
 Re: x ^= y ^= x ^= y is undefined. Period. .:fl0yd:.14-Jun-03 0:23 .:fl0yd:. 14-Jun-03 0:23
 Re: x ^= y ^= x ^= y is undefined. Period. Anonymous2-Nov-03 21:29 Anonymous 2-Nov-03 21:29
 Re: x ^= y ^= x ^= y is undefined. Period. BSCE200620-Nov-04 22:49 BSCE2006 20-Nov-04 22:49
 Re: x ^= y ^= x ^= y is undefined. Period. bitznarf9-Aug-06 7:35 bitznarf 9-Aug-06 7:35
 Suprised John R. Shaw4-May-03 11:13 John R. Shaw 4-May-03 11:13
 Thanks... Xambo11-Apr-03 19:52 Xambo 11-Apr-03 19:52
 A trick known ... AquaRegia26-Aug-02 23:11 AquaRegia 26-Aug-02 23:11
 Re: A trick known ... as undefined behaviour Moak21-Nov-02 4:01 Moak 21-Nov-02 4:01
 Re: A trick known ... as undefined behaviour .:fl0yd:.26-Jun-03 9:00 .:fl0yd:. 26-Jun-03 9:00
 Re: A trick known ... Dimitris Vassiliades26-Sep-03 22:59 Dimitris Vassiliades 26-Sep-03 22:59
 Nice little trick Brian Delahunty26-Aug-02 1:26 Brian Delahunty 26-Aug-02 1:26
 Re: Nice little trick Andreas Saurwein26-Aug-02 10:18 Andreas Saurwein 26-Aug-02 10:18
 Such XOR swap not work with : int * y = &x; Sprinter19-Aug-02 22:08 Sprinter 19-Aug-02 22:08
 Re: Such XOR swap not work with : int * y = &x; Crius20-Aug-02 5:58 Crius 20-Aug-02 5:58
 Dangerous to use with pointers on common array Sprinter20-Aug-02 22:28 Sprinter 20-Aug-02 22:28
 Re: Dangerous to use with pointers on common array Crius21-Aug-02 4:55 Crius 21-Aug-02 4:55
 Re: Dangerous to use with pointers on common array Anonymous9-May-03 11:26 Anonymous 9-May-03 11:26
 Re: Dangerous to use with pointers on common array Crius9-May-03 11:39 Crius 9-May-03 11:39
 Re: Such XOR swap not work with : int * y = &x; Andreas Saurwein26-Aug-02 10:24 Andreas Saurwein 26-Aug-02 10:24
 Alternative Way Krishnendu sengupta19-Aug-02 20:52 Krishnendu sengupta 19-Aug-02 20:52
 Re: Alternative Way Jack Ploeg19-Aug-02 22:38 Jack Ploeg 19-Aug-02 22:38
 Re: Alternative Way Krishnendu sengupta20-Aug-02 0:22 Krishnendu sengupta 20-Aug-02 0:22
 Re: Alternative Way Thresher31-Oct-02 21:56 Thresher 31-Oct-02 21:56
 Re: Alternative Way vboctor6-Nov-02 18:18 vboctor 6-Nov-02 18:18