Math multiplyHigh() method in Java with Examples

The multiplyHigh(long x, long y) method of Math 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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// multiplyHigh() method of Math class
  
public class GFG {
  
    // Main method
    public static void main(String[] args)
    {
  
        // two long values
        long a = 45267356745l, b = 45676556735l;
  
        // apply multiplyHigh method
        long c = Math.multiplyHigh(a, b);
  
        // print result
        System.out.println(a + " * "
                           + b + " = "
                           + c);
    }
}

chevron_right


Output:

45267356745 * 45676556735 = 112

Program 2: Multiplying two long contains Long.MAX values.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// multiplyHigh() method of Math class
  
public class GFG {
  
    // Main method
    public static void main(String[] args)
    {
  
        // two Integer values
        long a = Long.MAX_VALUE, b = Long.MAX_VALUE;
  
        // apply multiplyHigh method
        long c = Math.multiplyHigh(a, b);
  
        // print result
        System.out.println(a + " * "
                           + b + " = " + c);
    }
}

chevron_right


Output:

9223372036854775807 * 9223372036854775807 = 4611686018427387903

References: https://docs.oracle.com/javase/10/docs/api/java/lang/Math.html#multiplyHigh(long, long)



My Personal Notes arrow_drop_up

I am a Developer I love to code and bring my ideas alive

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.