When it's a power of 2 keep in mind that you can use a simple and fast shift expression: 1 << exponent

For example:

22 = 1 << 2 = (int) Math.pow(2, 2)
210 = 1 << 10 = (int) Math.pow(2, 10)

For larger exponents (over 31) use long instead:

232 = 1L << 32 = (long) Math.pow(2, 32)

BTW, in Kotlin you have shl instead of <<:

(Java) 1L << 32 = 1L shl 32 (Kotlin)

Answer from Stanislaw Baranski on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-program-to-find-whether-a-no-is-power-of-two
Java Program to find whether a no is power of two - GeeksforGeeks
July 23, 2025 - In any iteration, if n%2 becomes non-zero and n is not 1 then n is not a power of 2. If n becomes 1 then it is a power of 2. ... // Java program to find whether // a no is power of two import java.io.*; class GFG { // Function to check if // ...
🌐
W3Schools
w3schools.com › java › ref_math_pow.asp
Java Math pow() Method
Java Examples Java Videos Java ... System.out.println(Math.pow(10, -2)); Try it Yourself » · The pow() method raises a number to the power of another number....
🌐
Baeldung
baeldung.com › home › java › check if a number is power of 2 in java
Check if a Number Is Power of 2 in Java | Baeldung
May 14, 2024 - Lastly, we can use the logarithm base 2 to check the power of 2. The base-2 logarithm of a number is the exponent to which 2 must be raised to get that number. If the logarithm (base 2) of a number is a whole number, then the number is a power of 2. ...
🌐
Java Code Geeks
javacodegeeks.com › home › core java
Check if a Number Is Power of 2 in Java - Java Code Geeks
July 3, 2024 - We repeatedly divide the number by 2 as long as it is even. Finally, we check if the resulting number is 1. This method utilizes the property that powers of 2 have exactly one bit set in their binary representation.
🌐
Princeton CS
introcs.cs.princeton.edu › java › 13flow › PowersOfTwo.java.html
PowersOfTwo.java
* ****************************... i equals n while (i <= n) { System.out.println(i + " " + powerOfTwo); // print out the power of two powerOfTwo = 2 * powerOfTwo; // double to get the next one i = i + 1; } } }...
🌐
Programiz
programiz.com › java-programming › examples › power-number
Java Program to Calculate the Power of a Number
In this program, we use Java's Math.pow() function to calculate the power of the given base. We can also compute the power of a negative number using the pow() method. class Main { public static void main(String[] args) { // negative number int base = -3, exponent = 2; double result = ...
Find elsewhere
🌐
LeetCode
leetcode.com › problems › power-of-two
Power of Two - LeetCode
Can you solve this real interview question? Power of Two - Given an integer n, return true if it is a power of two. Otherwise, return false. An integer n is a power of two, if there exists an integer x such that n == 2x.
🌐
Quora
quora.com › How-do-I-write-a-Java-program-to-print-as-many-powers-of-2-as-the-user-requests
How to write a Java program to print as many powers of 2 as the user requests - Quora
Next, assuming that the powers of 2 must be integers . The easy way is to use BigDecimal, but it maxes out at [code ](2**2147483647) - 1[/code] - see: How to get biggest BigDecimal value . Beyo...
🌐
w3resource
w3resource.com › java-exercises › basic › java-basic-exercise-205.php
Java - Check an integer is power of 2 or not using O(1) time
Write a Java program to determine if a number is a power of 2 and count the number of right shifts needed to reduce it to 1.
🌐
Quora
quora.com › How-do-I-find-if-a-number-is-power-of-2-for-Java-Assignment
How to find if a number is power of 2 for Java Assignment - Quora
Answer (1 of 3): import java.util.*; public class Power{ public static void main(String[] args) { //int num=0; int rem=0; Scanner sc=new Scanner(System.in); int num=sc.nextInt(); if(num==0) { System.out.println("no"); } while (num!=1) { rem=num%2; num=num/2; } if(num==1 && rem==0...
🌐
Baeldung
baeldung.com › home › java › java numbers › using math.pow in java
Using Math.pow in Java | Baeldung
January 8, 2024 - Before looking at the example, let’s look at the method’s signature: public double pow(double a, double b) The method raises a to the power of b and returns the result as double. In other words, a is multiplied by itself b times. Let’s ...
🌐
Quora
quora.com › What-is-the-difference-between-math-pow-2-i-and-2-I-in-Java
What is the difference between math.pow(2,i) and 2^I in Java? - Quora
Answer (1 of 3): Math.pow function in Java performs the power operation of 2 to the power of i say for example, i is 3. Then 2 to the power 3 is 8 (2*2*2) ^ is a bitwise XOR operator.
🌐
GeeksforGeeks
geeksforgeeks.org › java › math-pow-method-in-java-with-example
Math pow() Method in Java with Example - GeeksforGeeks
March 28, 2025 - Example 1: This example demonstrates how to use the Math.pow() method in Java to calculate the power of a number (base raised to the exponent). ... // Java program to demonstrate working // of Math.pow() method // importing java.lang package ...
🌐
Octoperf
octoperf.com › blog › 2018 › 03 › 16 › java-math-pow
Java Math.pow Through Code Examples - OctoPerf
Usually, when you write the power of a number you use the following syntax: Using Math symbols: 2^5 = 2x2x2x2x2 = 32 Using Java Programming Language: Math.pow(2, 5)