{"id":25990,"date":"2017-10-26T09:22:27","date_gmt":"2017-10-26T03:52:27","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=25990"},"modified":"2017-10-26T09:22:27","modified_gmt":"2017-10-26T03:52:27","slug":"find-whether-given-number-power-4-not","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/find-whether-given-number-power-4-not\/","title":{"rendered":"Find whether a given number is a power of 4 or not"},"content":{"rendered":"<p><strong>We strongly recommend that you click here and practice it, before moving on to the solution.<\/strong><\/p>\n<p>1. A simple method is to take log of the given number on base 4, and if we get an integer then number is power of 4.<\/p>\n<p>2. Another solution is to keep dividing the number by 4, i.e, do n = n\/4 iteratively. In any iteration, if n%4 becomes non-zero and n is not 1 then n is not a power of 4, otherwise n is a power of 4.<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">c<\/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 4*\/<br\/>bool isPowerOfFour(int n)<br\/>{<br\/>  if(n == 0)<br\/>    return 0;<br\/>  while(n != 1)<br\/>  {    <br\/>   if(n%4 != 0)<br\/>      return 0;<br\/>    n = n\/4;      <br\/>  }<br\/>  return 1;<br\/>} <br\/> <br\/>\/*Driver program to test above function*\/<br\/>int main()<br\/>{<br\/>  int test_no = 64;<br\/>  if(isPowerOfFour(test_no))<br\/>    printf(&quot;%d is a power of 4&quot;, test_no);<br\/>  else<br\/>    printf(&quot;%d is not a power of 4&quot;, test_no);<br\/>  getchar();<br\/>}<\/code><\/pre> <\/div>\n<p>3. A number n is a power of 4 if following conditions are met.<br \/>\na) There is only one bit set in the binary representation of n (or n is a power of 2)<br \/>\nb) The count of zero bits before the (only) set bit is even.<\/p>\n<p>For example: 16 (10000) is power of 4 because there is only one bit set and count of 0s before the set bit is 4 which is even.<\/p>\n<p>Thanks to Geek4u for suggesting the approach and providing the code.<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">c<\/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\/>bool isPowerOfFour(unsigned int n)<br\/>{<br\/>  int count = 0;<br\/> <br\/>  \/*Check if there is only one bit set in n*\/<br\/>  if ( n &amp;&amp; !(n&amp;(n-1)) )<br\/>  {<br\/>     \/* count 0 bits before set bit *\/<br\/>     while(n &gt; 1)<br\/>     {<br\/>       n  &gt;&gt;= 1;<br\/>       count += 1;<br\/>     }      <br\/> <br\/>    \/*If count is even then return true else false*\/<br\/>    return (count%2 == 0)? 1 :0;<br\/>  }<br\/> <br\/>  \/* If there are more than 1 bit set<br\/>    then n is not a power of 4*\/<br\/>  return 0;<br\/>}    <br\/> <br\/>\/*Driver program to test above function*\/<br\/>int main()<br\/>{<br\/>   int test_no = 64;<br\/>   if(isPowerOfFour(test_no))<br\/>     printf(&quot;%d is a power of 4&quot;, test_no);<br\/>   else<br\/>     printf(&quot;%d is not a power of 4&quot;, test_no);<br\/>   getchar();<br\/>}<\/code><\/pre> <\/div>\n[ad type=&#8221;banner&#8221;]\n","protected":false},"excerpt":{"rendered":"<p>Find whether a given number is a power of 4 or not &#8211; Bit Algorithm &#8211; A simple method is to take log of the given number on base 4, and if we get an integer.<\/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],"tags":[76458,76440,76449,76445,76465,76438,76475,76473,76466,76479,76454,76441,76472,76455,76476,76480,76456,76469,76467,76481,76446,76436,76451,76461,76457,76459,76464,76448,74025,76443,76453,76444,76471,76437,76478,76442,76470,76463,76450,76439,76460,75883,76468,76474,76477,76482,76462,76435,76452,76447],"class_list":["post-25990","post","type-post","status-publish","format-standard","hentry","category-algorithm","category-bit-algorithms","tag-0-power","tag-0-power-0","tag-10-2-math","tag-10-power-0","tag-16-power-of-2","tag-2-power","tag-2-power-1-2","tag-2-power-14","tag-2-power-2","tag-2-power-31","tag-2-power-4","tag-2-power-6","tag-2-power-64","tag-2-power-7","tag-2-power-8-value","tag-2-power-of-2","tag-2-power-of-4","tag-2-to-power","tag-2-to-power-10","tag-2-to-the-negative-3rd-power","tag-2-to-the-power-of","tag-2-to-the-power-of-6","tag-3-power-6","tag-3-to-the-0-power","tag-4-power-2","tag-8-to-the-3rd-power","tag-all-powers-of-2","tag-definition-of-power-in-math","tag-how-to-calculate-the-power-of-a-number","tag-math-power","tag-power-0","tag-power-2","tag-power-calculator-math","tag-power-math","tag-power-math-example","tag-power-numbers","tag-power-of-2-in-c-programming","tag-power-of-2s","tag-power-of-one","tag-power-raised-to-a-power","tag-power-to-2","tag-powers-of-2","tag-to-the-0-power","tag-to-the-2-power","tag-two-power","tag-what-is-2-to-the-power-of-6","tag-what-is-5-to-the-3rd-power","tag-x-power-2","tag-x-power-3","tag-x-to-the-power-of-4"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/25990","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=25990"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/25990\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=25990"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=25990"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=25990"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}