Find whether a given number is a power of 4 or not | Function to check if x is power of 4 ; Check if there is only one bit set in n ; count 0 bits before set bit ; If count is even then return true else false ; If there are more than 1 bit set then n is not a power of 4 ; Driver program to test above function
< ? php function isPowerOfFour ( $ n ) { $ count = 0 ; if ( $ n && ! ( $ n & ( $ n - 1 ) ) ) { while ( $ n > 1 ) { $ n >>= 1 ; $ count += 1 ; } return ( $ count % 2 == 0 ) ? 1 : 0 ; } return 0 ; } $ test_no = 64 ; if ( isPowerOfFour ( $ test_no ) ) echo $ test_no , " ▁ is ▁ a ▁ power ▁ of ▁ 4" ; else echo $ test_no , " ▁ not ▁ is ▁ a ▁ power ▁ of ▁ 4" ; ? >