fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6. import java.math.BigInteger;
  7.  
  8. /* Name of the class has to be "Main" only if the class is public. */
  9. class Ideone
  10. {
  11. public static void main (String[] args) throws java.lang.Exception
  12. {
  13. long x = -1501598000831384712L;
  14. long y = -735932670715772870L;
  15. System.out.println(hex(x) + " * " + hex(y) + " = " + hex(hmul(x, y)));
  16. }
  17.  
  18. static String hex(long x) {
  19. return String.format("0x%016x", x);
  20. }
  21.  
  22. static long hmulbigint(long x, long y) {
  23. BigInteger c = BigInteger.valueOf(2).pow(64);
  24. BigInteger xb = BigInteger.valueOf(x);
  25. if (x < 0) xb = xb.add(c);
  26. BigInteger yb = BigInteger.valueOf(y);
  27. if (y < 0) yb = yb.add(c);
  28. BigInteger p = xb.multiply(yb);
  29. return p.shiftRight(64).longValue();
  30. }
  31.  
  32. static long hmul(long x, long y) {
  33. // split
  34. long xl = x & 0xffffffffL;
  35. long xh = x >>> 32;
  36. long yl = y & 0xffffffffL;
  37. long yh = y >>> 32;
  38. // partial products
  39. long t00 = xl * yl;
  40. long t01 = xh * yl;
  41. long t10 = xl * yh;
  42. long t11 = xh * yh;
  43. // resolve carries
  44. t11 += (t10 >>> 32) + (t01 >>> 32);
  45. long tc = (t10 & 0xffffffffL) + (t01 & 0xffffffffL) + (t00 >>> 32);
  46. t11 += tc >>> 32;
  47. return t11;
  48. }
  49. }
Success #stdin #stdout 0.1s 28064KB
stdin
Standard input is empty
stdout
0xeb294092f5023378 * 0xf5c971229761c03a = 0xe1c7861d53e80bf3