When you multiply two long values, the true answer can be up to 128 bits long, but a long holds only 64. The normal * keeps the low 64 bits and throws the rest away. Math.multiplyHigh(x, y) returns exactly the part that was thrown away: the high 64 bits.
Together, the high half (multiplyHigh) and the low half (x * y) describe the complete 128-bit product. This is an advanced tool for overflow checks, fixed-point maths, and hashing; most programs only need multiplyExact.
A static method of the java.lang.Math class, added in Java 9 (JDK 9). Call it on the class name, as in Math.multiplyHigh(...). No import is needed, because classes in java.lang are always visible. Requires Java 9 or newer.
Syntax
Pass two long values. The result is a signed long; the low half comes from the ordinary x * y.
Java
long high = Math.multiplyHigh(Long.MAX_VALUE, 4); // 1
long low = Long.MAX_VALUE * 4; // -4
long none = Math.multiplyHigh(1000L, 1000L); // 0 (product is small)
long neg = Math.multiplyHigh(-1L, 1L); // -1 (sign bits)
Call
What it does
Math.multiplyHigh(1000L, 1000L)
Small product: the high half is 0.
Math.multiplyHigh(1L << 62, 4L)
The product is exactly 2 to the 64: high half 1.
Math.multiplyHigh(-1L, 1L)
The product -1 is 128 one-bits, so the high half is -1.
Math.multiplyHigh(a, b) == ((a * b) >> 63)
true when a * b fits in a long.
Think of the 128-bit product as high * 2^64 + low, where low is read as an unsigned 64-bit number. The high half rounds toward negative infinity, as an arithmetic shift would, which is why the high half of a negative product is negative.
Library declarations
Java
// java.lang.Math (Java 9+)
public static long multiplyHigh(long x, long y)
Parameters
Parameter
Description
x
The first long factor.
y
The second long factor.
Return Value
Return Value
Description
long
The most significant 64 bits of the 128-bit signed product of x and y. Never throws.
Compare with Similar Methods
Choosing between the long multiplication tools:
Method
How it differs
x * y
Gives only the low 64 bits; the high bits are lost.
The 128-bit value.Long.MAX_VALUE is 2^63 - 1, so times 4 it is 2^65 - 4. Dividing by 2^64 (rounding down) gives 1, which is the high half.
The low half. The remainder, 2^64 - 4, is what the plain * keeps; read as a signed long it prints -4.
Small, negative, and huge. 1000 times 1000 is only 1000000, so the high half is 0. -1 times 1 is -1, a 128-bit run of 1 bits, so the high half is -1. And MAX times MAX is 2^126 - 2^64 + 1, whose high half is 2^62 - 1, or 4611686018427387903.
Example 2 — Does a Long Product Fit? Check With the High Half
If the high half equals the sign extension of the low half, the product fits in a long. Java 9+.
Java
public class MathMultiplyHighFits {
static boolean fits(long a, long b) {
long high = Math.multiplyHigh(a, b);
long low = a * b;
return high == (low >> 63);
}
public static void main(String[] args) {
long[][] pairs = {
{3_000_000_000L, 3_000_000_000L},
{4_000_000_000L, 4_000_000_000L},
{-3_000_000_000L, 3_000_000_000L},
{Long.MIN_VALUE, -1L}
};
for (long[] p : pairs) {
System.out.println(p[0] + " x " + p[1] + " fits? " + fits(p[0], p[1]));
}
}
}
Output
3000000000 x 3000000000 fits? true
4000000000 x 4000000000 fits? false
-3000000000 x 3000000000 fits? true
-9223372036854775808 x -1 fits? false
How It Works
The sign-extension test.low >> 63 is 0 for a non-negative low and -1 for a negative one. If high is the same value, the 128-bit product is just low padded with copies of its sign bit.
Fits and does not fit. 3 billion squared is 9000000000000000000, below 9.22 quintillion, and -3000000000 times 3000000000 is -9000000000000000000, so both pass. 4 billion squared is 1.6E19; its low half reads as negative while the high half is 0, so the test fails.
The classic edge case.Long.MIN_VALUE times -1 is 2^63, one more than Long.MAX_VALUE. The high half is 0 but the low half is negative, so fits correctly reports false.
Example 3 — Rebuild the Full 128-Bit Product
Combine the two halves with BigInteger and check the result against a direct BigInteger multiplication. Java 9+.
Java
import java.math.BigInteger;
public class MathMultiplyHighRebuild {
public static void main(String[] args) {
long a = 10_000_000_000L;
long high = Math.multiplyHigh(a, a);
long low = a * a;
BigInteger lowUnsigned = new BigInteger(Long.toUnsignedString(low));
BigInteger rebuilt = BigInteger.valueOf(high).shiftLeft(64).add(lowUnsigned);
BigInteger direct = BigInteger.valueOf(a).multiply(BigInteger.valueOf(a));
System.out.println("high: " + high);
System.out.println("low: " + low);
System.out.println("rebuilt: " + rebuilt);
System.out.println("direct: " + direct);
}
}
A product of 1E20. Ten billion squared is 100000000000000000000, which is about 5.4 times 2^64, so the high half (rounded down) is 5.
The low half. 1E20 minus 5 times 2^64 leaves 7766279631452241920. It is below 2^63, so it prints as a positive long; Long.toUnsignedString makes sure larger lows would be read as unsigned.
Shift and add.high shifted left by 64 bits (that is, times 2^64) plus the unsigned low half restores 100000000000000000000, identical to the direct BigInteger product.
Frequently Asked Questions
The upper (most significant) 64 bits of the 128-bit signed product of two longs. The ordinary * operator gives you the lower 64 bits.
Java 9. On older versions you would use BigInteger or write the 64-bit halves by hand.
It is 1. Long.MAX_VALUE times 4 is 2^65 - 4, and the high 64 bits of that number are 1 (the low 64 bits read as -4).
The product is -1, which in 128-bit two's complement is all one-bits. Its high 64 bits are therefore all ones, which is -1 as a signed long.
Use multiplyExact when you just want an exception on overflow. multiplyHigh is for advanced cases where you need the overflowed bits themselves, such as 128-bit arithmetic, fixed-point maths, or hash functions.