<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Swap two variables using XOR</title>
	<atom:link href="http://betterexplained.com/articles/swap-two-variables-using-xor/feed/" rel="self" type="application/rss+xml" />
	<link>http://betterexplained.com/articles/swap-two-variables-using-xor/</link>
	<description>Learning shouldn't hurt. Let's share the insights that made difficult ideas click.</description>
	<lastBuildDate>Fri,  6 Nov 2009 22:26:19 -0800</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Anonymous</title>
		<link>http://betterexplained.com/articles/swap-two-variables-using-xor/#comment-261233</link>
		<dc:creator>Anonymous</dc:creator>
		<pubDate>Mon, 02 Nov 2009 00:22:25 +0000</pubDate>
		<guid isPermaLink="false">http://betterexplained.com/articles/swap-two-variables-using-xor/#comment-261233</guid>
		<description>Well I&#039;m not getting one single exchange instruction... at least in debugging mode. This is what I see in my VC++ 6 dissassembly window:

45:           a.i ^= b.i;
00401450   mov         edx,dword ptr [a]
00401453   xor         edx,dword ptr [b]
00401456   mov         dword ptr [a],edx
46:           b.i = a.i^b.i;
00401459   mov         eax,dword ptr [a]
0040145C   xor         eax,dword ptr [b]
0040145F   mov         dword ptr [b],eax
47:           a.i ^= b.i;
00401462   mov         ecx,dword ptr [a]
00401465   xor         ecx,dword ptr [b]
00401468   mov         dword ptr [a],ecx
48:
49:           t = y;
0040146B   mov         edx,dword ptr [y]
0040146E   mov         dword ptr [t],edx
50:           y = x;
00401471   mov         eax,dword ptr [x]
00401474   mov         dword ptr [y],eax
51:           x = t;
00401477   mov         ecx,dword ptr [t]
0040147A   mov         dword ptr [x],ecx


&#039;a&#039; and &#039;b&#039; are what I call &quot;flints,&quot; a union containing a float and int.</description>
		<content:encoded><![CDATA[<p>Well I&#8217;m not getting one single exchange instruction&#8230; at least in debugging mode. This is what I see in my VC++ 6 dissassembly window:</p>
<p>45:           a.i ^= b.i;<br />
00401450   mov         edx,dword ptr [a]<br />
00401453   xor         edx,dword ptr [b]<br />
00401456   mov         dword ptr [a],edx<br />
46:           b.i = a.i^b.i;<br />
00401459   mov         eax,dword ptr [a]<br />
0040145C   xor         eax,dword ptr [b]<br />
0040145F   mov         dword ptr [b],eax<br />
47:           a.i ^= b.i;<br />
00401462   mov         ecx,dword ptr [a]<br />
00401465   xor         ecx,dword ptr [b]<br />
00401468   mov         dword ptr [a],ecx<br />
48:<br />
49:           t = y;<br />
0040146B   mov         edx,dword ptr [y]<br />
0040146E   mov         dword ptr [t],edx<br />
50:           y = x;<br />
00401471   mov         eax,dword ptr [x]<br />
00401474   mov         dword ptr [y],eax<br />
51:           x = t;<br />
00401477   mov         ecx,dword ptr [t]<br />
0040147A   mov         dword ptr [x],ecx</p>
<p>&#8216;a&#8217; and &#8216;b&#8217; are what I call &#8220;flints,&#8221; a union containing a float and int.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Anonymous</title>
		<link>http://betterexplained.com/articles/swap-two-variables-using-xor/#comment-246911</link>
		<dc:creator>Anonymous</dc:creator>
		<pubDate>Tue, 23 Jun 2009 13:41:53 +0000</pubDate>
		<guid isPermaLink="false">http://betterexplained.com/articles/swap-two-variables-using-xor/#comment-246911</guid>
		<description>Hi Kalid,
In response to Rich you said the following:

///////////////////
  Hi Rich, good question.

  When using pointers to the same location, when  you do the first step (x = x xor y) =&gt; 0 you also set y to 0 as well since it’s at the same location. In this case, you’ve lost the value 1234 — x and y were sharing the same location and it was overwritten with 0.

Hope this helps.

Kalid — July 17, 2008 @ 12:55 pm
//////

I&#039;m not able to understand why would pointer y change when pointer x changes. I think you meant
(*y) and (*x). Please Clarify.

Thanks.

RM</description>
		<content:encoded><![CDATA[<p>Hi Kalid,<br />
In response to Rich you said the following:</p>
<p>///////////////////<br />
  Hi Rich, good question.</p>
<p>  When using pointers to the same location, when  you do the first step (x = x xor y) =&gt; 0 you also set y to 0 as well since it’s at the same location. In this case, you’ve lost the value 1234 — x and y were sharing the same location and it was overwritten with 0.</p>
<p>Hope this helps.</p>
<p>Kalid — July 17, 2008 @ 12:55 pm<br />
//////</p>
<p>I&#8217;m not able to understand why would pointer y change when pointer x changes. I think you meant<br />
(*y) and (*x). Please Clarify.</p>
<p>Thanks.</p>
<p>RM</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Borsuc</title>
		<link>http://betterexplained.com/articles/swap-two-variables-using-xor/#comment-243041</link>
		<dc:creator>Borsuc</dc:creator>
		<pubDate>Sun, 31 May 2009 20:13:38 +0000</pubDate>
		<guid isPermaLink="false">http://betterexplained.com/articles/swap-two-variables-using-xor/#comment-243041</guid>
		<description>There&#039;s no reason why it wouldn&#039;t work with floating point numbers, because they are just 1s and 0s as well.

This technique will swap the BITS, and everything made with BITS will work.

Sure you&#039;ll first have to have a way to access float&#039;s bits directly with XOR.


There&#039;s nothing magical about float. Just bits that, together in a &quot;special&quot; format, represent some value. The XOR trick does not need to know the format, it just swaps the bits. (you could make your own format if you want, encryption for example, or compression, etc... and it would still work)

Also for the pointer-to-the-same-location case, this trick will also work. Operating directly on the pointer data is not a good concept by itself when doing computations -- because the compiler will not optimize it in registers. If you access the same memory subsequently it&#039;s better to use a temporary variable in C, so the compiler can make register optimizations (put it in registers, calculate on it, then put it in memory).

In this case it would work, since x and y would be on two registers or temporary variables.</description>
		<content:encoded><![CDATA[<p>There&#8217;s no reason why it wouldn&#8217;t work with floating point numbers, because they are just 1s and 0s as well.</p>
<p>This technique will swap the BITS, and everything made with BITS will work.</p>
<p>Sure you&#8217;ll first have to have a way to access float&#8217;s bits directly with XOR.</p>
<p>There&#8217;s nothing magical about float. Just bits that, together in a &#8220;special&#8221; format, represent some value. The XOR trick does not need to know the format, it just swaps the bits. (you could make your own format if you want, encryption for example, or compression, etc&#8230; and it would still work)</p>
<p>Also for the pointer-to-the-same-location case, this trick will also work. Operating directly on the pointer data is not a good concept by itself when doing computations &#8212; because the compiler will not optimize it in registers. If you access the same memory subsequently it&#8217;s better to use a temporary variable in C, so the compiler can make register optimizations (put it in registers, calculate on it, then put it in memory).</p>
<p>In this case it would work, since x and y would be on two registers or temporary variables.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
