<?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, 30 Jan 2012 01:37:52 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
	<item>
		<title>By: Lior</title>
		<link>http://www.exploringbinary.com/ten-ways-to-check-if-an-integer-is-a-power-of-two-in-c/comment-page-1/#comment-5616</link>
		<dc:creator>Lior</dc:creator>
		<pubDate>Wed, 25 Jan 2012 19:58:42 +0000</pubDate>
		<guid isPermaLink="false">http://www.exploringbinary.com/?p=207#comment-5616</guid>
		<description>Another way I was today inform of (assuming word length is reasonable, n): create a Boolean array, sized 2^n. For each element, store true if the index is a power of two and false otherwise: b[0]=false, b[1]=true, b[2]=true, b[3]=false, b[4]=true, b[5]=false,...

Time complexity is now O(1).</description>
		<content:encoded><![CDATA[<p>Another way I was today inform of (assuming word length is reasonable, n): create a Boolean array, sized 2^n. For each element, store true if the index is a power of two and false otherwise: b[0]=false, b[1]=true, b[2]=true, b[3]=false, b[4]=true, b[5]=false,&#8230;</p>
<p>Time complexity is now O(1).</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Powers of two &#171; Computing: the Science of Nearly Everything</title>
		<link>http://www.exploringbinary.com/ten-ways-to-check-if-an-integer-is-a-power-of-two-in-c/comment-page-1/#comment-5499</link>
		<dc:creator>Powers of two &#171; Computing: the Science of Nearly Everything</dc:creator>
		<pubDate>Mon, 21 Nov 2011 16:55:47 +0000</pubDate>
		<guid isPermaLink="false">http://www.exploringbinary.com/?p=207#comment-5499</guid>
		<description>[...] big thanks to Rick Regan and his in-depth Ten Ways to Check if an Integer Is a Power Of Two in C blog post)    LD_AddCustomAttr(&quot;AdOpt&quot;, &quot;1&quot;); LD_AddCustomAttr(&quot;Origin&quot;, &quot;other&quot;); [...]</description>
		<content:encoded><![CDATA[<p>[...] big thanks to Rick Regan and his in-depth Ten Ways to Check if an Integer Is a Power Of Two in C blog post)    LD_AddCustomAttr(&quot;AdOpt&quot;, &quot;1&quot;); LD_AddCustomAttr(&quot;Origin&quot;, &quot;other&quot;); [...]</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-5352</link>
		<dc:creator>Rick Regan</dc:creator>
		<pubDate>Wed, 31 Aug 2011 00:37:39 +0000</pubDate>
		<guid isPermaLink="false">http://www.exploringbinary.com/?p=207#comment-5352</guid>
		<description>@AAL,

Your code is the &lt;em&gt;negation&lt;/em&gt; of &#8220;Decrement and Compare.&#8221; You just need to negate it to get the right answer: if !nHasOnlyOne1(x) is true, the x is a power of two.</description>
		<content:encoded><![CDATA[<p>@AAL,</p>
<p>Your code is the <em>negation</em> of &ldquo;Decrement and Compare.&rdquo; You just need to negate it to get the right answer: if !nHasOnlyOne1(x) is true, the x is a power of two.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Krzysztof Wesołowski</title>
		<link>http://www.exploringbinary.com/ten-ways-to-check-if-an-integer-is-a-power-of-two-in-c/comment-page-1/#comment-5351</link>
		<dc:creator>Krzysztof Wesołowski</dc:creator>
		<pubDate>Tue, 30 Aug 2011 23:00:21 +0000</pubDate>
		<guid isPermaLink="false">http://www.exploringbinary.com/?p=207#comment-5351</guid>
		<description>Also note that some approaches can be implemented in C as macro to be calculated with constant argument at compile time (or for example with template argument with C++ to do static_asserts etc.).</description>
		<content:encoded><![CDATA[<p>Also note that some approaches can be implemented in C as macro to be calculated with constant argument at compile time (or for example with template argument with C++ to do static_asserts etc.).</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Harold Rabbie</title>
		<link>http://www.exploringbinary.com/ten-ways-to-check-if-an-integer-is-a-power-of-two-in-c/comment-page-1/#comment-5350</link>
		<dc:creator>Harold Rabbie</dc:creator>
		<pubDate>Tue, 30 Aug 2011 20:56:10 +0000</pubDate>
		<guid isPermaLink="false">http://www.exploringbinary.com/?p=207#comment-5350</guid>
		<description>If you&#039;re using gcc, that compiler includes a builtin function 
int __builtin_popcount (unsigned int x) which returns the number of 1-bits in x.

So (__builtin_popcount (x) == 1) is true iff x is a power of 2.
Some processor architectures even include a population count instruction.</description>
		<content:encoded><![CDATA[<p>If you&#8217;re using gcc, that compiler includes a builtin function<br />
int __builtin_popcount (unsigned int x) which returns the number of 1-bits in x.</p>
<p>So (__builtin_popcount (x) == 1) is true iff x is a power of 2.<br />
Some processor architectures even include a population count instruction.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: AAL</title>
		<link>http://www.exploringbinary.com/ten-ways-to-check-if-an-integer-is-a-power-of-two-in-c/comment-page-1/#comment-5349</link>
		<dc:creator>AAL</dc:creator>
		<pubDate>Tue, 30 Aug 2011 20:54:18 +0000</pubDate>
		<guid isPermaLink="false">http://www.exploringbinary.com/?p=207#comment-5349</guid>
		<description>You can use this macro and a bit of other code to quickly determine if only one bit is set, then check if it is BIT0the odd case the number 1:


/* Returns ZERO if paramter has a single bit set high */
#define nHasOnlyOne1( p )    ( ((p)==0) &#124;&#124; ( (p) &amp; ((p)-1) ) )</description>
		<content:encoded><![CDATA[<p>You can use this macro and a bit of other code to quickly determine if only one bit is set, then check if it is BIT0the odd case the number 1:</p>
<p>/* Returns ZERO if paramter has a single bit set high */<br />
#define nHasOnlyOne1( p )    ( ((p)==0) || ( (p) &amp; ((p)-1) ) )</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Pseudonym</title>
		<link>http://www.exploringbinary.com/ten-ways-to-check-if-an-integer-is-a-power-of-two-in-c/comment-page-1/#comment-5281</link>
		<dc:creator>Pseudonym</dc:creator>
		<pubDate>Sun, 31 Jul 2011 02:53:31 +0000</pubDate>
		<guid isPermaLink="false">http://www.exploringbinary.com/?p=207#comment-5281</guid>
		<description>If you&#039;re on a POSIX.1 compliant platform,  should contain the ffs() function. If you&#039;re lucky, your system will use your CPU&#039;s built-in priority encoder instruction to implement this, and you can do the following. Hopefully no loops, and at most one branch or conditional move:


int isPowerOfTwo(int x)
{
    int bit = ffs(x);
    return bit ? ((1 &lt;&lt; (bit - 1)) == x) : 0;
}</description>
		<content:encoded><![CDATA[<p>If you&#8217;re on a POSIX.1 compliant platform,  should contain the ffs() function. If you&#8217;re lucky, your system will use your CPU&#8217;s built-in priority encoder instruction to implement this, and you can do the following. Hopefully no loops, and at most one branch or conditional move:</p>
<p>int isPowerOfTwo(int x)<br />
{<br />
    int bit = ffs(x);<br />
    return bit ? ((1 &lt;&lt; (bit &#8211; 1)) == x) : 0;<br />
}</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-5280</link>
		<dc:creator>Rick Regan</dc:creator>
		<pubDate>Sat, 30 Jul 2011 15:25:37 +0000</pubDate>
		<guid isPermaLink="false">http://www.exploringbinary.com/?p=207#comment-5280</guid>
		<description>@Ivan,

I agree. Your code is a cleaner version of my &quot;Count Ones&quot; (#7) -- and a lot faster.</description>
		<content:encoded><![CDATA[<p>@Ivan,</p>
<p>I agree. Your code is a cleaner version of my &#8220;Count Ones&#8221; (#7) &#8212; and a lot faster.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Stefanus Du Toit</title>
		<link>http://www.exploringbinary.com/ten-ways-to-check-if-an-integer-is-a-power-of-two-in-c/comment-page-1/#comment-5279</link>
		<dc:creator>Stefanus Du Toit</dc:creator>
		<pubDate>Sat, 30 Jul 2011 05:15:25 +0000</pubDate>
		<guid isPermaLink="false">http://www.exploringbinary.com/?p=207#comment-5279</guid>
		<description>(and yes that is essentially no 9 - though I often don&#039;t care about the zero case)</description>
		<content:encoded><![CDATA[<p>(and yes that is essentially no 9 &#8211; though I often don&#8217;t care about the zero case)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Stefanus Du Toit</title>
		<link>http://www.exploringbinary.com/ten-ways-to-check-if-an-integer-is-a-power-of-two-in-c/comment-page-1/#comment-5278</link>
		<dc:creator>Stefanus Du Toit</dc:creator>
		<pubDate>Sat, 30 Jul 2011 02:52:48 +0000</pubDate>
		<guid isPermaLink="false">http://www.exploringbinary.com/?p=207#comment-5278</guid>
		<description>(x &amp; (x - 1)) == 0

If I remember right I first read this in Hacker&#039;s Delight or HAKMEM.</description>
		<content:encoded><![CDATA[<p>(x &amp; (x &#8211; 1)) == 0</p>
<p>If I remember right I first read this in Hacker&#8217;s Delight or HAKMEM.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ivan Tikhonov</title>
		<link>http://www.exploringbinary.com/ten-ways-to-check-if-an-integer-is-a-power-of-two-in-c/comment-page-1/#comment-5277</link>
		<dc:creator>Ivan Tikhonov</dc:creator>
		<pubDate>Fri, 29 Jul 2011 13:57:08 +0000</pubDate>
		<guid isPermaLink="false">http://www.exploringbinary.com/?p=207#comment-5277</guid>
		<description>Count Ones is actually better to be renamed and rewritten as &quot;Check the only one&quot;

So basically you:

1. find that one exists.
2. find that no ones exist beyond first.

This way you remove increment and condition inside of loop.


&lt;code&gt;
int isPowerOfTwo (unsigned int x)
{
 while(x &amp;&amp; (x&amp;1)==0) x&gt;&gt;=1; // shift till 1 found
 if(!x) return 0; // no ones found at all
 x&gt;&gt;=1;
 return x==0; // check there is no ones past first one
}
&lt;/code&gt;

Also if you unroll while loop there will be no need for &quot;x not zero&quot; checking at all.</description>
		<content:encoded><![CDATA[<p>Count Ones is actually better to be renamed and rewritten as &#8220;Check the only one&#8221;</p>
<p>So basically you:</p>
<p>1. find that one exists.<br />
2. find that no ones exist beyond first.</p>
<p>This way you remove increment and condition inside of loop.</p>
<p><code><br />
int isPowerOfTwo (unsigned int x)<br />
{<br />
 while(x &amp;&amp; (x&amp;1)==0) x&gt;&gt;=1; // shift till 1 found<br />
 if(!x) return 0; // no ones found at all<br />
 x&gt;&gt;=1;<br />
 return x==0; // check there is no ones past first one<br />
}<br />
</code></p>
<p>Also if you unroll while loop there will be no need for &#8220;x not zero&#8221; checking at all.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Friday miscellany &#8212; The Endeavour</title>
		<link>http://www.exploringbinary.com/ten-ways-to-check-if-an-integer-is-a-power-of-two-in-c/comment-page-1/#comment-5276</link>
		<dc:creator>Friday miscellany &#8212; The Endeavour</dc:creator>
		<pubDate>Fri, 29 Jul 2011 09:55:39 +0000</pubDate>
		<guid isPermaLink="false">http://www.exploringbinary.com/?p=207#comment-5276</guid>
		<description>[...] language fixes something 10 ways to check whether an integer is a power of 2 Version control by [...]</description>
		<content:encoded><![CDATA[<p>[...] language fixes something 10 ways to check whether an integer is a power of 2 Version control by [...]</p>
]]></content:encoded>
	</item>
</channel>
</rss>

