{"id":26404,"date":"2017-10-26T22:01:17","date_gmt":"2017-10-26T16:31:17","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=26404"},"modified":"2017-10-26T22:01:17","modified_gmt":"2017-10-26T16:31:17","slug":"c-program-find-whether-no-power-two","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/c-program-find-whether-no-power-two\/","title":{"rendered":"C Program to find whether a no is power of two"},"content":{"rendered":"<p>Given a positive integer, write a function to find if it is a power of two or not.<\/p>\n<p><strong>Examples:<\/strong><\/p>\n<pre>Input : n = 4\r\nOutput : Yes\r\n2<sup>2<\/sup> = 4\r\n\r\nInput : n = 7\r\nOutput : No\r\n\r\nInput : n = 32\r\nOutput : Yes\r\n2<sup>5<\/sup> = 32<\/pre>\n[ad type=&#8221;banner&#8221;]\n<p><strong>Recommended: Please solve it on &#8220;PRACTICE&#8221; first, before moving on to the solution.<\/strong><\/p>\n<p><strong>1.<\/strong> A simple method for this is to simply take the log of the number on base 2 and if you get an integer then number is power of 2.<\/p>\n<p><strong>2.<\/strong> Another solution is to keep dividing the number by two, i.e, do n = n\/2 iteratively. In any iteration, if n%2 becomes non-zero and n is not 1 then n is not a power of 2. If n becomes 1 then it is a power of 2.<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">C Programming<\/span> <\/div> <pre class=\"language-c code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-c code-embed-code\">#include&lt;stdio.h&gt;<br\/>#define bool int<br\/> <br\/>\/* Function to check if x is power of 2*\/<br\/>bool isPowerOfTwo(int n)<br\/>{<br\/>  if (n == 0)<br\/>    return 0;<br\/>  while (n != 1)<br\/>  {<br\/>    if (n%2 != 0)<br\/>      return 0;<br\/>    n = n\/2;<br\/>  }<br\/>  return 1;<br\/>}<br\/> <br\/>\/*Driver program to test above function*\/<br\/>int main()<br\/>{<br\/>  isPowerOfTwo(31)? printf(&quot;Yes\\n&quot;): printf(&quot;No\\n&quot;);<br\/>  isPowerOfTwo(17)? printf(&quot;Yes\\n&quot;): printf(&quot;No\\n&quot;);<br\/>  isPowerOfTwo(16)? printf(&quot;Yes\\n&quot;): printf(&quot;No\\n&quot;);<br\/>  isPowerOfTwo(2)? printf(&quot;Yes\\n&quot;): printf(&quot;No\\n&quot;);<br\/>  isPowerOfTwo(18)? printf(&quot;Yes\\n&quot;): printf(&quot;No\\n&quot;);<br\/>  isPowerOfTwo(1)? printf(&quot;Yes\\n&quot;): printf(&quot;No\\n&quot;);<br\/>  return 0;<br\/>}<\/code><\/pre> <\/div>\n<p><strong>Output:<\/strong><\/p>\n<pre>No\r\nNo\r\nYes\r\nYes\r\nNo\r\nYes<\/pre>\n<p><strong>3.<\/strong> All power of two numbers have only one bit set. So count the no. of set bits and if you get 1 then number is a power of 2. Please see http:\/\/www.geeksforgeeks.org\/?p=1176 for counting set bits.<\/p>\n<p><strong>4.<\/strong> If we subtract a power of 2 numbers by 1 then all unset bits after the only set bit become set; and the set bit become unset.<\/p>\n<p>For example for 4 ( 100) and 16(10000), we get following after subtracting 1<br \/>\n3 \u2013&gt; 011<br \/>\n15 \u2013&gt; 01111<\/p>\n<p>So, if a number n is a power of 2 then bitwise &amp; of n and n-1 will be zero. We can say n is a power of 2 or not based on value of n&amp;(n-1). The expression n&amp;(n-1) will not work when n is 0. To handle this case also, our expression will become n&amp; (!n&amp;(n-1)) (thanks to Mohammad for adding this case).<br \/>\nBelow is the implementation of this method.<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">C Programming<\/span> <\/div> <pre class=\"language-c code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-c code-embed-code\">#include&lt;stdio.h&gt;<br\/>#define bool int<br\/> <br\/>\/* Function to check if x is power of 2*\/<br\/>bool isPowerOfTwo (int x)<br\/>{<br\/>  \/* First x in the below expression is for the case when x is 0 *\/<br\/>  return x &amp;&amp; (!(x&amp;(x-1)));<br\/>}<br\/> <br\/>\/*Driver program to test above function*\/<br\/>int main()<br\/>{<br\/>  isPowerOfTwo(31)? printf(&quot;Yes\\n&quot;): printf(&quot;No\\n&quot;);<br\/>  isPowerOfTwo(17)? printf(&quot;Yes\\n&quot;): printf(&quot;No\\n&quot;);<br\/>  isPowerOfTwo(16)? printf(&quot;Yes\\n&quot;): printf(&quot;No\\n&quot;);<br\/>  isPowerOfTwo(2)? printf(&quot;Yes\\n&quot;): printf(&quot;No\\n&quot;);<br\/>  isPowerOfTwo(18)? printf(&quot;Yes\\n&quot;): printf(&quot;No\\n&quot;);<br\/>  isPowerOfTwo(1)? printf(&quot;Yes\\n&quot;): printf(&quot;No\\n&quot;);<br\/>  return 0;<br\/>}<\/code><\/pre> <\/div>\n<p><strong>Output:<\/strong><\/p>\n<pre>No\r\nNo\r\nYes\r\nYes\r\nNo\r\nYes<\/pre>\n[ad type=&#8221;banner&#8221;]\n","protected":false},"excerpt":{"rendered":"<p>C Program to find whether a no is power of two &#8211; Bit Algorithm &#8211; Given a positive integer, write a function to find if it is a power of two or not.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[69969,74852,82927,1],"tags":[74843,74845,74848,76423,76420,74851,78698,75083,75079,75263,76432,69926,70851,69955,73778,78701,70047,76434,74830,70832,70824,76424,70809,69948,69959,74842,74835,74832,74025,74012,74090,75439,76087,76281,76444,74039,76437,74023,75883,78700,70810,70836,78697,69956,69952,78699,78360,78357,74838],"class_list":["post-26404","post","type-post","status-publish","format-standard","hentry","category-algorithm","category-bit-algorithms","category-c-programming-2","category-coding","tag-arithmetic-operators-in-c","tag-basic-c-program-examples","tag-basic-c-programming-tutorial","tag-basic-c-programs","tag-basic-c-programs-for-beginners","tag-basic-programs-in-c","tag-basic-structure-of-c-program","tag-bitwise-operations-in-c","tag-bitwise-operator-in-c","tag-bitwise-operators-in-c","tag-c-basic-programs","tag-c-language-basics-notes","tag-c-language-examples","tag-c-language-operator","tag-c-program-to-find-power-of-a-number-using-function","tag-c-programing-language","tag-c-programming","tag-c-programming-basics","tag-c-programming-basics-notes","tag-c-programming-codes","tag-c-programming-examples-with-output","tag-c-programming-language-examples","tag-c-programming-tutorial","tag-data-types-in-c-language","tag-define-c-language","tag-function-in-c-language","tag-function-in-c-programming-examples","tag-functions-in-c-programming-with-examples","tag-how-to-calculate-the-power-of-a-number","tag-how-to-use-power-function-in-c","tag-how-to-write-a-program-in-c-language","tag-modulus-operator","tag-modulus-operator-in-c","tag-operators-in-c-programming","tag-power-2","tag-power-function-in-c","tag-power-math","tag-power-program-in-c","tag-powers-of-2","tag-programming-language-c","tag-simple-c-programs","tag-simple-c-programs-with-output","tag-types-of-operators-in-c-programming","tag-uses-of-c-language","tag-what-is-c-language-definition","tag-what-is-c-program","tag-what-is-c-programming-used-for","tag-what-is-in-c-language","tag-while-loop-in-c-programming-example"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26404","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=26404"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26404\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=26404"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=26404"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=26404"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}