Skip to main content
Back to problems
Leetcode
Easy
Bit Manipulation
Math
Power Of Two

Determine whether an integer is an exact power of two.

Acceptance 0%
Also Available On
Other platform versions and source mappings for the same problem.
Problem Statement

Given an integer n, return whether it can be written in the form 2^k for some integer k >= 0.

A number is a power of two if it is positive and has exactly one set bit in its binary representation.

Input Format

  • A single integer n.
  • Decide whether n is a power of two.

Output Format

  • Return true if n is an exact power of two.
  • Otherwise return false.

Constraints

  • You may assume n fits in a standard 32-bit signed integer.
  • n may be non-positive.
  • Aim for O(1)O(1) or O(logn)O(\log n) time.
Examples
Sample cases returned by the problem API.

Example 1

Input

n = 1

Output

true

Explanation

1=201 = 2^0.

Example 2

Input

n = 16

Output

true

Explanation

16=2416 = 2^4.

Show 1 more example

Example 3

Input

n = 3

Output

false

Explanation

3 is not an exact power of two.

Premium problem context

Unlock deeper context for this problem

Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.