Skip to main content
LeetCode 231, Easy. Topics: Math, Bit Manipulation, Recursion. View on LeetCode. Generate this problem as a practice environment: tested reference solution, 18 parametrized pytest cases, and a playground notebook:

Problem

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 == 2^x.

Examples

Constraints

  • -2^31 <= n <= 2^31 - 1
Follow up: Could you solve it without loops/recursion?

Solution

Reference implementation from solution.py on GitHub, full suite in test_solution.py:

Complexity

Tags

NeetCode All.
Last modified on September 7, 2026