← Back to Games

✨ Power Rune Test

Test if a number is a power of 2 - only one bit should be set!

🔮 The Rune Stone
n & (n - 1) == 0 && n > 0 → Power of 2
?
Enter a number to test
Binary Representation
Try:
2⁰=1
2¹=2
2²=4
2³=8
2⁴=16
2⁵=32
2⁶=64
2⁷=128

📚 Power of Two Detection

A power of 2 has exactly one bit set in its binary representation:

1 = 00000001 (2⁰)
2 = 00000010 (2¹)
4 = 00000100 (2²)
8 = 00001000 (2³)
...

// n & (n-1) removes the lowest set bit
// If result is 0, there was only ONE bit!
isPowerOf2 = (n > 0) && ((n & (n - 1)) == 0)