Power of Two
easyCheck if a number is a power of two
Power of Two
Key Insight
Power of 2 has exactly one 1-bit. n & (n-1) == 0 for powers of 2.
Step 1Powers of 2
8
8
=0
0
0
0
1
0
0
0
76543210
8 = 1000
1, 2, 4, 8... all have exactly one 1-bit.
1 / 5