private static final int FIRST = 8;
private static final int SECOND = (4 * 1024 * 1024)/8;
private static final int THIRD = (4 * 1024 * 1024);
private static final long RESULT = FIRST *SECOND * THIRD;
Why is the product of the 3 coming out to be 0?
Why is the product of the 3 coming out to be 0?
Your multiplication is being done in int
arithmetic, and it's overflowing, with a result of 0. You're basically doing 224 * 224, i.e. 248 - and the bottom 32 bits of that results are all 0. The fact that you assign the result of the operation to a long
doesn't change the type used to perform the operation.
To perform the arithmetic using 64-bit integer arithmetic, just change an operand to be long
:
private static final long RESULT = (long) FIRST * SECOND * THIRD;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With