Open In App

Program to find whether a given number is power of 2

Last Updated : 30 Aug, 2025
Comments
Improve
Suggest changes
181 Likes
Like
Report

Given a positive integer n, check whether it is a power of 2 or not.

Examples: 

Input : n = 16
Output : true
Explanation: 24 = 16

Input : n = 42
Output : false
Explanation: 42 is not a power of 2

Input : n = 1
Output : true
Explanation: 20 = 1

Approach 1: Using Log - O(1) time and O(1) space

The idea is to use the mathematical property that if a number n is a power of 2, then log2(n) will be an integer. We can compute log2(n) and check if it's an integer by comparing if 2 raised to the power of log2(n) equals n.

C++
#include <iostream>
#include <cmath>
using namespace std;

bool isPowerofTwo(int n) {
    if (n <= 0)
        return false;
    
    // Calculate log base 2 of n
    int logValue = log2(n);
    
    // Check if log2(n) is an integer
    // and 2^(logn) = n
    return pow(2, logValue) == n;
}

int main() {
    int n = 16;
    if (isPowerofTwo(n)) {
        cout << "true" << endl;
    }
    else {
        cout << "false" << endl;
    }
}
Java
import java.lang.Math;

class GfG {
    
    static boolean isPowerofTwo(int n) {
        if (n <= 0)
            return false;
        
        // Calculate log base 2 of n
        int logValue = (int)(Math.log(n) / Math.log(2));
        
        // Check if log2(n) is an integer
        // and 2^(logn) = n
        return Math.pow(2, logValue) == n;
    }

    public static void main(String[] args) {
        int n = 16;
        if (isPowerofTwo(n)) {
            System.out.println("true");
        } else {
            System.out.println("false");
        }
    }
}
Python
import math

def isPowerofTwo(n):
    if n <= 0:
        return False
    
    # Calculate log base 2 of n
    logValue = int(math.log2(n))
    
    # Check if log2(n) is an integer
    # and 2^(logn) = n
    return pow(2, logValue) == n

if __name__ == "__main__":
    n = 16
    if isPowerofTwo(n):
        print("true")
    else:
        print("false")
C#
using System;

class GfG {
    
    static bool isPowerofTwo(int n) {
        if (n <= 0)
            return false;
        
        // Calculate log base 2 of n
        int logValue = (int)(Math.Log(n, 2));
        
        // Check if log2(n) is an integer
        // and 2^(logn) = n
        return Math.Pow(2, logValue) == n;
    }

    static void Main() {
        int n = 16;
        if (isPowerofTwo(n)) {
            Console.WriteLine("true");
        } else {
            Console.WriteLine("false");
        }
    }
}
JavaScript
function isPowerofTwo(n) {
    if (n <= 0)
        return false;
    
    // Calculate log base 2 of n
    let logValue = Math.floor(Math.log2(n));
    
    // Check if log2(n) is an integer
    // and 2^(logn) = n
    return Math.pow(2, logValue) === n;
}

let n = 16;
if (isPowerofTwo(n)) {
    console.log("true");
} else {
    console.log("false");
}

Output
true

Approach 2: Using Division and Modulo Operator - O(log n) time and O(1) space

The idea is to repeatedly divide the number by 2 and check if there's any remainder during the process. If n is a power of 2, then dividing it by 2 repeatedly will eventually reduce it to 1 without encountering any odd number in between. If at any point n becomes odd (except for the final value 1), then n is not a power of 2.

C++
#include <iostream>
using namespace std;

bool isPowerofTwo(int n) {
    if (n <= 0)
        return false;
    
    while (n > 1) {
        if (n % 2 != 0)
            return false;
        n = n / 2;
    }
    return true;
}

int main() {
    int n = 16;
    if (isPowerofTwo(n)) {
        cout << "true" << endl;
    }
    else {
        cout << "false" << endl;
    }
}
Java
class GfG {
    
    static boolean isPowerofTwo(int n) {
        if (n <= 0)
            return false;
        
        while (n > 1) {
            if (n % 2 != 0)
                return false;
            n = n / 2;
        }
        return true;
    }

    public static void main(String[] args) {
        int n = 16;
        if (isPowerofTwo(n)) {
            System.out.println("true");
        } else {
            System.out.println("false");
        }
    }
}
Python
# Python program to find whether
# a given number is power of 2

