Is there any way to get the high half of the multiplication of two longs in Java? I.e. the part that vanishes due to overflow. (So the upper 64 bits of the 128-
Some of described cases above works wrong. First of all you have to ask yourself what types of operands do you treat (signed/unsigned).
There is a modified code from sample above that is fixed for carry flag (treat x & y as unsigned 64-bit values):
public static long productHi(long x, long y) {
final long x_hi = x >>> 32;
final long y_hi = y >>> 32;
final long x_lo = x & 0xFFFFFFFFL;
final long y_lo = y & 0xFFFFFFFFL;
long result = (x_lo * y_lo) >>> 32;
long a = x_hi * y_lo;
long b = x_lo * y_hi;
long sum = a + b + result;
long carry = ((a & b) | (~sum & (a ^ b))) >>> 63;
result = (sum >>> 32) + x_hi * y_hi + (carry << 32);
return result;
}