<?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: Using Bitwise XOR to exchange variable values in ActionScript</title>
	<atom:link href="http://www.mikechambers.com/blog/2007/10/22/using-bitwise-xor-to-exchange-variable-values-in-actionscript/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.mikechambers.com/blog/2007/10/22/using-bitwise-xor-to-exchange-variable-values-in-actionscript/</link>
	<description>code = joy</description>
	<lastBuildDate>Fri, 02 Dec 2011 01:36:37 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.3</generator>
	<item>
		<title>By: Allan Pichler</title>
		<link>http://www.mikechambers.com/blog/2007/10/22/using-bitwise-xor-to-exchange-variable-values-in-actionscript/comment-page-1/#comment-11946</link>
		<dc:creator>Allan Pichler</dc:creator>
		<pubDate>Thu, 17 Apr 2008 00:31:22 +0000</pubDate>
		<guid isPermaLink="false">http://www.mikechambers.com/blog/2007/10/22/using-bitwise-xor-to-exchange-variable-values-in-actionscript/#comment-11946</guid>
		<description>There are virtually unlimited places to make minor improvements of this type.... same way if you need to multiply fixed range numbers, instead of doing the multiplication you can get around it by using a pre-calculated table of log(x) values and accomplish the multiplication with a simple add which should be significantly faster. 

Simple logic behind that is that:

a * b = x

exp ( log(a) + log(b) ) =  x

log(a) + log(b) = log(x)

So by reading from a table of log values you can replace the multiplication with a simple add.

Obviously this is only an advantage when you multiply fairly small numbers, but nonetheless.... an advantage.

I used many many years ago to multiply 8bit numbers.

