An int is represented with 32 bits. Thus any value between -2^31 and 2^31-1 can be represented. Nothing out of this range.

You can use a long (64 bits), or a BigInteger (a datastructures that can represented all numbers up to the memory limit).

The disadvantage of using these structures (especially BigInteger) is that a CPU does not always offer instructions for arithmetic. Thus adding two BigInteger instances requires more time than doing this with an int or long. In case of a long, if the CPU is 32-bit, it requires at least two instructions to process this.


On a sidenote. A CPU offers a better way to calculate the powers of two: shift operations.

You can simply write:

long value = 0x01L << power;//power of two, all in one simple instruction.

This works as follow: a shift moves bits to the left. Thus if the original value is:

  0001 1011 << 2
= 0110 1100

Shifting to the left in a binary representation is arithmetically the same as multiplying with two.

Answer from willeM_ Van Onsem on Stack Overflow
🌐
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 ...
Top answer
1 of 2
4

This is due to overflow of the int data type.

Java's int size is 32 bits, so the range is -2,147,483,648 to 2,147,483,647.

2^31 = 2147483648

So it is overflowing to -2147483648 as the binary value of 2,147,483,647 is 01111111111111111111111111111111 (one zero and 31 ones), where the first bit is the "sign bit" (2's complement form).

If you try to go beyond this limit (2,147,483,647) by 1 (i.e. adding 1 to it), it changes the sign bit to 1, making this int negative.

So it will become 10000000000000000000000000000000 (1 one and 31 zeros), giving you the answer -2147483648.

2 of 2
3

larger exponents return 0 (however I think this may have to do with the fact that we need to use ints vs longs.)

Correct.

Copyint i = (int) 2147483648L; // -2147483648 due to over flow
int j = i * 2; // 0 due to overflow.

You can use long however this has the same problem but for a higher value.

Copypublic static long recPower(int baseNum, int power) {
    if (power < 0) throw new IllegalArgumentException();
    return power == 0 ? 1L : baseNum * recPower(baseNum, power - 1);
}

One way to check for an overflow is to see

Copypublic static long recPower(int baseNum, int power) {
    if (power < 0) throw new IllegalArgumentException();
    return power == 0 ? 1L : baseNum * recPower(baseNum, power - 1);
}

or to check for overflow

Copypublic static long recPower(int baseNum, int power) {
    if (power < 0) throw new IllegalArgumentException();
    return power == 0 ? 1L 
           : Math.multiplyExact(baseNum, recPower(baseNum, power - 1));
}

You can use BigInteger which has a much, much greater limit.

🌐
Programiz
programiz.com › java-programming › examples › power-number
Java Program to Calculate the Power of a Number
Can you solve the following challenge? ... Write a function to calculate the power of a number. The formula to calculate the power is given as: result = base ^ exponent. Return the power of base raised to exponent. For example, if base = 2 and exponent = 3, the expected output is 8.
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-program-to-calculate-power-of-a-number
Java Program to Calculate Power of a Number - GeeksforGeeks
Given a number N and a power P, the task is to find the exponent of this number raised to the given power, i.e. NP. Examples: Input: N = 5, P = 2 Output: 25 Input: N = 2, P = 5 Output: 32 ... // Java program to find the power of a number // using Recursion class GFG { // Function to calculate N raised to the power P static int power(int N, int P) { if (P == 0) return 1; else return N * power(N, P - 1); } // Driver code public static void main(String[] args) { int N = 2; int P = 3; System.out.println(power(N, P)); } }
Published   July 12, 2025
🌐
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 - // Java program to find whether // a no is power of two import java.io.*; class GFG { // Function to check if // x is power of 2 static boolean isPowerOfTwo(int n) { if (n == 0) return false; while (n != 1) { if (n % 2 != 0) return false; n = n / 2; } return true; } // Driver program public static void main(String args[]) { if (isPowerOfTwo(31)) System.out.println("Yes"); else System.out.println("No"); if (isPowerOfTwo(64)) System.out.println("Yes"); else System.out.println("No"); } } // This code is contributed by Nikita tiwari.
🌐
Baeldung
baeldung.com › home › java › java numbers › using math.pow in java
Using Math.pow in Java | Baeldung
January 8, 2024 - DecimalFormat df = new DecimalFormat(".00"); double dblResult = Math.pow(4.2, 3); In this quick article, we have seen how to use the Java’s Math.pow() method to calculate the power of any given base.
Find elsewhere
🌐
Princeton CS
introcs.cs.princeton.edu › java › 13flow › PowersOfTwo.java.html
PowersOfTwo.java
/****************************************************************************** * Compilation: javac PowersOfTwo.java * Execution: java PowersOfTwo n * * This program takes a command-line argument n and prints a table of * the powers of 2 that are less than or equal to 2^n. * * % java PowersOfTwo 5 * 0 1 * 1 2 * 2 4 * 3 8 * 4 16 * 5 32 * * % java PowersOfTwo 6 * 0 1 * 1 2 * 2 4 * 3 8 * 4 16 * 5 32 * 6 64 * * Remarks * ------------ * Only works if 0 <= n < 31 since 2^31 overflows an int.
🌐
Medium
rameshfadatare.medium.com › java-program-to-calculate-the-power-of-a-number-4e8f493e1c24
Java Program to Calculate the Power of a Number | by Ramesh Fadatare | Medium
December 13, 2024 - Using Math.pow(): The Math.pow() method is a built-in function in Java that calculates the power of a number. It takes two arguments: the base and the exponent. Using a Loop: A loop is used to multiply the base by itself as many times as indicated by the exponent. This method works well for integer values. The program prints the result of the base raised to the power of the exponent using System.out.println(). Enter the base number: 2 Enter the exponent: 3 2.0 raised to the power of 3.0 is 8.0
🌐
Medium
medium.com › @soham-khade › 231-power-of-two-leetcode-problem-java-2c213e5a58a5
231. Power of Two LeetCode Problem Java - Soham - Medium
February 19, 2024 - An integer n is a power of two, if there exists an integer x such that n == 2^x. Constraints: -2^31 <= n <= 2^31 - 1 · Example 1: Input: n = 1 Output: true Explanation: 20 = 1 · Example 2: Input: n = 16 Output: true Explanation: 24 = 16 · ...
🌐
Sololearn
sololearn.com › en › Discuss › 112634 › intmathpowx-y
(int)Math.pow(x, y) | Sololearn: Learn to code for FREE!
int variables can only hold integer values up till 2,147,483,647 . If you do ' 2^31' the answer is 2,147,483,648. To prevent the system from crashing, the number was reduced to the max amount which the variable can hold.
🌐
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 - 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. Here’s the Java code demonstrating this method:
🌐
W3Schools
w3schools.com › java › ref_math_pow.asp
Java Math pow() Method
System.out.println(Math.pow(2, ...ln(Math.pow(8, -1)); System.out.println(Math.pow(10, -2)); ... The pow() method raises a number to the power of another number....
🌐
LeetCode
leetcode.com › problems › power-of-two
Power of Two - LeetCode
Otherwise, return false. An integer n is a power of two, if there exists an integer x such that n == 2x. Example 1: Input: n = 1 Output: true Explanation: 20 = 1 Example 2: Input: ...
🌐
Studytonight
studytonight.com › java-programs › java-program-to-calculate-the-power-of-a-number
Java Program to Calculate the Power of a Number - Studytonight
Enter the base value: 2 Enter the exponent value: 3 2 raised to the power 3 is: 8 · In this program, we will see how to calculate the power of a number using a for loop. ... Create an instance of the Scanner class. Declare two variables for the base and exponent. Ask the user to initialize both variables. Use a for loop to calculate the power of a number. Print the calculated value. ... Below is the code for the same. //Java Program to Calculate the Power of a number import java.util.Scanner; public class Main { public static void main(String[] args) { //Take input from the user //Create an i