Java Math getExponent() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Java 6+

Definition

In Java, Math.getExponent(d) tells you which power of two a number sits between. If 2n <= |d| < 2n+1, the answer is n. For example 1000 lies between 512 (29) and 1024 (210), so Math.getExponent(1000.0) is 9.

A computer stores a double as "a number between 1 and 2, times 2 to some power". getExponent simply reads that power. It is a fast, exact way to get the rough binary size of a number, the same as floor(log2(|d|)) for ordinary values.

A static method of the java.lang.Math class, added in Java 6 (JDK 1.6). Call it on the class name, as in Math.getExponent(...). No import is needed, because classes in java.lang are always visible.

Picture it

Think of the number line cut into "rooms" at 1, 2, 4, 8, 16, 32... The exponent is the room number: 5.0 and 7.9 are both in room 2 (between 4 and 8), 8.0 starts room 3, and 0.75 is in room -1 (between 0.5 and 1).

Syntax

Pass a double or a float; the result is always an int.

Java
int a = Math.getExponent(8.0);      // 3   (8 = 2^3)
int b = Math.getExponent(1000.0);   // 9   (512 <= 1000 < 1024)
int c = Math.getExponent(0.5);      // -1  (0.5 = 2^-1)
int d = Math.getExponent(-8.0);     // 3   (the sign is ignored)
CallWhat it does
Math.getExponent(1.0)Returns 0, because 1 = 20.
Math.getExponent(0.1)Returns -4: 0.1 lies between 0.0625 and 0.125.
Math.getExponent((double) n) + 1Number of bits needed to write a positive int n in binary.
Math.getExponent(0.0)Returns -1023 (Double.MIN_EXPONENT - 1).

An int argument widens to float, not double, so Math.getExponent(n) runs the float version. That is fine for small values, but a large int such as 33554431 is rounded up to 225 as a float and gives 25 instead of 24. Cast to double to be safe.

Library declarations
Java
// java.lang.Math
public static int getExponent(double d)
public static int getExponent(float f)

Parameters

ParameterDescription
d / fThe number whose binary exponent you want. Its sign does not matter.

Return Value

Return ValueDescription
int (double version)The unbiased exponent, from -1022 to 1023 for normal numbers. Zero and very tiny (subnormal) values give -1023; NaN and infinities give 1024.
int (float version)From -126 to 127 for normal floats. Zero and subnormal floats give -127; NaN and infinities give 128.

Compare with Similar Methods

Methods that also look at the binary size of a number:

MethodHow it differs
scalb(d, n)The reverse idea: multiplies by 2n by changing the exponent.
ulp(d)The gap between neighbours; equals 2getExponent(d) - 52 for normal doubles.
log(x) / log(2)A real-valued log2; slower and may be off by a tiny rounding error.
Integer.numberOfLeadingZeros(n)Integer-only version: 31 - numberOfLeadingZeros(n) gives the same answer for a positive int.

Java Examples

Example 1 — Find the Power of Two Each Number Sits Between

Print the exponent for whole numbers, fractions, and a negative value.

Java
public class MathGetExponentBasics {
    public static void main(String[] args) {
        double[] values = {1.0, 8.0, 0.5, 1000.0, -8.0, 0.1};
        for (double v : values) {
            System.out.println("getExponent(" + v + ") = " + Math.getExponent(v));
        }
    }
}

How It Works

  1. Exact powers of two. 1.0 is 20, 8.0 is 23, and 0.5 is 2-1, so their exponents are 0, 3, and -1.
  2. In-between values round down to a room. 1000.0 is at least 512 but below 1024, so it reports 9; 0.1 is between 0.0625 (2-4) and 0.125 (2-3), so it reports -4.
  3. Sign does not matter. The exponent describes the size of the number only, so -8.0 gives the same 3 as 8.0.

Example 2 — Count Binary Digits and Spot Powers of Two

Use the exponent to count how many bits a number needs, and combine it with scalb to test for a power of two.

Java
public class MathGetExponentBits {
    static boolean isPowerOfTwo(int n) {
        return n > 0 && n == Math.scalb(1.0, Math.getExponent((double) n));
    }

    public static void main(String[] args) {
        int[] numbers = {1, 255, 256, 1000};
        for (int n : numbers) {
            int bits = Math.getExponent((double) n) + 1;
            System.out.println(n + " needs " + bits + " bits, power of two? " + isPowerOfTwo(n));
        }
    }
}

How It Works

  1. Exponent plus one is the bit count. 255 is 11111111 in binary: its exponent is 7, so it needs 8 bits. 256 is 100000000, exponent 8, so 9 bits.
  2. Rebuild the power and compare. Math.scalb(1.0, e) is exactly 2e. Only a power of two equals the power of two its own exponent points to: 256 matches 256.0, but 1000 does not match 512.0.
  3. The double cast is deliberate. Every int fits exactly in a double, so the exponent is always right; without the cast Java would pick the float overload.

Example 3 — Special Values and the float Version

See the "out of range" results for zero, the tiniest double, infinity, and NaN.

Java
public class MathGetExponentSpecial {
    public static void main(String[] args) {
        System.out.println("0.0:       " + Math.getExponent(0.0));
        System.out.println("MIN_VALUE: " + Math.getExponent(Double.MIN_VALUE));
        System.out.println("MAX_VALUE: " + Math.getExponent(Double.MAX_VALUE));
        System.out.println("Infinity:  " + Math.getExponent(Double.POSITIVE_INFINITY));
        System.out.println("NaN:       " + Math.getExponent(Double.NaN));
        System.out.println("0.0f:      " + Math.getExponent(0.0f));
        System.out.println("NaN (f):   " + Math.getExponent(Float.NaN));
    }
}

How It Works

  1. Zero and subnormals share -1023. Zero has no power of two at all, and Double.MIN_VALUE (4.9E-324) is a "subnormal" tiny value; both get the marker Double.MIN_EXPONENT - 1.
  2. The largest normal exponent is 1023. Double.MAX_VALUE is just under 21024, so it lives in room 1023, the top one.
  3. Not real numbers get 1024. Infinity and NaN report Double.MAX_EXPONENT + 1; the float overload uses its own smaller range, -127 and 128.

Frequently Asked Questions

The power of two that the number sits between: if 2 to the n is less than or equal to the absolute value and 2 to the n+1 is greater, it returns n. Math.getExponent(1000.0) is 9.
Yes for ordinary positive numbers, and it is exact and faster because it just reads the stored exponent. It differs for zero, subnormal values, NaN, and infinity, which return special marker values.
It returns -1023, which is Double.MIN_EXPONENT - 1. The float version returns -127 for 0.0f.
1024 (Double.MAX_EXPONENT + 1) means the argument is NaN or infinite. For a float argument the marker is 128.
Java 6 (JDK 1.6), for both the float and the double version.

Next: hypot()

The length sqrt(x*x + y*y) without overflow, for example a distance between points.

hypot() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

14 people found this page helpful