--Allan</description>
		<content:encoded><![CDATA[<p>There are virtually unlimited places to make minor improvements of this type&#8230;. same way if you need to multiply fixed range numbers, instead of doing the multiplication you can get around it by using a pre-calculated table of log(x) values and accomplish the multiplication with a simple add which should be significantly faster. </p>
<p>Simple logic behind that is that:</p>
<p>a * b = x</p>
<p>exp ( log(a) + log(b) ) =  x</p>
<p>log(a) + log(b) = log(x)</p>
<p>So by reading from a table of log values you can replace the multiplication with a simple add.</p>
<p>Obviously this is only an advantage when you multiply fairly small numbers, but nonetheless&#8230;. an advantage.</p>
<p>I used many many years ago to multiply 8bit numbers.</p>
<p>&#8211;Allan</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Doug</title>
		<link>http://www.mikechambers.com/blog/2007/10/22/using-bitwise-xor-to-exchange-variable-values-in-actionscript/comment-page-1/#comment-9505</link>
		<dc:creator>Doug</dc:creator>
		<pubDate>Wed, 24 Oct 2007 19:55:56 +0000</pubDate>
		<guid isPermaLink="false">http://www.mikechambers.com/blog/2007/10/22/using-bitwise-xor-to-exchange-variable-values-in-actionscript/#comment-9505</guid>
		<description>Good point Ryan. It should run the XORs faster on a RISC system, which are usually optimized for scientific functions. Tested on the my old G4 powerbook and it is the case, but still similar memory usage. Not much use in a practical sense outside a science lab now that most client machines for Flash are now Intel based... ActionScript and bitwise operators!! Are you going to start making Flex Karnaugh maps next Mike? :-)</description>
		<content:encoded><![CDATA[<p>Good point Ryan. It should run the XORs faster on a RISC system, which are usually optimized for scientific functions. Tested on the my old G4 powerbook and it is the case, but still similar memory usage. Not much use in a practical sense outside a science lab now that most client machines for Flash are now Intel based&#8230; ActionScript and bitwise operators!! Are you going to start making Flex Karnaugh maps next Mike? :-)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Phil Douglas</title>
		<link>http://www.mikechambers.com/blog/2007/10/22/using-bitwise-xor-to-exchange-variable-values-in-actionscript/comment-page-1/#comment-9491</link>
		<dc:creator>Phil Douglas</dc:creator>
		<pubDate>Wed, 24 Oct 2007 04:23:52 +0000</pubDate>
		<guid isPermaLink="false">http://www.mikechambers.com/blog/2007/10/22/using-bitwise-xor-to-exchange-variable-values-in-actionscript/#comment-9491</guid>
		<description>@ Samuel

Thats a pretty brutal interview question!</description>
		<content:encoded><![CDATA[<p>@ Samuel</p>
<p>Thats a pretty brutal interview question!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ryan Taylor</title>
		<link>http://www.mikechambers.com/blog/2007/10/22/using-bitwise-xor-to-exchange-variable-values-in-actionscript/comment-page-1/#comment-9486</link>
		<dc:creator>Ryan Taylor</dc:creator>
		<pubDate>Wed, 24 Oct 2007 01:38:18 +0000</pubDate>
		<guid isPermaLink="false">http://www.mikechambers.com/blog/2007/10/22/using-bitwise-xor-to-exchange-variable-values-in-actionscript/#comment-9486</guid>
		<description>Mike, I re-ran those tests and checked memory - it was the same each test, every time. So your hypothesis about the allocated memory seems to be accurate. I found an explanation for these results on wikipedia:

&quot;On modern (desktop) CPUs, the XOR technique is considerably slower than using a temporary variable to do swapping. One reason is that modern CPUs strive to execute commands in parallel; see Instruction pipeline. In the XOR technique, the inputs to each operation depend on the results of the previous operation, so they must be executed in strictly sequential order. If efficiency is of tremendous concern, it is advised to test the speeds of both the XOR technique and temporary variable swapping on the target architecture.&quot;</description>
		<content:encoded><![CDATA[<p>Mike, I re-ran those tests and checked memory &#8211; it was the same each test, every time. So your hypothesis about the allocated memory seems to be accurate. I found an explanation for these results on wikipedia:</p>
<p>&#8220;On modern (desktop) CPUs, the XOR technique is considerably slower than using a temporary variable to do swapping. One reason is that modern CPUs strive to execute commands in parallel; see Instruction pipeline. In the XOR technique, the inputs to each operation depend on the results of the previous operation, so they must be executed in strictly sequential order. If efficiency is of tremendous concern, it is advised to test the speeds of both the XOR technique and temporary variable swapping on the target architecture.&#8221;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Luke</title>
		<link>http://www.mikechambers.com/blog/2007/10/22/using-bitwise-xor-to-exchange-variable-values-in-actionscript/comment-page-1/#comment-9484</link>
		<dc:creator>Luke</dc:creator>
		<pubDate>Tue, 23 Oct 2007 21:58:47 +0000</pubDate>
		<guid isPermaLink="false">http://www.mikechambers.com/blog/2007/10/22/using-bitwise-xor-to-exchange-variable-values-in-actionscript/#comment-9484</guid>
		<description>Keep in mind. This only works on integers, not on floats.</description>
		<content:encoded><![CDATA[<p>Keep in mind. This only works on integers, not on floats.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: mikechambers</title>
		<link>http://www.mikechambers.com/blog/2007/10/22/using-bitwise-xor-to-exchange-variable-values-in-actionscript/comment-page-1/#comment-9483</link>
		<dc:creator>mikechambers</dc:creator>
		<pubDate>Tue, 23 Oct 2007 20:24:51 +0000</pubDate>
		<guid isPermaLink="false">http://www.mikechambers.com/blog/2007/10/22/using-bitwise-xor-to-exchange-variable-values-in-actionscript/#comment-9483</guid>
		<description>&gt;Out of pure curiosity, I just ran some performance tests to

Wow. That surprises me a little, as I would expect the XOR to be a little faster...

Can you run your example and look at memory usage?

flash.system.System.totalMemory


I ran it with a simple test (one exchange) and saw no difference, but I assumed that was because the player had been allocated memory by the OS and my code didnt go over that.

mike chambers

mesh@adobe.com</description>
		<content:encoded><![CDATA[<p>>Out of pure curiosity, I just ran some performance tests to</p>
<p>Wow. That surprises me a little, as I would expect the XOR to be a little faster&#8230;</p>
<p>Can you run your example and look at memory usage?</p>
<p>flash.system.System.totalMemory</p>
<p>I ran it with a simple test (one exchange) and saw no difference, but I assumed that was because the player had been allocated memory by the OS and my code didnt go over that.</p>
<p>mike chambers</p>
<p><a href="mailto:mesh@adobe.com">mesh@adobe.com</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ryan Taylor</title>
		<link>http://www.mikechambers.com/blog/2007/10/22/using-bitwise-xor-to-exchange-variable-values-in-actionscript/comment-page-1/#comment-9482</link>
		<dc:creator>Ryan Taylor</dc:creator>
		<pubDate>Tue, 23 Oct 2007 19:34:17 +0000</pubDate>
		<guid isPermaLink="false">http://www.mikechambers.com/blog/2007/10/22/using-bitwise-xor-to-exchange-variable-values-in-actionscript/#comment-9482</guid>
		<description>Out of pure curiosity, I just ran some performance tests to see how the two approaches compare. All three tests that I ran were inside a 10 million iteration for-loop. Results are in milliseconds.

Test #1:

c = a;
a = b;
b = c;

Time: 42 ms

--

Test #2:

a ^= b;
b ^= a;

Time: 49ms

--

Test #3:

var d:int = a;
a = b;
b = d;

Time: 48ms

--

So in light of the better performance and code clarity, I would definitely recommend sticking with the temporary variable approach. Thanks for sharing that though Mike, I had not seen that before.</description>
		<content:encoded><![CDATA[<p>Out of pure curiosity, I just ran some performance tests to see how the two approaches compare. All three tests that I ran were inside a 10 million iteration for-loop. Results are in milliseconds.</p>
<p>Test #1:</p>
<p>c = a;<br />
a = b;<br />
b = c;</p>
<p>Time: 42 ms</p>
<p>&#8211;</p>
<p>Test #2:</p>
<p>a ^= b;<br />
b ^= a;</p>
<p>Time: 49ms</p>
<p>&#8211;</p>
<p>Test #3:</p>
<p>var d:int = a;<br />
a = b;<br />
b = d;</p>
<p>Time: 48ms</p>
<p>&#8211;</p>
<p>So in light of the better performance and code clarity, I would definitely recommend sticking with the temporary variable approach. Thanks for sharing that though Mike, I had not seen that before.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Julien</title>
		<link>http://www.mikechambers.com/blog/2007/10/22/using-bitwise-xor-to-exchange-variable-values-in-actionscript/comment-page-1/#comment-9480</link>
		<dc:creator>Julien</dc:creator>
		<pubDate>Tue, 23 Oct 2007 16:43:02 +0000</pubDate>
		<guid isPermaLink="false">http://www.mikechambers.com/blog/2007/10/22/using-bitwise-xor-to-exchange-variable-values-in-actionscript/#comment-9480</guid>
		<description>Very interesting, thanks</description>
		<content:encoded><![CDATA[<p>Very interesting, thanks</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Tink</title>
		<link>http://www.mikechambers.com/blog/2007/10/22/using-bitwise-xor-to-exchange-variable-values-in-actionscript/comment-page-1/#comment-9479</link>
		<dc:creator>Tink</dc:creator>
		<pubDate>Tue, 23 Oct 2007 15:04:15 +0000</pubDate>
		<guid isPermaLink="false">http://www.mikechambers.com/blog/2007/10/22/using-bitwise-xor-to-exchange-variable-values-in-actionscript/#comment-9479</guid>
		<description>Like others mention clarity in code is really important, but nevertheless this is interesting, thanks.</description>
		<content:encoded><![CDATA[<p>Like others mention clarity in code is really important, but nevertheless this is interesting, thanks.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Samuel Neff</title>
		<link>http://www.mikechambers.com/blog/2007/10/22/using-bitwise-xor-to-exchange-variable-values-in-actionscript/comment-page-1/#comment-9478</link>
		<dc:creator>Samuel Neff</dc:creator>
		<pubDate>Tue, 23 Oct 2007 13:11:22 +0000</pubDate>
		<guid isPermaLink="false">http://www.mikechambers.com/blog/2007/10/22/using-bitwise-xor-to-exchange-variable-values-in-actionscript/#comment-9478</guid>
		<description>Even in a loop you&#039;re still only using one additional memory location--reusing the same one over and over again.  I don&#039;t think there are any practical applications, especially in ActionScript, where it&#039;s necessary to avoid the extra memory loop.  A lot of people ask questions like this in interviews and that&#039;s pretty much all it&#039;s good for, trivia.

Draco,

The +/- operation seems to do the same thing but doesn&#039;t work in edge conditions (values close to min/max number).

Sam</description>
		<content:encoded><![CDATA[<p>Even in a loop you&#8217;re still only using one additional memory location&#8211;reusing the same one over and over again.  I don&#8217;t think there are any practical applications, especially in ActionScript, where it&#8217;s necessary to avoid the extra memory loop.  A lot of people ask questions like this in interviews and that&#8217;s pretty much all it&#8217;s good for, trivia.</p>
<p>Draco,</p>
<p>The +/- operation seems to do the same thing but doesn&#8217;t work in edge conditions (values close to min/max number).</p>
<p>Sam</p>
]]></content:encoded>
	</item>
</channel>
</rss>