def isPowerofTwo(n):
    if n <= 0:
        return False
    
    while n > 1:
        if n % 2 != 0:
            return False
        n = n // 2
    return True

if __name__ == "__main__":
    n = 16
    if isPowerofTwo(n):
        print("true")
    else:
        print("false")
C#
// C# program to find whether
// a given number is power of 2
using System;

class GfG {
    
    static bool isPowerofTwo(int n) {
        if (n <= 0)
            return false;
        
        while (n > 1) {
            if (n % 2 != 0)
                return false;
            n = n / 2;
        }
        return true;
    }

    static void Main() {
        int n = 16;
        if (isPowerofTwo(n)) {
            Console.WriteLine("true");
        } else {
            Console.WriteLine("false");
        }
    }
}
JavaScript
// JavaScript program to find whether
// a given number is power of 2

function isPowerofTwo(n) {
    if (n <= 0)
        return false;
    
    while (n > 1) {
        if (n % 2 !== 0)
            return false;
        n = Math.floor(n / 2);
    }
    return true;
}

let n = 16;
if (isPowerofTwo(n)) {
    console.log("true");
} else {
    console.log("flase");
}

Output
true

Approach 3: Using Count of Set Bits - O(log n) time and O(1) space

The idea is to leverage the binary representation of powers of 2, which always have exactly one bit set to 1 (the rest are 0). We can count the number of set bits in the binary representation of n, and if there's exactly one set bit, then n is a power of 2. This works because powers of 2 have the form 2^k, which in binary is a 1 followed by k zeros.

C++
#include <iostream>
using namespace std;

bool isPowerofTwo(int n) {
    if (n <= 0)
        return false;
    
    // Count set bits
    int count = 0;
    while (n > 0) {
        if (n & 1)
            count++;
        n = n >> 1;
    }
    
    // If count of set bits is 1, 
    // then n is a power of 2
    return (count == 1);
}

int main() {
    int n = 16;
    if (isPowerofTwo(n)) {
        cout << "true" << endl;
    }
    else {
        cout << "false" << endl;
    }
}
Java
class GfG {
    
    static boolean isPowerofTwo(int n) {
        if (n <= 0)
            return false;
        
        // Count set bits
        int count = 0;
        while (n > 0) {
            if ((n & 1) != 0)
                count++;
            n = n >> 1;
        }
        
        // If count of set bits is 1, 
        // then n is a power of 2
        return (count == 1);
    }

    public static void main(String[] args) {
        int n = 16;
        if (isPowerofTwo(n)) {
            System.out.println("true");
        } else {
            System.out.println("false");
        }
    }
}
Python
def isPowerofTwo(n):
    if n <= 0:
        return False
    
    # Count set bits
    count = 0
    while n > 0:
        if n & 1:
            count += 1
        n = n >> 1
    
    # If count of set bits is 1, 
    # then n is a power of 2
    return count == 1

if __name__ == "__main__":
    n = 16
    if isPowerofTwo(n):
        print("true")
    else:
        print("false")
C#
using System;

class GfG {
    
    static bool isPowerofTwo(int n) {
        if (n <= 0)
            return false;
        
        // Count set bits
        int count = 0;
        while (n > 0) {
            if ((n & 1) != 0)
                count++;
            n = n >> 1;
        }
        
        // If count of set bits is 1, 
        // then n is a power of 2
        return (count == 1);
    }

    static void Main() {
        int n = 16;
        if (isPowerofTwo(n)) {
            Console.WriteLine("true");
        } else {
            Console.WriteLine("false");
        }
    }
}
JavaScript
function isPowerofTwo(n) {
    if (n <= 0)
        return false;
    
    // Count set bits
    let count = 0;
    while (n > 0) {
        if (n & 1)
            count++;
        n = n >> 1;
    }
    
    // If count of set bits is 1, 
    // then n is a power of 2
    return count === 1;
}

let n = 16;
if (isPowerofTwo(n)) {
    console.log("true");
} else {
    console.log("false");
}

Output
true

Approach 4: Using AND Operator - O(1) time and O(1) space

The idea is to use bit manipulation based on the observation that if n is a power of 2, then its binary representation has exactly one bit set to 1, and n-1 will have all bits to the right of that bit set to 1. Therefore, n & (n-1) will always be 0 for powers of 2. This provides an elegant one-line solution to check if a number is a power of 2 or not.

C++
// C++ program to find whether
// a given number is power of 2
#include <iostream>
using namespace std;

