Four clear steps — then pick the bitwise trick or a divide-by-2 loop.
Require n > 0.
Prefer the interview classic: (n & (n - 1)) == 0.
Or keep dividing by 2 while n is even; end at 1 if it is a power of two.
Reject zero and negatives before either test.
Method
Idea
Time
Best for
Bitwise
n > 0 && (n & (n - 1)) == 0
O(1)
Interviews, production
Divide by 2
Halve until odd / reach 1
O(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
Reference
Cheat Sheet
Keep these facts handy while writing the program.
Topic
Detail
One-liner
n > 0 && (n & (n - 1)) == 0
Why it works
Powers of two have a single set bit
1
2^0 — yes
0
Not a power of two
Negatives
Not in this tutorial
Classic checks
8 yes; 6 no; 16 yes
Try it
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
Trace
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
Code
C# 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.
C#
using System;
class Program
{
static bool IsPowerOfTwo(int n)
{
return n > 0 && (n & (n - 1)) == 0;
}
static void Main()
{
int number = 8;
Console.WriteLine(number + (IsPowerOfTwo(number) ? " is a power of 2" : " is not a power of 2"));
}
}
Output
8 is a power of 2
How It Works
n > 0 rejects zero and negatives up front.
n - 1 clears the lowest set bit of n and turns on all bits below it.
For a single-bit number like 8 (1000), that AND becomes 0.
If two or more bits are set (like 6), the AND is nonzero.
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.
C#
using System;
class Program
{
static bool IsPowerOfTwo(int n)
{
if (n <= 0)
{
return false;
}
while (n % 2 == 0)
{
n /= 2;
}
return n == 1;
}
static void Main()
{
Console.WriteLine(IsPowerOfTwo(1)); // True
Console.WriteLine(IsPowerOfTwo(8)); // True
Console.WriteLine(IsPowerOfTwo(6)); // False
Console.WriteLine(IsPowerOfTwo(0)); // False
}
}
Output
True
True
False
False
How It Works
Reject n <= 0 immediately.
While n is even, divide by 2 — peel off factors of two.
When the loop stops, n is odd.
If that odd value is 1, every factor was a 2 — so it was a power of two.
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.
Analysis
Time and Space Complexity
Program
Time
Extra space
Bitwise check
O(1)
O(1)
Divide-by-2 loop
O(log n)
O(1)
Prefer bitwise in code and interviews. Use the loop when explaining the idea without bit operations.
Remember
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).