{"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=\u201dbanner\u201d]\n<p><strong>Recommended: Please solve it on \u201cPRACTICE\u201d 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[pastacode lang=\u201dc\u201d manual=\u201d%23include%3Cstdio.h%3E%0A%23define%20bool%20int%0A%20%0A%2F*%20Function%20to%20check%20if%20x%20is%20power%20of%202*%2F%0Abool%20isPowerOfTwo(int%20n)%0A%7B%0A%20%20if%20(n%20%3D%3D%200)%0A%20%20%20%20return%200%3B%0A%20%20while%20(n%20!%3D%201)%0A%20%20%7B%0A%20%20%20%20if%20(n%252%20!%3D%200)%0A%20%20%20%20%20%20return%200%3B%0A%20%20%20%20n%20%3D%20n%2F2%3B%0A%20%20%7D%0A%20%20return%201%3B%0A%7D%0A%20%0A%2F*Driver%20program%20to%20test%20above%20function*%2F%0Aint%20main()%0A%7B%0A%20%20isPowerOfTwo(31)%3F%20printf(%22Yes%5Cn%22)%3A%20printf(%22No%5Cn%22)%3B%0A%20%20isPowerOfTwo(17)%3F%20printf(%22Yes%5Cn%22)%3A%20printf(%22No%5Cn%22)%3B%0A%20%20isPowerOfTwo(16)%3F%20printf(%22Yes%5Cn%22)%3A%20printf(%22No%5Cn%22)%3B%0A%20%20isPowerOfTwo(2)%3F%20printf(%22Yes%5Cn%22)%3A%20printf(%22No%5Cn%22)%3B%0A%20%20isPowerOfTwo(18)%3F%20printf(%22Yes%5Cn%22)%3A%20printf(%22No%5Cn%22)%3B%0A%20%20isPowerOfTwo(1)%3F%20printf(%22Yes%5Cn%22)%3A%20printf(%22No%5Cn%22)%3B%0A%20%20return%200%3B%0A%7D\u201d message=\u201dC Programming\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\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> 011<br \/>\n15 \u2013> 01111<\/p>\n<p>So, if a number n is a power of 2 then bitwise & of n and n-1 will be zero. We can say n is a power of 2 or not based on value of n&(n-1). The expression n&(n-1) will not work when n is 0. To handle this case also, our expression will become n& (!n&(n-1)) (thanks to Mohammad for adding this case).<br \/>\nBelow is the implementation of this method.<\/p>\n[pastacode lang=\u201dc\u201d manual=\u201d%23include%3Cstdio.h%3E%0A%23define%20bool%20int%0A%20%0A%2F*%20Function%20to%20check%20if%20x%20is%20power%20of%202*%2F%0Abool%20isPowerOfTwo%20(int%20x)%0A%7B%0A%20%20%2F*%20First%20x%20in%20the%20below%20expression%20is%20for%20the%20case%20when%20x%20is%200%20*%2F%0A%20%20return%20x%20%26%26%20(!(x%26(x-1)))%3B%0A%7D%0A%20%0A%2F*Driver%20program%20to%20test%20above%20function*%2F%0Aint%20main()%0A%7B%0A%20%20isPowerOfTwo(31)%3F%20printf(%22Yes%5Cn%22)%3A%20printf(%22No%5Cn%22)%3B%0A%20%20isPowerOfTwo(17)%3F%20printf(%22Yes%5Cn%22)%3A%20printf(%22No%5Cn%22)%3B%0A%20%20isPowerOfTwo(16)%3F%20printf(%22Yes%5Cn%22)%3A%20printf(%22No%5Cn%22)%3B%0A%20%20isPowerOfTwo(2)%3F%20printf(%22Yes%5Cn%22)%3A%20printf(%22No%5Cn%22)%3B%0A%20%20isPowerOfTwo(18)%3F%20printf(%22Yes%5Cn%22)%3A%20printf(%22No%5Cn%22)%3B%0A%20%20isPowerOfTwo(1)%3F%20printf(%22Yes%5Cn%22)%3A%20printf(%22No%5Cn%22)%3B%0A%20%20return%200%3B%0A%7D\u201d message=\u201dC Programming\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p><strong>Output:<\/strong><\/p>\n<pre>No\r\nNo\r\nYes\r\nYes\r\nNo\r\nYes<\/pre>\n[ad type=\u201dbanner\u201d]\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}]}}