Problems / Power of Two
Easy

Power of Two

Easy 0 submissions 10 coins +50 XP
Bit Manipulation Math
Problem Description
## 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 `k` such that `n == 2^k`. ## Examples **Example 1:** ``` Input: n = 1 Output: True Explanation: 2^0 = 1 ``` **Example 2:** ``` Input: n = 16 Output: True Explanation: 2^4 = 16 ``` **Example 3:** ``` Input: n = 3 Output: False ``` ## Hints - A power of two must be positive. - A power of two in binary has exactly one bit set to 1. - The bit trick `n & (n - 1) == 0` checks if at most one bit is set. - Combine with `n > 0` to get your answer.
Constraints
- `-2^31 <= n <= 2^31 - 1`

Need help?

Connect with expert programmers for real-time collaborative coding, video meetings, and whiteboard sessions via CodeConnect.

Video Call Whiteboard Live Coding Screen Share