def isPowerOf4(x): mask = 0x55555555 return isPowerOf2(x) and not not (x & mask) def isPowerOf2(x): return not x & (x - 1)
not not ... pretty ugly and prefer
bool(...). Much clearer in my opinion.
Or in this case maybe
... and x & mask != 0.
This is clever!
For those who don't understand the syntax,
x & (x - 1) == 0 != x & 0x55555555 means
x & (x-1) == 0 and
0 != 0x55555555
I use your 3BLD method :)
Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.