Library contents

Java Math.multiplyHigh

Return the high 64 bits of a signed 128-bit product represented by two long inputs.

A simple example

Math.multiplyHigh

  1. Start with 2 and 3
  2. Do this Read the upper 64 bits of their product
  3. Result 0
The product 6 fits entirely in the lower half of the 128-bit result.
Main.java
class Main {
    public static void main(String[] args) throws Exception {
        System.out.println(Math.multiplyHigh(2L, 3L));
    }
}
Output
0

Syntax

Java syntax
Math.multiplyHigh(units, factor)
JDK 21 method signatures (1)
public static long multiplyHigh(long x, long y)
Run these examples in Java

Save one example at a time as Main.java. Use JDK 21.

java Main.java

Keep the high word of a large meter total

A device transmits a counter as a fixed-width bit pattern. When scaling or multiplying that value, the reader must keep track of which part of the wider result belongs in the stored field.

From the programlong units = 4611686018427387904L
Result1
Main.java

class Main {
    public static void main(String[] args) throws Exception {
        long units = 4611686018427387904L;
        long factor = 4L;
        System.out.println(Math.multiplyHigh(units, factor));
    }
}
Output
1

Multiplying two to the power of 62 by four produces two to the power of 64. Its high word is one and its low word is zero.

Advertisements

Keep the upper word of a packet counter

A packet counter stores a large total in two 64-bit words. Its decoder applies an eightfold counter scale and needs the upper word of the exact product.

Given4611686018427387904 × 8
Worked result2

Divide the full nonnegative product by 2⁶⁴ and keep its integer quotient.

Keep the upper word of a storage meter

A storage meter stores a large total in two 64-bit words. Its decoder applies a sixteenfold counter scale and needs the upper word of the exact product.

Given2305843009213693952 × 16
Worked result2

Divide the full nonnegative product by 2⁶⁴ and keep its integer quotient.

What to watch for

The high word alone is only part of the product. Use BigInteger when you need the full decimal value.

Documentation for Math.multiplyHigh