~/boss / one-bit

Bit trick: n & (n − 1)

The Bit Check

Power of two or not? Subtract one, AND, and call it before the clock runs out.

Loading the fight…

How to beat it

A power of two has exactly one bit set. Subtract one and that bit turns off while every bit below it turns on, so n and n − 1 share no bits and n & (n − 1) is 0. Any other number has a higher 1 that survives the AND. And 0 never counts: check n > 0.

Now the real boss

Interviews ask for the code. Watch it run, then write it yourself, tested in Python, C++ or Java.

On your phone? Writing code is easier on a laptop: open bossfight.dev/problems/power-of-two there. Sign in with your email to keep your progress on every device.

More bosses

esc