34 · Bit Manipulation

#231 Power of Two

Easysolve on LeetCode ↗

frequently reported atGoogleAmazon

the problem

In plain words

Return true if n is a power of two, that is, n == 2ˣ for some integer x ≥ 0. Can you do it without loops or recursion?

in n = 1

out true

2⁰ = 1.

in n = 16

out true

in n = 3

out false

  • −2³¹ ≤ n ≤ 2³¹ − 1

your turn

Try it before you peek

Read the examples, say the brute force out loud, then write code: here or on LeetCode ↗. Stuck for 20 minutes? Open one hint at a time.

Solve it in the browser

LeetCode-style: complete the class. Compiled by the real javac (Java 17) and judged right here — nothing is sent anywhere.

    Solutions are hidden so you can try first. Even 20 minutes of real effort makes the answer stick far better than reading it.

    Marking the problem solved, or opening every hint, also shows it.

    practise next

    Similar problems

    1. #191
      Number of 1 Bits
      Easyin Bits
    2. #338
      Counting Bits
      Easyin Bits
    3. #50
      Pow(x, n)
      Mediumin Recursion