<?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>Eoin Griffin &#187; Code hints</title>
	<atom:link href="http://www.eoingriffin.com/tag/code-hints/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.eoingriffin.com</link>
	<description>Web/Multimedia Design &#38; Development Portfolio</description>
	<lastBuildDate>Mon, 14 Mar 2011 08:30:51 +0000</lastBuildDate>
	
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Useful hints: #1 &#8211; Odd/Even numbers (Modulo operator [%] and Bitwise And [&amp;])</title>
		<link>http://www.eoingriffin.com/code-hints/useful-hints-1-oddeven-numbers-modulo-operator/</link>
		<comments>http://www.eoingriffin.com/code-hints/useful-hints-1-oddeven-numbers-modulo-operator/#comments</comments>
		<pubDate>Thu, 11 Mar 2010 19:21:50 +0000</pubDate>
		<dc:creator>Eoin</dc:creator>
				<category><![CDATA[Code hints]]></category>
		<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[modulo]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.eoingriffin.com/?p=212</guid>
		<description><![CDATA[I&#8217;m going to start posting some useful little bits of code up here as i think of them.
This is partly to help with my exceptionally poor memory, and partly to help out anyone who might be interested. I encourage anyone else who knows of some similar little helpful tips to send them to me and [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m going to start posting some useful little bits of code up here as i think of them.<br />
This is partly to help with my exceptionally poor memory, and partly to help out anyone who might be interested. I encourage anyone else who knows of some similar little helpful tips to send them to me and i&#8217;ll post them up here.</p>
<p>Ok, so here&#8217;s a nice simple one to start off. What&#8217;s an easy and simple method to determine if a number is odd or even?<br />
The answer lies with the modulo operator (%) or the bitwise AND operator (&#038;).</p>
<div class="codecolorer-container actionscript3 dawn" 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="actionscript3 codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">i<span style="color: #000066; font-weight: bold;">%</span>2</div></td></tr></tbody></table></div>
<p>Whatever <strong>i</strong> is, <strong>i%2</strong> will evaluate to either 0 or 1.<br />
Or a faster method for determining odd or even numbers would be the Bitwise AND operator (represented by a single ampersand: &#038;):</p>
<div class="codecolorer-container actionscript3 dawn" 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="actionscript3 codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">i<span style="color: #000066; font-weight: bold;">&amp;</span><span style="color: #000000; font-weight:bold;">1</span></div></td></tr></tbody></table></div>
<p>So what use is this?<br />
<span id="more-212"></span><br />
Let&#8217;s say you&#8217;ve got a for loop:</p>
<div class="codecolorer-container actionscript3 dawn" 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 /></div></td><td><div class="actionscript3 codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0033ff; font-weight: bold;">for</span><span style="color: #000000;">&#40;</span><span style="color: #6699cc; font-weight: bold;">var</span> i<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=int%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:int.html"><span style="color: #004993;">int</span></a> = <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">;</span> i <span style="color: #000066; font-weight: bold;">&amp;</span>lt<span style="color: #000066; font-weight: bold;">;</span> <span style="color: #000000; font-weight:bold;">50</span><span style="color: #000066; font-weight: bold;">;</span> i<span style="color: #000066; font-weight: bold;">++</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span>i<span style="color: #000066; font-weight: bold;">%</span>2<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span> <span style="color: #009900; font-style: italic;">// Or i&amp;1</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900; font-style: italic;">//do something for odd numbers</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#125;</span><span style="color: #0033ff; font-weight: bold;">else</span><span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900; font-style: italic;">//do something else for even numbers</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
<span style="color: #000000;">&#125;</span></div></td></tr></tbody></table></div>
<p>So why would we use the modulo operator if a bitwise AND is faster?<br />
To take this further, you could specify separate code for every nth number:</p>
<div class="codecolorer-container actionscript3 dawn" 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 />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br /></div></td><td><div class="actionscript3 codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0033ff; font-weight: bold;">for</span><span style="color: #000000;">&#40;</span><span style="color: #6699cc; font-weight: bold;">var</span> i<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=int%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:int.html"><span style="color: #004993;">int</span></a> = <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">;</span> i <span style="color: #000066; font-weight: bold;">&amp;</span>lt<span style="color: #000066; font-weight: bold;">;</span> <span style="color: #000000; font-weight:bold;">50</span><span style="color: #000066; font-weight: bold;">;</span> i<span style="color: #000066; font-weight: bold;">++</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0033ff; font-weight: bold;">switch</span><span style="color: #000000;">&#40;</span>i<span style="color: #000066; font-weight: bold;">%</span>4<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0033ff; font-weight: bold;">case</span> <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #004993;">trace</span> <span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;run every 4 iterations starting from 0&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0033ff; font-weight: bold;">break</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0033ff; font-weight: bold;">case</span> <span style="color: #000000; font-weight:bold;">1</span><span style="color: #000066; font-weight: bold;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #004993;">trace</span> <span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;run every 4 iterations starting from 1&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0033ff; font-weight: bold;">break</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0033ff; font-weight: bold;">case</span> <span style="color: #000000; font-weight:bold;">2</span><span style="color: #000066; font-weight: bold;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #004993;">trace</span> <span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;run every 4 iterations starting from 2&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0033ff; font-weight: bold;">break</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0033ff; font-weight: bold;">case</span> <span style="color: #000000; font-weight:bold;">3</span><span style="color: #000066; font-weight: bold;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #004993;">trace</span> <span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;run every 4 iterations starting from 3&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0033ff; font-weight: bold;">break</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
<span style="color: #000000;">&#125;</span></div></td></tr></tbody></table></div>
<p><strong>UPDATE</strong><br />
Some supporting code to show the speed difference.</p>
<p><em>Results in milliseconds:</em><br />
Bitwise &#038; : 2809<br />
Modulo : 3456<br />
Bitwise shift : 2801<br />
Simple division : 2792</p>
<div class="codecolorer-container text dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><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 />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br />38<br />39<br />40<br />41<br />42<br />43<br />44<br />45<br />46<br />47<br />48<br />49<br />50<br />51<br />52<br />53<br />54<br />55<br />56<br />57<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">package<br />
{<br />
&nbsp; &nbsp; import flash.display.Sprite;<br />
&nbsp; &nbsp; import flash.utils.getTimer;<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; public class SpeedTest extends Sprite<br />
&nbsp; &nbsp; {<br />
&nbsp; &nbsp; &nbsp; &nbsp; public function SpeedTest()<br />
&nbsp; &nbsp; &nbsp; &nbsp; {<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var bAndResult:int = ( runTest( bAnd, 10000000 ) );<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; trace(&quot;Bitwise &amp; :&quot;, bAndResult);<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var modResult:int = ( runTest( mod, 10000000 ) );<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; trace(&quot;Modulo :&quot;, modResult);<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var shiftResult:int = ( runTest( bShift, 10000000 ) );<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; trace(&quot;Bitwise shift :&quot;, shiftResult);<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var divResult:int = ( runTest( div, 10000000 ) );<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; trace(&quot;Simple division :&quot;, divResult);<br />
&nbsp; &nbsp; &nbsp; &nbsp; }<br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; private function runTest( fnc:Function, count:uint ):int<br />
&nbsp; &nbsp; &nbsp; &nbsp; {<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var start:int = getTimer();<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fnc( count );<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return getTimer() - start;<br />
&nbsp; &nbsp; &nbsp; &nbsp; }<br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; private function bAnd( count:uint ):void<br />
&nbsp; &nbsp; &nbsp; &nbsp; {<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for( var i:uint = 0; i &lt; count; ++i )<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var bleh:int = i &amp; 1;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br />
&nbsp; &nbsp; &nbsp; &nbsp; }<br />
&nbsp; &nbsp; &nbsp; &nbsp; private function mod( count:uint ):void<br />
&nbsp; &nbsp; &nbsp; &nbsp; {<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for( var i:uint = 0; i &lt; count; ++i )<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var bleh:int = i % 2;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br />
&nbsp; &nbsp; &nbsp; &nbsp; }<br />
&nbsp; &nbsp; &nbsp; &nbsp; private function bShift( count:uint ):void<br />
&nbsp; &nbsp; &nbsp; &nbsp; {<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for( var i:uint = 0; i &lt; count; ++i )<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var bleh:int = i &gt;&gt; 1;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br />
&nbsp; &nbsp; &nbsp; &nbsp; }<br />
&nbsp; &nbsp; &nbsp; &nbsp; private function div( count:uint ):void<br />
&nbsp; &nbsp; &nbsp; &nbsp; {<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for( var i:uint = 0; i &lt; count; ++i )<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var bleh:int = i / 2;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br />
&nbsp; &nbsp; &nbsp; &nbsp; }<br />
&nbsp; &nbsp; }<br />
}</div></td></tr></tbody></table></div>]]></content:encoded>
			<wfw:commentRss>http://www.eoingriffin.com/code-hints/useful-hints-1-oddeven-numbers-modulo-operator/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

