StrictMath multiplyHigh() method in Java with Examples
Last Updated :
29 Oct, 2018
The multiplyHigh(long x, long y) method of StrictMath class is used to return the most significant 64 bits from the 128-bit product of two 64-bit factors. This method takes two long values as input and a long is a 64-bit number. This method multiplies both long values and returns the most significant 64 bits from the product result which can be 128 bit long.
Syntax:
public static long multiplyHigh(long x, long y)
Parameters: This method accepts two long x, y as parameter where x and y are argument we want to multiply.
Return Value: This method returns the most significant 64 bits long value from the 128-bit product of two 64-bit numbersi.e. X * Y.
Note: This method was added in JDK 9. Hence it wont run in Online IDE.
Below programs illustrate the multiplyHigh() method:
Program 1:
public class GFG {
public static void main(String[] args)
{
long a = 45267356745l, b = 45676556735l;
long c = StrictMath.multiplyHigh(a, b);
System.out.println(a + " * "
+ b + " = "
+ c);
}
}
|
Output:
45267356745 * 45676556735 = 112
Program 2: Multiplying two long contains Long.MAX values.
public class GFG {
public static void main(String[] args)
{
long a = Long.MAX_VALUE, b = Long.MAX_VALUE;
long c = StrictMath.multiplyHigh(a, b);
System.out.println(a + " * "
+ b + " = " + c);
}
}
|
Output:
9223372036854775807 * 9223372036854775807 = 4611686018427387903
References: https://docs.oracle.com/javase/10/docs/api/java/lang/StrictMath.html#multiplyHigh(long, long)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...