← Interval & Math

Micro-Drill #252 — Power of two check

Interval & Math Target: 5s

Powers of two have exactly one set bit. n & (n-1) clears the lowest set bit — if the result is zero, only one bit was set.

def is_power_of_two(n):
    return n > 0 and (n & (n - 1)) == 0

Type it from memory. Go.

Practice Problems

Related Coding Drills

← Micro #251 Micro #253 →