<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
		>
<channel>
	<title>Comments on: Ten Ways to Check if an Integer Is a Power Of Two in C</title>
	<atom:link href="http://www.exploringbinary.com/ten-ways-to-check-if-an-integer-is-a-power-of-two-in-c/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.exploringbinary.com/ten-ways-to-check-if-an-integer-is-a-power-of-two-in-c/</link>
	<description>Binary Numbers, Binary Code, and Binary Logic</description>
	<lastBuildDate>Mon, 06 Sep 2010 19:40:04 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
	<item>
		<title>By: Find if a number is a power of two, v2 &#171; Kami Kode</title>
		<link>http://www.exploringbinary.com/ten-ways-to-check-if-an-integer-is-a-power-of-two-in-c/comment-page-1/#comment-4236</link>
		<dc:creator>Find if a number is a power of two, v2 &#171; Kami Kode</dc:creator>
		<pubDate>Wed, 21 Oct 2009 22:52:51 +0000</pubDate>
		<guid isPermaLink="false">http://www.exploringbinary.com/?p=207#comment-4236</guid>
		<description>[...] to how to find using only bitwise operators, if a number is a power of two. This site led me to another site with 10 different ways to check if an integer is a power of two; the one I need is a slight [...]</description>
		<content:encoded><![CDATA[<p>[...] to how to find using only bitwise operators, if a number is a power of two. This site led me to another site with 10 different ways to check if an integer is a power of two; the one I need is a slight [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Rick Regan</title>
		<link>http://www.exploringbinary.com/ten-ways-to-check-if-an-integer-is-a-power-of-two-in-c/comment-page-1/#comment-106</link>
		<dc:creator>Rick Regan</dc:creator>
		<pubDate>Tue, 03 Feb 2009 12:47:26 +0000</pubDate>
		<guid isPermaLink="false">http://www.exploringbinary.com/?p=207#comment-106</guid>
		<description>Jeff,

I ran them on an Intel Core Duo processor T2050, 1.60 GHz. But that&#039;s only part of the difference. I didn&#039;t mention that my loop that called each function 2^32 times also included a verification that the result was correct (seems kind of recursive, doesn&#039;t it?). I really shouldn&#039;t have left the checks in after I verified the algorithms, so I repeated the runs without them and &lt;strong&gt;updated the tables above&lt;/strong&gt;.  The algorithms still perform in the same relative order.

As an aside, I also re-ran your &quot;table method&quot; algorithm against #7 without short-circuiting the while loops (so counted all 1 bits).  Your table algorithm did indeed perform better in that case.</description>
		<content:encoded><![CDATA[<p>Jeff,</p>
<p>I ran them on an Intel Core Duo processor T2050, 1.60 GHz. But that&#8217;s only part of the difference. I didn&#8217;t mention that my loop that called each function 2^32 times also included a verification that the result was correct (seems kind of recursive, doesn&#8217;t it?). I really shouldn&#8217;t have left the checks in after I verified the algorithms, so I repeated the runs without them and <strong>updated the tables above</strong>.  The algorithms still perform in the same relative order.</p>
<p>As an aside, I also re-ran your &#8220;table method&#8221; algorithm against #7 without short-circuiting the while loops (so counted all 1 bits).  Your table algorithm did indeed perform better in that case.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jeff</title>
		<link>http://www.exploringbinary.com/ten-ways-to-check-if-an-integer-is-a-power-of-two-in-c/comment-page-1/#comment-105</link>
		<dc:creator>Jeff</dc:creator>
		<pubDate>Mon, 02 Feb 2009 17:58:38 +0000</pubDate>
		<guid isPermaLink="false">http://www.exploringbinary.com/?p=207#comment-105</guid>
		<description>Ii confess my surprise at the lookup table&#039;s poor performance.  

So I tried it on my machine (Mac w/ 2.16 GHz Intel Cor 2 Duo,  and gcc 4.0.1 (Apple Inc. build 5465, using only default options).  

I also added a modified lookup function where the lookup array was a global.  I repeated the three cases (the first two literally cut-and-pasted from above) 10 times each, and got...

#7:  152 seconds
lookup table w/ local array: 105 s
lookup table w/ global array: 80 s

So, one could conclude that
a) chip and compiler can make a BIG difference
b) you need a faster machine! :) :)</description>
		<content:encoded><![CDATA[<p>Ii confess my surprise at the lookup table&#8217;s poor performance.  </p>
<p>So I tried it on my machine (Mac w/ 2.16 GHz Intel Cor 2 Duo,  and gcc 4.0.1 (Apple Inc. build 5465, using only default options).  </p>
<p>I also added a modified lookup function where the lookup array was a global.  I repeated the three cases (the first two literally cut-and-pasted from above) 10 times each, and got&#8230;</p>
<p>#7:  152 seconds<br />
lookup table w/ local array: 105 s<br />
lookup table w/ global array: 80 s</p>
<p>So, one could conclude that<br />
a) chip and compiler can make a BIG difference<br />
b) you need a faster machine! <img src='http://www.exploringbinary.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  <img src='http://www.exploringbinary.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Rick Regan</title>
		<link>http://www.exploringbinary.com/ten-ways-to-check-if-an-integer-is-a-power-of-two-in-c/comment-page-1/#comment-101</link>
		<dc:creator>Rick Regan</dc:creator>
		<pubDate>Sat, 31 Jan 2009 16:49:19 +0000</pubDate>
		<guid isPermaLink="false">http://www.exploringbinary.com/?p=207#comment-101</guid>
		<description>Jeff,

I coded your idea, but in the spirit of how I coded #7 (quitting once the bit count exceeds 1):

&lt;pre&gt;
int isPowerOfTwo (unsigned int x)
{
 unsigned int numberOfOneBits = 0;
 unsigned int oneBits[16] = {0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4};

 while(x &amp;&amp; numberOfOneBits &lt;=1)
   {
    numberOfOneBits += oneBits[x &amp; 0xF];
    x &gt;&gt;= 4; /* Examine next 4 bits */
   }

 return (numberOfOneBits == 1); /* &#039;True&#039; if only one 1 bit */
}
&lt;/pre&gt;

This ran in &lt;strike&gt;16 minutes, 38 seconds&lt;/strike&gt; 13 minutes, 1 second, making it slower than #7.

The algorithms with the table lookups seem good in principle, but are among the slowest. Maybe they&#039;d compare better for 64-bit integers?  (Of course humans may no longer inhabit the planet when those runs finish ;) )</description>
		<content:encoded><![CDATA[<p>Jeff,</p>
<p>I coded your idea, but in the spirit of how I coded #7 (quitting once the bit count exceeds 1):</p>
<pre>
int isPowerOfTwo (unsigned int x)
{
 unsigned int numberOfOneBits = 0;
 unsigned int oneBits[16] = {0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4};

 while(x &#038;&#038; numberOfOneBits < =1)
   {
    numberOfOneBits += oneBits[x &#038; 0xF];
    x >>= 4; /* Examine next 4 bits */
   }

 return (numberOfOneBits == 1); /* 'True' if only one 1 bit */
}
</pre>
<p>This ran in <strike>16 minutes, 38 seconds</strike> 13 minutes, 1 second, making it slower than #7.</p>
<p>The algorithms with the table lookups seem good in principle, but are among the slowest. Maybe they&#8217;d compare better for 64-bit integers?  (Of course humans may no longer inhabit the planet when those runs finish <img src='http://www.exploringbinary.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' />  )</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jeff</title>
		<link>http://www.exploringbinary.com/ten-ways-to-check-if-an-integer-is-a-power-of-two-in-c/comment-page-1/#comment-99</link>
		<dc:creator>Jeff</dc:creator>
		<pubDate>Thu, 29 Jan 2009 20:30:52 +0000</pubDate>
		<guid isPermaLink="false">http://www.exploringbinary.com/?p=207#comment-99</guid>
		<description>A variation on counting the ones (#7), though I don&#039;t know how fast it might be, uses a lookup table.  For example, take 4-bits at a time (shifting and masking as necessary) and use the value to index into an array like:

int bitpop[16] = { 0, 1, 1, 2 ... 2, 3, 3, 4 };

Shift and mask by 8 and index into a 256-element array fir more speed (and memory).

Offsetting the slowness of counting ones is you get back more information than just whether or not a value is a power of two.</description>
		<content:encoded><![CDATA[<p>A variation on counting the ones (#7), though I don&#8217;t know how fast it might be, uses a lookup table.  For example, take 4-bits at a time (shifting and masking as necessary) and use the value to index into an array like:</p>
<p>int bitpop[16] = { 0, 1, 1, 2 &#8230; 2, 3, 3, 4 };</p>
<p>Shift and mask by 8 and index into a 256-element array fir more speed (and memory).</p>
<p>Offsetting the slowness of counting ones is you get back more information than just whether or not a value is a power of two.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
