<?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>CodeMocha &#187; Code</title>
	<atom:link href="http://codemocha.com/category/code/feed/" rel="self" type="application/rss+xml" />
	<link>http://codemocha.com</link>
	<description>Freshly ground code.  Thoughts on software and algorithms by Chet Mancini</description>
	<lastBuildDate>Fri, 18 Sep 2009 15:22:50 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Beatitudes in C#</title>
		<link>http://codemocha.com/2009/07/beatitudes-in-c/</link>
		<comments>http://codemocha.com/2009/07/beatitudes-in-c/#comments</comments>
		<pubDate>Sun, 05 Jul 2009 01:26:04 +0000</pubDate>
		<dc:creator>Chet</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[fun]]></category>
		<category><![CDATA[humor]]></category>

		<guid isPermaLink="false">http://codemocha.com/?p=29</guid>
		<description><![CDATA[12345678class the_earth
&#123;
...
&#125;;
class the_meek : public the_earth
&#123;
...
&#125;;
From: StackOverflow answer
]]></description>
			<content:encoded><![CDATA[<div class="codecolorer-container csharp vibrant" style="overflow:auto;white-space:nowrap;width:600px"><table cellspacing="0" cellpadding="0"><tbody><tr><td class="line-numbers"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br /></div></td><td><div class="csharp codecolorer" style="font-family:Monaco,Lucida Console,monospace"><span class="kw4">class</span> the_earth<br />
<span class="br0">&#123;</span><br />
...<br />
<span class="br0">&#125;</span><span class="sy0">;</span><br />
<span class="kw4">class</span> the_meek <span class="sy0">:</span> <span class="kw1">public</span> the_earth<br />
<span class="br0">&#123;</span><br />
...<br />
<span class="br0">&#125;</span><span class="sy0">;</span></div></td></tr></tbody></table></div>
<p>From: <a href="http://stackoverflow.com/questions/660972/what-is-your-best-pseudo-code-phrase/664411#664411">StackOverflow answer</a></p>
]]></content:encoded>
			<wfw:commentRss>http://codemocha.com/2009/07/beatitudes-in-c/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Bit Operations</title>
		<link>http://codemocha.com/2009/07/bit-operations/</link>
		<comments>http://codemocha.com/2009/07/bit-operations/#comments</comments>
		<pubDate>Sat, 04 Jul 2009 18:31:41 +0000</pubDate>
		<dc:creator>Chet</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[bits]]></category>
		<category><![CDATA[c]]></category>
		<category><![CDATA[function]]></category>

		<guid isPermaLink="false">http://codemocha.com/?p=22</guid>
		<description><![CDATA[For CS351, Intro to Systems, we had to do several operations using bit operators only.  These took awhile, but are pretty cool to show off.  We were graded by how few operations we used to return the right values for the generated test cases, including some difficult boundary ones.
1234//NOR operation using only ~ [...]]]></description>
			<content:encoded><![CDATA[<p>For CS351, Intro to Systems, we had to do several operations using bit operators only.  These took awhile, but are pretty cool to show off.  We were graded by how few operations we used to return the right values for the generated test cases, including some difficult boundary ones.<span id="more-22"></span></p>
<div class="codecolorer-container c vibrant" style="overflow:auto;white-space:nowrap;width:600px"><table cellspacing="0" cellpadding="0"><tbody><tr><td class="line-numbers"><div>1<br />2<br />3<br />4<br /></div></td><td><div class="c codecolorer" style="font-family:Monaco,Lucida Console,monospace"><span class="co1">//NOR operation using only ~ and &amp;</span><br />
<span class="kw4">int</span> bitNor<span class="br0">&#40;</span><span class="kw4">int</span> x<span class="sy0">,</span> <span class="kw4">int</span> y<span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="kw1">return</span> <span class="br0">&#40;</span>~x<span class="br0">&#41;</span><span class="sy0">&amp;</span><span class="br0">&#40;</span>~y<span class="br0">&#41;</span><span class="sy0">;</span><br />
<span class="br0">&#125;</span></div></td></tr></tbody></table></div>
<p>The XOR operation is equivalent to the (x NAND y) AND (x OR y).</p>
<div class="codecolorer-container c vibrant" style="overflow:auto;white-space:nowrap;width:600px"><table cellspacing="0" cellpadding="0"><tbody><tr><td class="line-numbers"><div>1<br />2<br />3<br />4<br /></div></td><td><div class="c codecolorer" style="font-family:Monaco,Lucida Console,monospace"><span class="co1">//XOR operation using only ~ and &amp;</span><br />
<span class="kw4">int</span> bitXor<span class="br0">&#40;</span><span class="kw4">int</span> x<span class="sy0">,</span> <span class="kw4">int</span> y<span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="kw1">return</span> <span class="br0">&#40;</span>~<span class="br0">&#40;</span>x<span class="sy0">&amp;</span>y<span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">&amp;</span><span class="br0">&#40;</span>~<span class="br0">&#40;</span><span class="br0">&#40;</span>~x<span class="br0">&#41;</span><span class="sy0">&amp;</span><span class="br0">&#40;</span>~y<span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span><br />
<span class="br0">&#125;</span></div></td></tr></tbody></table></div>
<div class="codecolorer-container c vibrant" style="overflow:auto;white-space:nowrap;width:600px"><table cellspacing="0" cellpadding="0"><tbody><tr><td class="line-numbers"><div>1<br />2<br />3<br />4<br /></div></td><td><div class="c codecolorer" style="font-family:Monaco,Lucida Console,monospace"><span class="co1">//Return true if x is not equal to y not using comparison operators.</span><br />
<span class="kw4">int</span> isNotEqual<span class="br0">&#40;</span><span class="kw4">int</span> x<span class="sy0">,</span> <span class="kw4">int</span> y<span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="kw1">return</span> <span class="sy0">!!</span><span class="br0">&#40;</span>x<span class="sy0">^</span>y<span class="br0">&#41;</span><span class="sy0">;</span><br />
<span class="br0">&#125;</span></div></td></tr></tbody></table></div>
<p>There are two types of shifts, arithmetic and logical.  Left shifts are the same in both types, but right shifts are different.  Logical shifts insert zeroes to fill in the empty spaces to keep the logic of the bits, but arithmetic shifts fill in the value of the sign bit to preserve the sign of the operand in twos-complement integers.</p>
<div class="codecolorer-container c vibrant" style="overflow:auto;white-space:nowrap;width:600px"><table cellspacing="0" cellpadding="0"><tbody><tr><td class="line-numbers"><div>1<br />2<br />3<br />4<br />5<br /></div></td><td><div class="c codecolorer" style="font-family:Monaco,Lucida Console,monospace"><span class="kw4">int</span> rightLogicalShift<span class="br0">&#40;</span><span class="kw4">int</span> x<span class="sy0">,</span> <span class="kw4">int</span> n<span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="kw4">int</span> allOnes <span class="sy0">=</span> ~<span class="nu0">0</span><span class="sy0">;</span><br />
&nbsp; &nbsp; <span class="kw4">int</span> y <span class="sy0">=</span> ~<span class="br0">&#40;</span><span class="br0">&#40;</span>allOnes<span class="br0">&#41;</span> <span class="sy0">&lt;&lt;</span> <span class="br0">&#40;</span><span class="nu0">32</span> <span class="sy0">+</span> <span class="br0">&#40;</span>~n <span class="sy0">+</span> <span class="nu0">1</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span><br />
&nbsp; &nbsp; <span class="kw1">return</span> <span class="br0">&#40;</span><span class="br0">&#40;</span>x <span class="sy0">&gt;&gt;</span> n<span class="br0">&#41;</span> <span class="sy0">&amp;</span> y<span class="br0">&#41;</span><span class="sy0">;</span><br />
<span class="br0">&#125;</span></div></td></tr></tbody></table></div>
]]></content:encoded>
			<wfw:commentRss>http://codemocha.com/2009/07/bit-operations/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
