<?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>Fri, 10 May 2013 09:09:45 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5</generator>
	<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-6025</link>
		<dc:creator>Rick Regan</dc:creator>
		<pubDate>Tue, 23 Apr 2013 16:14:10 +0000</pubDate>
		<guid isPermaLink="false">http://www.exploringbinary.com/?p=207#comment-6025</guid>
		<description><![CDATA[@Divanushka,

That works too. If x is a power of two, x - 1 is the inversion of x, that is, a 0 followed by all 1s. Inverting that gives a 1 followed by all 0s, that is, x.]]></description>
		<content:encoded><![CDATA[<p>@Divanushka,</p>
<p>That works too. If x is a power of two, x &#8211; 1 is the inversion of x, that is, a 0 followed by all 1s. Inverting that gives a 1 followed by all 0s, that is, x.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Divanushka</title>
		<link>http://www.exploringbinary.com/ten-ways-to-check-if-an-integer-is-a-power-of-two-in-c/comment-page-1/#comment-6021</link>
		<dc:creator>Divanushka</dc:creator>
		<pubDate>Sun, 21 Apr 2013 17:49:55 +0000</pubDate>
		<guid isPermaLink="false">http://www.exploringbinary.com/?p=207#comment-6021</guid>
		<description><![CDATA[return (x &gt; 0) &amp;&amp; ((x &amp; ~(x - 1)) == x)]]></description>
		<content:encoded><![CDATA[<p>return (x &gt; 0) &amp;&amp; ((x &amp; ~(x &#8211; 1)) == x)</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-5902</link>
		<dc:creator>Rick Regan</dc:creator>
		<pubDate>Thu, 18 Oct 2012 12:09:59 +0000</pubDate>
		<guid isPermaLink="false">http://www.exploringbinary.com/?p=207#comment-5902</guid>
		<description><![CDATA[@AASHISH,

Yes, that&#039;s method 9, &quot;Decrement and Compare&quot; (except you don&#039;t check if num == 0).]]></description>
		<content:encoded><![CDATA[<p>@AASHISH,</p>
<p>Yes, that&#8217;s method 9, &#8220;Decrement and Compare&#8221; (except you don&#8217;t check if num == 0).</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: AASHISH</title>
		<link>http://www.exploringbinary.com/ten-ways-to-check-if-an-integer-is-a-power-of-two-in-c/comment-page-1/#comment-5901</link>
		<dc:creator>AASHISH</dc:creator>
		<pubDate>Thu, 18 Oct 2012 10:38:42 +0000</pubDate>
		<guid isPermaLink="false">http://www.exploringbinary.com/?p=207#comment-5901</guid>
		<description><![CDATA[I give u best solution.
Logic--&gt; when u convert a decimal number to its binary number then u get series of 1 and 0
Now if a number is power of two then u will observe it has only one (bit 1) and if u do binary AND of of it with a number just previous to it ,then u will get  series of Zeros
For example
8=1000(binary form ) here u can see only one (bit 1) is there and rest are zeros
Now the number just previous to 8 is 7
so, 7=0111 and if u perform AND operation on it as
         1000(8)
(AND) 0111(7)
       ---------
         0000(0)
       ---------  
So if we perform AND operation between the number (which we want to check is a power of two or not) and the number just previous to it and result comes as zeros then its power of two otherwise it&#039;s not.
       
                         PROGRAM(IN C_LANGUAGE)

#include
#include
main()
{
int num, num1,sum;
clrscr();
printf(&quot;Enter the number&quot;);
scanf(&quot;%d&quot;,&amp;num);
printf(&quot;\nEntered number=%d&quot;,num);
num1=num-1;
sum=num &amp; num1;
if(sum==0)
  printf(&quot;\n Entered number is power of two&quot;);
else
  printf(&quot;\n Entered number is not power of two&quot;);
getch();

Voila!!!! Isn&#039;t it sooooo easy!!!!
Enjoy!!!]]></description>
		<content:encoded><![CDATA[<p>I give u best solution.<br />
Logic&#8211;&gt; when u convert a decimal number to its binary number then u get series of 1 and 0<br />
Now if a number is power of two then u will observe it has only one (bit 1) and if u do binary AND of of it with a number just previous to it ,then u will get  series of Zeros<br />
For example<br />
8=1000(binary form ) here u can see only one (bit 1) is there and rest are zeros<br />
Now the number just previous to 8 is 7<br />
so, 7=0111 and if u perform AND operation on it as<br />
         1000(8)<br />
(AND) 0111(7)<br />
       &#8212;&#8212;&#8212;<br />
         0000(0)<br />
       &#8212;&#8212;&#8212;<br />
So if we perform AND operation between the number (which we want to check is a power of two or not) and the number just previous to it and result comes as zeros then its power of two otherwise it&#8217;s not.</p>
<p>                         PROGRAM(IN C_LANGUAGE)</p>
<p>#include<br />
#include<br />
main()<br />
{<br />
int num, num1,sum;<br />
clrscr();<br />
printf(&#8220;Enter the number&#8221;);<br />
scanf(&#8220;%d&#8221;,&amp;num);<br />
printf(&#8220;\nEntered number=%d&#8221;,num);<br />
num1=num-1;<br />
sum=num &amp; num1;<br />
if(sum==0)<br />
  printf(&#8220;\n Entered number is power of two&#8221;);<br />
else<br />
  printf(&#8220;\n Entered number is not power of two&#8221;);<br />
getch();</p>
<p>Voila!!!! Isn&#8217;t it sooooo easy!!!!<br />
Enjoy!!!</p>
]]></content:encoded>
	</item>
	<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><![CDATA[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><![CDATA[[...] 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><![CDATA[@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><![CDATA[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><![CDATA[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><![CDATA[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><![CDATA[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><![CDATA[@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>
</channel>
</rss>