bool isPowerofTwo(int n) {
    
    // Check if n is positive and n & (n-1) is 0
    return (n > 0) && ((n & (n-1)) == 0);
}

int main() {
    int n = 16;
    if (isPowerofTwo(n)) {
        cout << "true" << endl;
    }
    else {
        cout << "false" << endl;
    }
}
Java
// Java program to find whether
// a given number is power of 2

class GfG {
    
    static boolean isPowerofTwo(int n) {
        
        // Check if n is positive and n & (n-1) is 0
        return (n > 0) && ((n & (n - 1)) == 0);
    }

    public static void main(String[] args) {
        int n = 16;
        if (isPowerofTwo(n)) {
            System.out.println("true");
        } else {
            System.out.println("false");
        }
    }
}
Python
# Python program to find whether
# a given number is power of 2

def isPowerofTwo(n):
    
    # Check if n is positive and n & (n-1) is 0
    return (n > 0) and ((n & (n - 1)) == 0)

if __name__ == "__main__":
    n = 16
    if isPowerofTwo(n):
        print("true")
    else:
        print("false")
C#
using System;

class GfG {
    
    static bool isPowerofTwo(int n) {
        
        // Check if n is positive and n & (n-1) is 0
        return (n > 0) && ((n & (n - 1)) == 0);
    }

    static void Main() {
        int n = 16;
        if (isPowerofTwo(n)) {
            Console.WriteLine("true");
        } else {
            Console.WriteLine("false");
        }
    }
}
JavaScript
// JavaScript program to find whether
// a given number is power of 2

function isPowerofTwo(n) {
    
    // Check if n is positive and n & (n-1) is 0
    return (n > 0) && ((n & (n - 1)) === 0);
}

let n = 16;
if (isPowerofTwo(n)) {
    console.log("true");
} else {
    console.log("false");
}

Output
true

Approach 5: Using AND & NOT Operator - O(1) time and O(1) space

The idea is to use a bit manipulation technique similar to approach 4, but with a different pattern. When n is a power of 2, it has exactly one bit set to 1. For n-1, all bits to the right of that single set bit will be 1, and the set bit itself will be 0. Taking the NOT of (n-1) flips all these bits, resulting in a number where all bits to the right of the original set bit position will be 0, and all bits to the left (including the bit position itself) will be 1. When we perform n & (~(n-1)), for a power of 2, this operation preserves the original number n because the only set bit in n aligns with a set bit in ~(n-1). For non-powers of 2, this equality doesn't hold.

C++
// C++ program to find whether
// a given number is power of 2
#include <iostream>
using namespace std;

bool isPowerofTwo(int n) {
    
    // Check if n is positive and n & ~(n-1) equals n
    return (n > 0) && ((n & (~(n-1))) == n);
}

int main() {
    int n = 16;
    if (isPowerofTwo(n)) {
        cout << "true" << endl;
    }
    else {
        cout << "false" << endl;
    }
}
Java
class GfG {
    
    // Check if n is positive and n & ~(n-1) equals n
    static boolean isPowerofTwo(int n) {
        return (n > 0) && ((n & (~(n - 1))) == n);
    }

    public static void main(String[] args) {
        int n = 16;
        if (isPowerofTwo(n)) {
            System.out.println("true");
        } else {
            System.out.println("false");
        }
    }
}
Python
def isPowerofTwo(n):
    
    # Check if n is positive and n & ~(n-1) equals n
    return (n > 0) and ((n & (~(n - 1))) == n)

if __name__ == "__main__":
    n = 16
    if isPowerofTwo(n):
        print("Yes")
    else:
        print("No")
C#
using System;

class GfG {
    
    static bool isPowerofTwo(int n) {
        
        // Check if n is positive and n & ~(n-1) equals n
        return (n > 0) && ((n & (~(n - 1))) == n);
    }

    static void Main() {
        int n = 16;
        if (isPowerofTwo(n)) {
            Console.WriteLine("true");
        } else {
            Console.WriteLine("false");
        }
    }
}
JavaScript
// JavaScript program to find whether
// a given number is power of 2

function isPowerofTwo(n) {
    
    // Check if n is positive and n & ~(n-1) equals n
    return (n > 0) && ((n & (~(n - 1))) === n);
}

let n = 16;
if (isPowerofTwo(n)) {
    console.log("true");
} else {
    console.log("false");
}

Output
true

Power of 2 | DSA Problem

Explore