Check Power of 2 in C#
What you’ll learn
- What a power of 2 means:
n = 2^kfor a whole numberk. - The classic bitwise test
n > 0 && (n & (n - 1)) == 0. - How to check
16and list all powers of 2 from1to20.
What is a power of 2?
A positive integer is a power of 2 when it equals 2 multiplied by itself k times. Examples: 1, 2, 4, 8, 16. Values like 3, 6, 10, 12 are not powers of 2.
Binary view
8 is 1000 — one lone 1 bit.
Bit trick
n & (n - 1) clears the lowest set bit.
Range 1–20
Outputs 1 2 4 8 16.
Prerequisites
Binary basics, bitwise &, and positive integers.
- You know that binary digits are bits and a set bit is
1. - You can read a simple
ifandforloop in C#.
Understanding the concept
In binary, powers of two have exactly one bit set. For 16 the pattern is 10000. Subtracting 1 flips trailing zeros and the lowest one, so 16 & 15 == 0.
Intuition examples
16 = 2&sup4; (power of 2). 12 = 1100 has two set bits, so it fails. 1 = 2&sup0; counts as a power of 2.
Live preview
Enter an integer and see binary form plus the bitwise test result.
Algorithm
Require positive n
If n <= 0, return false.
Apply bit trick
Compute n & (n - 1). Powers of two yield 0.
Return result
true when the bitwise result is zero.
📜 Pseudocode
function isPowerOfTwo(n):
if n <= 0:
return false
return (n & (n - 1)) == 0 Check one number (bitwise)
Reference program: tests whether 16 is a power of 2 using n & (n - 1).
using System;
class Program
{
static bool IsPowerOfTwo(int num)
{
return num > 0 && (num & (num - 1)) == 0;
}
static void Main()
{
int number = 16;
if (IsPowerOfTwo(number))
Console.WriteLine($"{number} is a power of 2.");
else
Console.WriteLine($"{number} is not a power of 2.");
}
} Powers of 2 from 1 to 20
Reuses IsPowerOfTwo inside a range loop — matches the reference range output.
using System;
class Program
{
static bool IsPowerOfTwo(int num)
{
return num > 0 && (num & (num - 1)) == 0;
}
static void Main()
{
Console.WriteLine("Power of 2 in the range 1 to 20:");
for (int i = 1; i <= 20; i++)
{
if (IsPowerOfTwo(i))
Console.Write($"{i} ");
}
Console.WriteLine();
}
} Optimization and alternatives
Bit trick (preferred). n > 0 && (n & (n - 1)) == 0 runs in constant time.
Divide by 2. While n % 2 == 0, divide; if you finish at 1, it was a power of two — no bitwise operators needed.
❓ FAQ
🔄 Input / output examples
Input n | Binary | Result |
|---|---|---|
16 | 10000 | Power of 2 |
18 | 10010 | Not a power of 2 |
1 | 1 | Power of 2 |
0 | 0 | Not a power of 2 |
Edge cases and pitfalls
n = 1
1 = 2^0 — valid power of 2.
n = 0
Fails the n > 0 guard even though 0 & (-1) == 0 without the guard.
Not powers of two
Return false for n <= 0.
⏱️ Time and space complexity
| Method | Time | Extra space |
|---|---|---|
Bitwise n & (n - 1) | O(1) | O(1) |
| Repeated divide-by-2 | O(log n) | O(1) |
Range 1..R | O(R) with O(1) check each | O(1) |
Summary
- Powers of two are
2^kand have one set bit in binary. - Use
n > 0 && (n & (n - 1)) == 0for the classic interview answer. - From
1to20, only1, 2, 4, 8, 16qualify.
Powers of two have exactly one bit set in binary — that is why n & (n - 1) becomes zero. RAM sizes like 512 MB or 1 GB follow the same pattern.
8 people found this page helpful
