-2

Why if I multiply int num = 2,147,483,647 by the same int num is it returning 1 as result? Note that I am in the limit of the int possible value.

I already try to catch the exception but still give the result as 1.

user212514
  • 3,110
  • 1
  • 15
  • 11
edgm
  • 13
  • 1
  • 6

6 Answers6

3

Before any multiplication java translates ints to binary numbers. So you are actually trying to multiply 01111111111111111111111111111111 by 01111111111111111111111111111111. The result of this is something like 1111111111111111111111111111111000000000000000000000000000000001. The int can hold just 32 bits, so in fact you get 00000000000000000000000000000001 which is =1 in decimal.

Yev
  • 321
  • 3
  • 9
2

In integer arithmetic, Java doesn't throw an exception when an overflow occurs. Instead, it just the 32 least significant bits of the outcome, or equivalently, it "wraps around". That is, if you calculate 2147483647 + 1, the outcome is -2147483648.

2,147,483,647 squared happens to be, in binary:

11111111111111111111111111111100000000000000000000000000000001

The least significant 32 bits of the outcome are equal to the value 1.

If you want to calculate with values which don't fit in 32 bits, you have to use either long (if 64 bits are sufficient) or java.math.BigInteger (if not).

Hoopje
  • 12,677
  • 8
  • 34
  • 50
1

int cannot handle just any large value.Look here. In JAVA you have an exclusive class for this problem which comes quite handy

import java.math.BigInteger;

 public class BigIntegerDemo {

  public static void main(String[] args) {

    BigInteger b1 = new BigInteger("987654321987654321000000000"); //change it to your number
    BigInteger b2 = new BigInteger("987654321987654321000000000"); //change it to your number

    BigInteger product = b1.multiply(b2);
    BigInteger division = b1.divide(b2);

    System.out.println("product = " + product);
    System.out.println("division = " + division);     
}

}

Source : Using BigInteger In JAVA

Community
  • 1
  • 1
Rahul Jha
  • 1,131
  • 1
  • 10
  • 25
1

The Java Language Specification exactly rules what should happen in the given case.

If an integer multiplication overflows, then the result is the low-order bits of the mathematical product as represented in some sufficiently large two's-complement format. As a result, if overflow occurs, then the sign of the result may not be the same as the sign of the mathematical product of the two operand values.

It means, that when you multiply two ints, the result will be represented in a long value first (that type holds sufficient bits to represent the result). Then, because you assign it to an int variable, the lower bits are kept for your int.

The JLS also says:

Despite the fact that overflow, underflow, or loss of information may occur, evaluation of a multiplication operator * never throws a run-time exception.

That's why you never get an exception.

My guess: Store the result in a long, and check what happens if you downcast to int. For example:

int num = 2147483647;
long result = num * num;
if (result != (long)((int)result)) {
    // overflow happened
}

To really follow the arithmetics, let's follow the calculation:

((2^n)-1) * ((2^n)-1) = 
 2^(2n) - 2^n - 2^n + 1 = 
 2^(2n) - 2^(n+1) + 1

In your case, n=31 (your number is 2^31 - 1). The result is 2^62 + 2^32 + 1. In bits it looks like this (split by the 32bit boundary):

01000000000000000000000000000001 00000000000000000000000000000001

From this number, you get the rightmost part, which equals to 1.

gaborsch
  • 15,408
  • 6
  • 37
  • 48
0

It seems that the issue is because the int can not handle such a large value. Based on this link from oracle regarding the primitive types, the maximum range of values allowed is 2^31 -1 (2,147,483,647) which is exactly the same value that you want to multiply.

So, in this case is recommended to use the next primitive type with greater capacity, for example you could change your "int" variables to "long" which have a bigger range between -2^63 to 2^63-1 (-9223372036854775808 to 9223372036854775807).

For example:

public static void main(String[] args) {
        long num = 2147483647L;
        long total = num * num;
        System.out.println("total: " + total);
}

And the output is:

total: 4611686014132420609

I hope this can help you.

Regards.

B.Demille
  • 33
  • 6
-1

According to this link, a Java 'int' signed is 2^31 - 1. Which is equal to 2,147,483,647.

So if you are already at the max for int, and if you multiply it by anything, I would expect an error.

Alvin Bunk
  • 7,621
  • 3
  • 29
  • 45