<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	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/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Nisi's work blog &#187; CPU</title>
	<atom:link href="http://wiki.nisi.ro/tag/cpu/feed/" rel="self" type="application/rss+xml" />
	<link>http://wiki.nisi.ro</link>
	<description>Nisi's work blog - programing tips</description>
	<lastBuildDate>Mon, 21 Nov 2011 08:43:00 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1</generator>
		<item>
		<title>Interchanging 2 variables without the use of a third</title>
		<link>http://wiki.nisi.ro/2009/03/interchanging-2-variables-without-the-use-of-a-third/</link>
		<comments>http://wiki.nisi.ro/2009/03/interchanging-2-variables-without-the-use-of-a-third/#comments</comments>
		<pubDate>Mon, 09 Mar 2009 21:17:53 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[C/C++]]></category>
		<category><![CDATA[CPU]]></category>
		<category><![CDATA[interchanging]]></category>
		<category><![CDATA[memory]]></category>
		<category><![CDATA[speed]]></category>
		<category><![CDATA[variable]]></category>

		<guid isPermaLink="false">http://wiki.nisi.ro/?p=29</guid>
		<description><![CDATA[One of the interesting problems witch every programmer is faced when target device do not have so many memory availble is to interchange 2 variables widthout using a third one: Theory: a b a XOR b 0 0 0 0 1 1 1 0 1 1 1 0 . a 0101 b 0011 a = [...]]]></description>
			<content:encoded><![CDATA[<p>One of the interesting problems witch every programmer is faced when target device do not have so many memory availble is to interchange 2 variables widthout using a third one:</p>
<p><strong>Theory:</strong></p>
<table border="1" cellspacing="1" cellpadding="1" width="164" align="center">
<tbody>
<tr>
<td style="text-align: center;"><span style="font-size: x-small;">a</span></td>
<td style="text-align: center;"><span style="font-size: x-small;">b</span></td>
<td style="text-align: center;"><span style="font-size: x-small;">a XOR b</span></td>
</tr>
<tr>
<td style="text-align: center;"><span style="font-size: x-small;">0</span></td>
<td style="text-align: center;"><span style="font-size: x-small;">0</span></td>
<td style="text-align: center;"><span style="font-size: x-small;">0</span></td>
</tr>
<tr>
<td style="text-align: center;"><span style="font-size: x-small;">0</span></td>
<td style="text-align: center;"><span style="font-size: x-small;">1</span></td>
<td style="text-align: center;"><span style="font-size: x-small;">1</span></td>
</tr>
<tr>
<td style="text-align: center;"><span style="font-size: x-small;">1</span></td>
<td style="text-align: center;"><span style="font-size: x-small;">0</span></td>
<td style="text-align: center;"><span style="font-size: x-small;">1</span></td>
</tr>
<tr>
<td style="text-align: center;"><span style="font-size: x-small;">1</span></td>
<td style="text-align: center;"><span style="font-size: x-small;">1</span></td>
<td style="text-align: center;"><span style="font-size: x-small;">0<br />
</span></td>
</tr>
</tbody>
</table>
<p>.</p>
<table style="height: 108px;" border="1" cellspacing="1" cellpadding="1" width="165" align="center">
<tbody>
<tr>
<td><span style="font-size: x-small;">a</span></td>
<td style="text-align: center;"><span style="font-size: x-small;">0101</span></td>
</tr>
<tr>
<td><span style="font-size: x-small;">b</span></td>
<td style="text-align: center;"><span style="font-size: x-small;">0011</span></td>
</tr>
<tr>
<td><span style="font-size: x-small;">a = a XOR b</span></td>
<td style="text-align: center;"><span style="font-size: x-small;">0110</span></td>
</tr>
<tr>
<td><span style="font-size: x-small;">b = a XOR b</span></td>
<td style="text-align: center;"><span style="font-size: x-small;">0101</span></td>
</tr>
<tr>
<td><span style="font-size: x-small;">a = a XOR b</span></td>
<td style="text-align: center;"><span style="font-size: x-small;">0011</span></td>
</tr>
</tbody>
</table>
<div class="art_preview_content">a = a ^ b;    <span class="co1">// a will contain a xor b = 6</span><br />
b = a ^  b;    <span class="co1">// b will receive 6 xor 3 = 5</span><br />
a = a ^ b;     <span class="co1">// a will become 6 xor 5 = 3</span></p>
</div>
<div class="art_preview_content"><strong>other methods:</strong></div>
<div class="art_preview_content">a = a * b<br />
b = a / b<br />
a = a / b</div>
<div class="art_preview_content"><strong>or </strong></div>
<div class="art_preview_content">a = a + b<br />
b = a &#8211; b<br />
a = a &#8211; b</div>
<div class="art_preview_content">Every one is ok to use but if you want to program a microcontroller first one is perfect becouse use basic CPU operations XOR and is very fast.</div>
]]></content:encoded>
			<wfw:commentRss>http://wiki.nisi.ro/2009/03/interchanging-2-variables-without-the-use-of-a-third/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

