Multiplizieren von long-Ganzzahlen
Es gibt keinen primitiven Datentyp der mehr als 64 Bit (8 Byte) hat, sodass das Ergebnis von long * long mit seinen 128 Bit nur in ein BigInteger komplett passt. Allerdings erlaubt eine neue Methode aus Java 9, die oberen 64 Bit einer long-Multiplikation getrennt zu erfragen, und zwar mit der Methode multiplyHigh(long x, long y) in Math und StrictMath – wobei StrictMath nur auf Math leitet.
BigInteger v = BigInteger.valueOf( Long.MAX_VALUE )
.multiply( BigInteger.valueOf( Long.MAX_VALUE ) );
System.out.println( v ); // 85070591730234615847396907784232501249
long lowLong = Long.MAX_VALUE * Long.MAX_VALUE;
long highLong = Math.multiplyHigh( Long.MAX_VALUE, Long.MAX_VALUE );
BigInteger w = BigInteger.valueOf( highLong )
.shiftLeft( 64 )
.add( BigInteger.valueOf( lowLong ) );
System.out.println( w ); // 85070591730234615847396907784232501249