Integer overflow: Difference between revisions

New post showing additional methods introduced in Java 8, in addition to an existing post which was retained.
(add RPL)
(New post showing additional methods introduced in Java 8, in addition to an existing post which was retained.)
 
(One intermediate revision by one other user not shown)
Line 1,588:
-2147479015
-2147483648
Signed 64-bit:
-9223372036854775808
-8446744073709551616
2
-9223372036709301616
-9223372036854775808
</pre>
 
===Using Java 8===
<syntaxhighlight lang="java">
public final class IntegerOverflow {
 
public static void main(String[] args) {
// The following examples show that Java allows integer overflow without warning
// and calculates an incorrect result.
// From version 8, Java introduced methods which throw an ArithmeticException when overflow occurs,
// which prevents the calculation of an incorrect result. It also allows the programmer to replace an "int"
// with a "long" and to replace a "long" with a BigInteger.
// Uncomment the lines below to see the use of the new methods:
// addExact(), subtractExact(), multiplyExact() and negateExact().
System.out.println("Signed 32-bit:");
System.out.println(-(-2_147_483_647 - 1));
// System.out.println(Math.negateExact(-2_147_483_647 - 1));
System.out.println(2_000_000_000 + 2_000_000_000);
// System.out.println(Math.addExact(2_000_000_000, 2_000_000_000));
System.out.println(-2_147_483_647 - 2_147_483_647);
// System.out.println(Math.subtractExact(-2_147_483_647, 2_147_483_647));
System.out.println(46_341 * 46_341);
// System.out.println(Math.multiplyExact(46_341, 46_341));
System.out.println((-2_147_483_647 - 1) / -1);
// System.out.println(Math.negateExact(Math.subtractExact(-2_147_483_647, 1) / 1));
System.out.println();
System.out.println("Signed 64-bit:");
System.out.println(-(-9_223_372_036_854_775_807L - 1));
// System.out.println(Math.negateExact(-9_223_372_036_854_775_807L - 1));
System.out.println(5_000_000_000_000_000_000L + 5_000_000_000_000_000_000L);
// System.out.println(Math.addExact(5_000_000_000_000_000_000L, 5_000_000_000_000_000_000L));
System.out.println(-9_223_372_036_854_775_807L - 9_223_372_036_854_775_807L);
// System.out.println(Math.subtractExact(-9_223_372_036_854_775_807L, 9_223_372_036_854_775_807L));
System.out.println(3_037_000_500L * 3_037_000_500L);
// System.out.println(Math.multiplyExact(3_037_000_500L, 3_037_000_500L));
System.out.println((-9_223_372_036_854_775_807L - 1) / -1);
// System.out.println(Math.negateExact(Math.subtractExact(-9_223_372_036_854_775_807L, 1) / 1));
}
 
}
</syntaxhighlight>
{{ out }}
<pre>
Signed 32-bit:
-2147483648
-294967296
2
-2147479015
-2147483648
 
Signed 64-bit:
-9223372036854775808
Line 2,885 ⟶ 2,952:
 
However, within this overall framework, Wren also has an unsigned 32-bit integer sub-system when dealing with bitwise operations. All values are converted internally to such integers before the corresponding C bitwise operation is performed (Wren's VM is written in C) and can therefore overflow without warning. Fortunately, we can easily observe these effects by performing the operations required by the task and then (for example) right shifting them by 0 places.
<syntaxhighlight lang="ecmascriptwren">var exprs = [-4294967295, 3000000000 + 3000000000, 2147483647 - 4294967295, 65537 * 65537]
System.print("Unsigned 32-bit:")
for (expr in exprs) System.print(expr >> 0)</syntaxhighlight>
871

edits