Check Power of 2 in C#

Beginner
⏱️ 9 min read
📚 Updated: Jul 2026
🎯 2 Code Examples
Bit manipulation

What you’ll learn

  • What a power of 2 means: n = 2^k for a whole number k.
  • The classic bitwise test n > 0 && (n & (n - 1)) == 0.
  • How to check 16 and list all powers of 2 from 1 to 20.

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 if and for loop 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.

Live result
Press “Run check”.

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

Pseudocode
function isPowerOfTwo(n):
    if n <= 0:
        return false
    return (n & (n - 1)) == 0
1

Check one number (bitwise)

Reference program: tests whether 16 is a power of 2 using n & (n - 1).

c#
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.");
    }
}
2

Powers of 2 from 1 to 20

Reuses IsPowerOfTwo inside a range loop — matches the reference range output.

c#
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

A positive integer n is a power of 2 when n = 2^k for some whole number k — examples: 1, 2, 4, 8, 16.
Yes. 1 = 2^0.
A power of two has exactly one set bit. The expression n & (n - 1) clears the lowest set bit; only powers of two become 0.
Zero and negatives are not powers of two in this tutorial's definition.
Yes. Repeatedly divide by 2 while n is even; if you end at 1, n was a power of two.
The bitwise check is O(1). The divide-by-2 loop is O(log n).

🔄 Input / output examples

Input nBinaryResult
1610000Power of 2
1810010Not a power of 2
11Power of 2
00Not a power of 2

Edge cases and pitfalls

One

n = 1

1 = 2^0 — valid power of 2.

Zero

n = 0

Fails the n > 0 guard even though 0 & (-1) == 0 without the guard.

Negatives

Not powers of two

Return false for n <= 0.

⏱️ Time and space complexity

MethodTimeExtra space
Bitwise n & (n - 1)O(1)O(1)
Repeated divide-by-2O(log n)O(1)
Range 1..RO(R) with O(1) check eachO(1)

Summary

  • Powers of two are 2^k and have one set bit in binary.
  • Use n > 0 && (n & (n - 1)) == 0 for the classic interview answer.
  • From 1 to 20, only 1, 2, 4, 8, 16 qualify.
Did you know?

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.

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