Valid Number:
1
100
10000
1000000
100000000
10000000000
1000000000000
100000000000000
10000000000000000
1000000000000000000
100000000000000000000
10000000000000000000000
1000000000000000000000000
100000000000000000000000000
10000000000000000000000000000
1000000000000000000000000000000
Any other number not it the list should be considered as invalid. So if you XOR them altogether, you will get a mask value, which is:
1010101010101010101010101010101 (1431655765)
num &(num-1) == 0 is making sure that 2 - based is not included in ans
O(1)
class Solution:
def isPowerOfFour(self, num: int) -> bool:
return num != 0 and num &(num-1) == 0 and num & 1431655765 == num