fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. long v = 3 << 11;
  13. v = v << 20;
  14. System.out.println("(3 << 31): " + v + " " + Long.toBinaryString(v));
  15. v = v + 1;
  16. System.out.println("(3 << 31) + 1: " + v + " " + Long.toBinaryString(v));
  17. int w = (int) v;
  18. System.out.println("to_int: " + w + " " + Integer.toBinaryString(w));
  19. int x = 1_000_000_001;
  20. int y = 1_000_000_000;
  21. try {
  22. int result = Math.subtractExact(x, y);
  23. System.out.println("The proper result is " + result);
  24. } catch(ArithmeticException e) {
  25. System.out.println("Sorry, " + e);
  26. }
  27.  
  28. }
  29. }
Success #stdin #stdout 0.19s 55912KB
stdin
Standard input is empty
stdout
(3 << 31):     6442450944  110000000000000000000000000000000
(3 << 31) + 1: 6442450945  110000000000000000000000000000001
to_int:        -2147483647  10000000000000000000000000000001
The proper result is 1