<?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>Thu, 15 Mar 2012 14:35:34 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Bit Operations</title>
		<link>http://codemocha.com/2011/09/bit-operations-2/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=bit-operations-2</link>
		<comments>http://codemocha.com/2011/09/bit-operations-2/#comments</comments>
		<pubDate>Sat, 10 Sep 2011 22:41:28 +0000</pubDate>
		<dc:creator>Chet</dc:creator>
				<category><![CDATA[Code]]></category>

		<guid isPermaLink="false">http://codemocha.com/?p=42</guid>
		<description><![CDATA[I got into a discussion with a fellow student today who worked at Microsoft. His product manager told him not to use bit operations because it&#8217;s important to keep code readable. Code should be self-documenting. I agree with this, but when you can perform functions with a couple lines of bit operations, it can be [...]]]></description>
			<content:encoded><![CDATA[<p>I got into a discussion with a fellow student today who worked at Microsoft. His product manager told him not to use bit operations because it&#8217;s important to keep code readable. Code should be self-documenting. I agree with this, but when you can perform functions with a couple lines of bit operations, it can be a beautiful thing.</p>
<p>Here&#8217;s how to determine if a number is a power of two:</p>
<div class="codecolorer-container text vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">(n &amp; (n - 1)) == 0</div></td></tr></tbody></table></div>
<p>Of course, this sets 0 to be a power of two as well. I found this <a href="http://graphics.stanford.edu/~seander/bithacks.html">site</a> which recommends using this instead which fixes this.</p>
<div class="codecolorer-container text vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">v &amp;&amp; !(v &amp; (v - 1))</div></td></tr></tbody></table></div>
]]></content:encoded>
			<wfw:commentRss>http://codemocha.com/2011/09/bit-operations-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Beatitudes in C#</title>
		<link>http://codemocha.com/2009/07/beatitudes-in-c/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=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;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><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="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #6666cc; font-weight: bold;">class</span> the_earth<br />
<span style="color: #008000;">&#123;</span><br />
<span style="color: #008000;">...</span><br />
<span style="color: #008000;">&#125;</span><span style="color: #008000;">;</span><br />
<span style="color: #6666cc; font-weight: bold;">class</span> the_meek <span style="color: #008000;">:</span> <span style="color: #0600FF; font-weight: bold;">public</span> the_earth<br />
<span style="color: #008000;">&#123;</span><br />
<span style="color: #008000;">...</span><br />
<span style="color: #008000;">&#125;</span><span style="color: #008000;">;</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/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=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 ~ and [...]]]></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;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br /></div></td><td><div class="c codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666; font-style: italic;">//NOR operation using only ~ and &amp;</span><br />
<span style="color: #993333;">int</span> bitNor<span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> x<span style="color: #339933;">,</span> <span style="color: #993333;">int</span> y<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span>~x<span style="color: #009900;">&#41;</span><span style="color: #339933;">&amp;</span><span style="color: #009900;">&#40;</span>~y<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#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;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br /></div></td><td><div class="c codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666; font-style: italic;">//XOR operation using only ~ and &amp;</span><br />
<span style="color: #993333;">int</span> bitXor<span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> x<span style="color: #339933;">,</span> <span style="color: #993333;">int</span> y<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span>~<span style="color: #009900;">&#40;</span>x<span style="color: #339933;">&amp;</span>y<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">&amp;</span><span style="color: #009900;">&#40;</span>~<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>~x<span style="color: #009900;">&#41;</span><span style="color: #339933;">&amp;</span><span style="color: #009900;">&#40;</span>~y<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<div class="codecolorer-container c vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br /></div></td><td><div class="c codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666; font-style: italic;">//Return true if x is not equal to y not using comparison operators.</span><br />
<span style="color: #993333;">int</span> isNotEqual<span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> x<span style="color: #339933;">,</span> <span style="color: #993333;">int</span> y<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #339933;">!!</span><span style="color: #009900;">&#40;</span>x<span style="color: #339933;">^</span>y<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#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;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br /></div></td><td><div class="c codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333;">int</span> rightLogicalShift<span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> x<span style="color: #339933;">,</span> <span style="color: #993333;">int</span> n<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #993333;">int</span> allOnes <span style="color: #339933;">=</span> ~<span style="color: #0000dd;">0</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #993333;">int</span> y <span style="color: #339933;">=</span> ~<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>allOnes<span style="color: #009900;">&#41;</span> <span style="color: #339933;">&lt;&lt;</span> <span style="color: #009900;">&#40;</span><span style="color: #0000dd;">32</span> <span style="color: #339933;">+</span> <span style="color: #009900;">&#40;</span>~n <span style="color: #339933;">+</span> <span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>x <span style="color: #339933;">&gt;&gt;</span> n<span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;</span> y<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#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>

