<?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>Chakkrit Tantithamthavorn&#039;s Official Personal Website &#187; xor</title>
	<atom:link href="http://www.klainfo.com/tag/xor/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.klainfo.com</link>
	<description></description>
	<lastBuildDate>Wed, 01 Feb 2012 13:12:31 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Method Swap &#8211; without temporary variable</title>
		<link>http://www.klainfo.com/2009/06/10/method-swap-without-temporary-variable/</link>
		<comments>http://www.klainfo.com/2009/06/10/method-swap-without-temporary-variable/#comments</comments>
		<pubDate>Wed, 10 Jun 2009 07:48:52 +0000</pubDate>
		<dc:creator>klainfo</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[swap]]></category>
		<category><![CDATA[xor]]></category>

		<guid isPermaLink="false">http://blog.klainfo.com/?p=71</guid>
		<description><![CDATA[ตัวอย่างข้างต้น เป็นการเขียนโปรแกรม swap ค่าแบบแปลกๆครับ ไม่ต้องมาสร้างตัวแปร temp อีกแย๊ว จะนำเสนอ 2 วิธีครับ วิธีแรก :: การใช้ Operator XOR ตัวอย่าง code สำหรับ ภาษา C# 1 2 3 4 5 void swap &#40;ref int a, ref int b&#41; &#123; a ^= b; b ^= a; a ^= b; &#125; ตัวอย่าง code สำหรับ ภาษา C/C++ 1 2 3 4 5 void [...]]]></description>
			<content:encoded><![CDATA[<p>ตัวอย่างข้างต้น เป็นการเขียนโปรแกรม swap ค่าแบบแปลกๆครับ ไม่ต้องมาสร้างตัวแปร temp อีกแย๊ว<br />
จะนำเสนอ 2 วิธีครับ</p>
<p><strong><span style="color:#ff0000;">วิธีแรก :: การใช้ Operator XOR</span><br />
ตัวอย่าง code สำหรับ ภาษา C#</strong></p>
<div class="wp_syntax">
<table>
<tr>
<td class="line_numbers">
<pre>1
2
3
4
5
</pre>
</td>
<td class="code">
<pre class="csharp" style="font-family:monospace;"><span style="color: #6666cc; font-weight: bold;">void</span> swap <span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">ref</span> <span style="color: #6666cc; font-weight: bold;">int</span> a, <span style="color: #0600FF; font-weight: bold;">ref</span> <span style="color: #6666cc; font-weight: bold;">int</span> b<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
     a <span style="color: #008000;">^=</span> b<span style="color: #008000;">;</span>
     b <span style="color: #008000;">^=</span> a<span style="color: #008000;">;</span>
     a <span style="color: #008000;">^=</span> b<span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre>
</td>
</tr>
</table>
</div>
<p><strong>ตัวอย่าง code สำหรับ ภาษา C/C++</strong></p>
<div class="wp_syntax">
<table>
<tr>
<td class="line_numbers">
<pre>1
2
3
4
5
</pre>
</td>
<td class="code">
<pre class="c" style="font-family:monospace;"><span style="color: #993333;">void</span> swap <span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> <span style="color: #339933;">&amp;</span>a<span style="color: #339933;">,</span> <span style="color: #993333;">int</span> <span style="color: #339933;">&amp;</span>b<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
     a <span style="color: #339933;">^=</span> b<span style="color: #339933;">;</span>
     b <span style="color: #339933;">^=</span> a<span style="color: #339933;">;</span>
     a <span style="color: #339933;">^=</span> b<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre>
</td>
</tr>
</table>
</div>
<p><strong><span style="color:#ff0000;">วิธีที่สอง :: การใช้ Operator +- ค่าเข้าออกจากตัวแปร</span><br />
ตัวอย่าง code สำหรับ ภาษา C#</strong></p>
<div class="wp_syntax">
<table>
<tr>
<td class="line_numbers">
<pre>1
2
3
4
5
</pre>
</td>
<td class="code">
<pre class="csharp" style="font-family:monospace;"><span style="color: #6666cc; font-weight: bold;">void</span> swap <span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">ref</span> <span style="color: #6666cc; font-weight: bold;">int</span> a, <span style="color: #0600FF; font-weight: bold;">ref</span> <span style="color: #6666cc; font-weight: bold;">int</span> b<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
     b <span style="color: #008000;">+=</span> a<span style="color: #008000;">;</span>
     a <span style="color: #008000;">=</span> b − a<span style="color: #008000;">;</span>
     b −<span style="color: #008000;">=</span> a<span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre>
</td>
</tr>
</table>
</div>
<p><strong>ตัวอย่าง code สำหรับ ภาษา C/C++</strong></p>
<div class="wp_syntax">
<table>
<tr>
<td class="line_numbers">
<pre>1
2
3
4
5
</pre>
</td>
<td class="code">
<pre class="c" style="font-family:monospace;"><span style="color: #993333;">void</span> swap <span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> <span style="color: #339933;">&amp;</span>a<span style="color: #339933;">,</span> <span style="color: #993333;">int</span> <span style="color: #339933;">&amp;</span>b<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
     b <span style="color: #339933;">+=</span> a<span style="color: #339933;">;</span>
     a <span style="color: #339933;">=</span> b − a<span style="color: #339933;">;</span>
     b −<span style="color: #339933;">=</span> a<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre>
</td>
</tr>
</table>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.klainfo.com/2009/06/10/method-swap-without-temporary-variable/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Served from: www.klainfo.com @ 2012-02-06 19:25:37 -->
