Java Power of 2 Program

Beginner
5 min read
Updated: Oct 2026
2 programs
Live preview

What Is a Power of 2?

A positive integer is a power of 2 when it equals 2k for some integer k ≥ 0. In binary, that means exactly one bit is set.

Remember
Powers of 2: 1, 2, 4, 8, 16, 32, 64, ...

Binary (one bit set):
   1 → 1
   2 → 10
   4 → 100
   8 → 1000
  16 → 10000

Not: 0, 3, 6, 12, 18, negatives
nBinaryPower of 2?
81000Yes (23)
11Yes (20)
6110No
00No

How to Check a Power of 2 in Java

Four clear steps — then pick the bitwise trick or a divide-by-2 loop.

  1. Require n > 0.
  2. Prefer the interview classic: (n & (n - 1)) == 0.
  3. Or keep dividing by 2 while n is even; end at 1 if it is a power of two.
  4. Reject zero and negatives before either test.
MethodIdeaTimeBest for
Bitwisen > 0 && (n & (n - 1)) == 0O(1)Interviews, production
Divide by 2Halve until odd / reach 1O(log n)Explaining without bits

Pseudocode

Pseudocode
function IsPowerOfTwo(n):
    return n > 0 and (n & (n - 1)) == 0

// Loop alternative:
function IsPowerOfTwoLoop(n):
    if n <= 0: return false
    while n % 2 == 0:
        n = n / 2
    return n == 1

Cheat Sheet

Keep these facts handy while writing the program.

TopicDetail
One-linern > 0 && (n & (n - 1)) == 0
Why it worksPowers of two have a single set bit
12^0 — yes
0Not a power of two
NegativesNot in this tutorial
Classic checks8 yes; 6 no; 16 yes

Live Preview

Enter an integer to see the bitwise check and the power-of-two verdict.

Whole numbers only. Tap a chip or type a value.

Result
n = 8
n - 1 = 7
n & (n - 1) = 0
8 is a power of 2

Worked Walkthrough — n = 8

Show the bitwise identity, then the same answer by dividing.

Calculation
Bitwise:
  8 in binary = 1000
  7 in binary = 0111
  1000 & 0111 = 0000  → power of 2

Divide-by-2 loop:
  8 / 2 = 4
  4 / 2 = 2
  2 / 2 = 1
  stop at 1 → power of 2

Counterexample n = 6:
  6 = 110, 5 = 101
  110 & 101 = 100 ≠ 0 → not a power of 2

Java Programs

Start with the bitwise one-liner — that is the usual interview answer. Keep the divide loop ready when you need to explain without bits.

Example 1 — Bitwise Check

A positive power of two has exactly one set bit, so n & (n - 1) is zero.

Java
class PowerOfTwo
{
    static boolean isPowerOfTwo(int n)
    {
        return n > 0 && (n & (n - 1)) == 0;
    }

    public static void main(String[] args)
    {
        int number = 8;
        if (isPowerOfTwo(number))
        {
            System.out.println(number + " is a power of 2");
        }
        else
        {
            System.out.println(number + " is not a power of 2");
        }
    }
}

How It Works

  1. n > 0 rejects zero and negatives up front.
  2. n - 1 clears the lowest set bit of n and turns on all bits below it.
  3. For a single-bit number like 8 (1000), that AND becomes 0.
  4. If two or more bits are set (like 6), the AND is nonzero.
  5. Constant-time check — the default answer in interviews.

Example 2 — Divide-by-2 Loop

Keep dividing even values by 2; a power of two ends exactly at 1.

Java
class PowerOfTwoLoop
{
    static boolean isPowerOfTwo(int n)
    {
        if (n <= 0)
        {
            return false;
        }

        while (n % 2 == 0)
        {
            n /= 2;
        }

        return n == 1;
    }

    public static void main(String[] args)
    {
        System.out.println(isPowerOfTwo(1));  // true
        System.out.println(isPowerOfTwo(8));  // true
        System.out.println(isPowerOfTwo(6));  // false
        System.out.println(isPowerOfTwo(0));  // false
    }
}

How It Works

  1. Reject n <= 0 immediately.
  2. While n is even, divide by 2 — peel off factors of two.
  3. When the loop stops, n is odd.
  4. If that odd value is 1, every factor was a 2 — so it was a power of two.
  5. If it is any other odd number (3, 5, …), extra prime factors remain.

Edge Cases & Pitfalls

Check these before calling the solution done.

1

20 counts

1 is a power of two. Do not require n > 1.

0

Not a power of 2

0 & (-1) tricks people — always guard with n > 0.

Negatives

Reject early

Two’s-complement bits are not “powers of two” for this problem.

Even ≠ 2k

6, 12, 18…

Even numbers can still have other factors. Use the full test.

Time and Space Complexity

ProgramTimeExtra space
Bitwise checkO(1)O(1)
Divide-by-2 loopO(log n)O(1)

Prefer bitwise in code and interviews. Use the loop when explaining the idea without bit operations.

Key Takeaways

  • Definition: n = 2k for some k ≥ 0 (one set bit).
  • Trick: n > 0 && (n & (n - 1)) == 0.
  • Loop: divide by 2 until odd; success if you land on 1.
  • Test set: 1 → yes; 8 → yes; 6 → no; 0 → no.

One line: positive integers with a single set bit — check with n & (n - 1).

Frequently Asked Questions

A positive integer of the form 2^k for some integer k ≥ 0. Examples: 1, 2, 4, 8, 16, 32.
A power of two has exactly one bit set. Subtracting 1 turns that bit off and sets all lower bits, so the bitwise AND with n is zero.
Yes. 1 = 2^0.
0 and negatives are not powers of two in this tutorial. Always require n > 0 before the bitwise test.
The bitwise check is O(1). Repeatedly dividing by 2 is O(log n).

Next: Power of 3

Check whether a number is a power of three.

Power of 3 tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful