WikiGalaxy

Personalize

Detecting Power of Two

Understanding Powers of Two

A number is considered a power of two if it can be expressed as 2 raised to an integer power. For example, numbers like 1, 2, 4, 8, 16, etc., are all powers of two because they can be written as 20, 21, 22, 23, and 24 respectively.

Why Detect Powers of Two?

Detecting whether a number is a power of two is essential in various computing scenarios, such as optimizing algorithms, memory allocation, and understanding binary operations.

Bitwise Approach

One efficient way to determine if a number is a power of two is to use bitwise operations. A number that is a power of two has exactly one bit set in its binary representation. For instance, 4 is 100 in binary, and 8 is 1000.


public class PowerOfTwo {
    public static boolean isPowerOfTwo(int n) {
        return (n > 0) && ((n & (n - 1)) == 0);
    }
    public static void main(String[] args) {
        int number = 16;
        System.out.println(number + " is power of two: " + isPowerOfTwo(number));
    }
}
    

Mathematical Approach

Another method to check if a number is a power of two is using logarithms. If the logarithm (base 2) of a number is an integer, then the number is a power of two.


import java.lang.Math;

public class PowerOfTwoLog {
    public static boolean isPowerOfTwo(int n) {
        if (n <= 0) return false;
        double log2 = Math.log(n) / Math.log(2);
        return (log2 == Math.floor(log2));
    }
    public static void main(String[] args) {
        int number = 32;
        System.out.println(number + " is power of two: " + isPowerOfTwo(number));
    }
}
    

Iterative Division Method

This method involves continuously dividing the number by 2. If the number eventually reduces to 1, it is a power of two.


public class PowerOfTwoDivision {
    public static boolean isPowerOfTwo(int n) {
        if (n <= 0) return false;
        while (n % 2 == 0) {
            n /= 2;
        }
        return n == 1;
    }
    public static void main(String[] args) {
        int number = 64;
        System.out.println(number + " is power of two: " + isPowerOfTwo(number));
    }
}
    

Using Recursion

A recursive approach can also be implemented to determine if a number is a power of two by repeatedly dividing it by 2.


public class PowerOfTwoRecursive {
    public static boolean isPowerOfTwo(int n) {
        if (n <= 0) return false;
        if (n == 1) return true;
        if (n % 2 != 0) return false;
        return isPowerOfTwo(n / 2);
    }
    public static void main(String[] args) {
        int number = 128;
        System.out.println(number + " is power of two: " + isPowerOfTwo(number));
    }
}
    

Using Bit Count

Another approach is to count the number of set bits in a number's binary representation. If there is exactly one set bit, the number is a power of two.


public class PowerOfTwoBitCount {
    public static boolean isPowerOfTwo(int n) {
        return Integer.bitCount(n) == 1;
    }
    public static void main(String[] args) {
        int number = 256;
        System.out.println(number + " is power of two: " + isPowerOfTwo(number));
    }
}
    

Console Output:

16 is power of two: true

32 is power of two: true

64 is power of two: true

128 is power of two: true

256 is power of two: true

logo of wikigalaxy

Newsletter

Subscribe to our newsletter for weekly updates and promotions.

Privacy Policy

 • 

Terms of Service

Copyright © WikiGalaxy 2025