We strongly recommend that you click here and practice it, before moving on to the solution.

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.

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.

[pastacode lang=”c” manual=”%23include%3Cstdio.h%3E%0A%23define%20bool%20int%0A%20%0A%2F*%20Function%20to%20check%20if%20x%20is%20power%20of%204*%2F%0Abool%20isPowerOfFour(int%20n)%0A%7B%0A%20%20if(n%20%3D%3D%200)%0A%20%20%20%20return%200%3B%0A%20%20while(n%20!%3D%201)%0A%20%20%7B%20%20%20%20%0A%20%20%20if(n%254%20!%3D%200)%0A%20%20%20%20%20%20return%200%3B%0A%20%20%20%20n%20%3D%20n%2F4%3B%20%20%20%20%20%20%0A%20%20%7D%0A%20%20return%201%3B%0A%7D%20%0A%20%0A%2F*Driver%20program%20to%20test%20above%20function*%2F%0Aint%20main()%0A%7B%0A%20%20int%20test_no%20%3D%2064%3B%0A%20%20if(isPowerOfFour(test_no))%0A%20%20%20%20printf(%22%25d%20is%20a%20power%20of%204%22%2C%20test_no)%3B%0A%20%20else%0A%20%20%20%20printf(%22%25d%20is%20not%20a%20power%20of%204%22%2C%20test_no)%3B%0A%20%20getchar()%3B%0A%7D” message=”c” highlight=”” provider=”manual”/]

3. A number n is a power of 4 if following conditions are met.
a) There is only one bit set in the binary representation of n (or n is a power of 2)
b) The count of zero bits before the (only) set bit is even.

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.

Thanks to Geek4u for suggesting the approach and providing the code.

[pastacode lang=”c” manual=”%23include%3Cstdio.h%3E%0A%23define%20bool%20int%0A%20%0Abool%20isPowerOfFour(unsigned%20int%20n)%0A%7B%0A%20%20int%20count%20%3D%200%3B%0A%20%0A%20%20%2F*Check%20if%20there%20is%20only%20one%20bit%20set%20in%20n*%2F%0A%20%20if%20(%20n%20%26%26%20!(n%26(n-1))%20)%0A%20%20%7B%0A%20%20%20%20%20%2F*%20count%200%20bits%20before%20set%20bit%20*%2F%0A%20%20%20%20%20while(n%20%3E%201)%0A%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20n%20%20%3E%3E%3D%201%3B%0A%20%20%20%20%20%20%20count%20%2B%3D%201%3B%0A%20%20%20%20%20%7D%20%20%20%20%20%20%0A%20%0A%20%20%20%20%2F*If%20count%20is%20even%20then%20return%20true%20else%20false*%2F%0A%20%20%20%20return%20(count%252%20%3D%3D%200)%3F%201%20%3A0%3B%0A%20%20%7D%0A%20%0A%20%20%2F*%20If%20there%20are%20more%20than%201%20bit%20set%0A%20%20%20%20then%20n%20is%20not%20a%20power%20of%204*%2F%0A%20%20return%200%3B%0A%7D%20%20%20%20%0A%20%0A%2F*Driver%20program%20to%20test%20above%20function*%2F%0Aint%20main()%0A%7B%0A%20%20%20int%20test_no%20%3D%2064%3B%0A%20%20%20if(isPowerOfFour(test_no))%0A%20%20%20%20%20printf(%22%25d%20is%20a%20power%20of%204%22%2C%20test_no)%3B%0A%20%20%20else%0A%20%20%20%20%20printf(%22%25d%20is%20not%20a%20power%20of%204%22%2C%20test_no)%3B%0A%20%20%20getchar()%3B%0A%7D” message=”c” highlight=”” provider=”manual”/] [ad type=”banner”]

Categorized in: