#231
Easy Algorithms Power of two
Math Bit Manipulation Recursion
49.9% acceptance
Jan 12, 2026
7988
502
Given an integer n, return true if it is a power of two. Otherwise, return false.
An integer n is a power of two, if there exists an integer x such that n == 2x.
Solution
Rust
Time O(1)
Space O(1)
impl Solution {
pub fn is_power_of_two(n: i32) -> bool {
n > 0 && (n & (n - 1)) == 0
}
}