oneby7

Bit Manipulation Bit basics

105. Power of Two

Easy

Given an integer n, return true if n is a power of two, and false otherwise.

A power of two is a number 2k with k a whole number, 0 or more: 1, 2, 4, 8, 16 and so on. 1 counts, because it is 20.

Example 1

Input: n = 64

Output: true

Explanation: 64 = 26.

Example 2

Input: n = 96

Output: false

Explanation: 96 = 25 × 3. It is even, but the factor 3 means it is not a power of two.

Example 3

Input: n = 0

Output: false

Explanation: Every power of two is at least 1, so 0 and the negative numbers are never powers of two.

Check your understanding

Work out the answer for this input before you write any code.

n = 1

Constraints

  • -253 ≤ n ≤ 253

Clarifying questions

What you would ask an interviewer before writing any code.

Hints

Interview follow-ups

Runs in your browser
Code
Runs in your browser for now · some things may not work as expectedLn 1, Col 1

Article

Send feedback

Thank you for helping. We read every report and fix what is wrong.

How do you feel about oneby7?

Sign in to oneby7

Keep your progress, your streak and your code, on any device.

New here? This creates your account too.