How to check if a number is a power of 2 in C#?

A power of 2 is a number of the form 2n where n is a non-negative integer. These numbers have a special property in their binary representation − they contain exactly one bit set to 1.

For example, 8 = 23 has binary representation 1000, and 16 = 24 has binary representation 10000.

n 2n Binary
0 1 0001
1 2 0010
2 4 0100
3 8 1000
4 16 10000

Bitwise Trick: n & (n-1) == 0 Power of 2 8: 1000 7: 0111 8 & 7 = 0000 Not Power of 2 6: 0110 5: 0101 6 & 5 = 0100

Using Bitwise AND Operation

The most efficient method uses the bitwise property that for any power of 2, n & (n-1) equals 0. This works because powers of 2 have exactly one bit set, and subtracting 1 flips all bits after and including that bit −

Example

using System;

class Program {
    static void Main() {
        Console.WriteLine("Testing numbers using bitwise method:");
        Console.WriteLine($"1 is power of 2: {IsPowerOfTwo(1)}");
        Console.WriteLine($"4 is power of 2: {IsPowerOfTwo(4)}");
        Console.WriteLine($"6 is power of 2: {IsPowerOfTwo(6)}");
        Console.WriteLine($"16 is power of 2: {IsPowerOfTwo(16)}");
        Console.WriteLine($"0 is power of 2: {IsPowerOfTwo(0)}");
    }

    static bool IsPowerOfTwo(ulong x) {
        return x > 0 && (x & (x - 1)) == 0;
    }
}

The output of the above code is −

Testing numbers using bitwise method:
1 is power of 2: True
4 is power of 2: True
6 is power of 2: False
16 is power of 2: False
0 is power of 2: False

Using Division Method

This approach repeatedly divides the number by 2. If at any point the number becomes odd (other than 1), it's not a power of 2 −

Example

using System;

class Program {
    static void Main() {
        Console.WriteLine("Testing numbers using division method:");
        Console.WriteLine($"8 is power of 2: {IsPowerOfTwoByDivision(8)}");
        Console.WriteLine($"12 is power of 2: {IsPowerOfTwoByDivision(12)}");
        Console.WriteLine($"32 is power of 2: {IsPowerOfTwoByDivision(32)}");
    }

    static bool IsPowerOfTwoByDivision(ulong n) {
        if (n == 0)
            return false;
        while (n != 1) {
            if (n % 2 != 0)
                return false;
            n = n / 2;
        }
        return true;
    }
}

The output of the above code is −

Testing numbers using division method:
8 is power of 2: True
12 is power of 2: False
32 is power of 2: True

Using Math.Log Method

This method uses logarithms to check if log2(n) is an integer −

Example

using System;

class Program {
    static void Main() {
        Console.WriteLine("Testing numbers using logarithm method:");
        Console.WriteLine($"64 is power of 2: {IsPowerOfTwoByLog(64)}");
        Console.WriteLine($"100 is power of 2: {IsPowerOfTwoByLog(100)}");
        Console.WriteLine($"128 is power of 2: {IsPowerOfTwoByLog(128)}");
    }

    static bool IsPowerOfTwoByLog(double n) {
        if (n <= 0) return false;
        double log = Math.Log2(n);
        return log == Math.Floor(log);
    }
}

The output of the above code is −

Testing numbers using logarithm method:
64 is power of 2: True
100 is power of 2: False
128 is power of 2: True

Performance Comparison

Method Time Complexity Advantages
Bitwise AND O(1) Fastest, works with large integers
Division O(log n) Easy to understand, step-by-step
Logarithm O(1) Mathematical approach, works with doubles

Conclusion

The bitwise AND method x > 0 && (x & (x - 1)) == 0 is the most efficient way to check if a number is a power of 2. It leverages the binary property of powers of 2 and runs in constant time, making it ideal for performance-critical applications.

Updated on: 2026-03-17T07:04:36+05:30

719 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements