1

For integer datatype 2147483647 number is withing the range.

I tried a small program as below,

int a = 2147483647;
int b = 2147483647;
int c = a + b;

When i print c it prints as -2. I changed the type of c to long and double still same result.

Why is this beahvior ?

I am using java 12.

Raghu
  • 450
  • 1
  • 5
  • 15
  • `int` + `int` gives an `int` and you assign the result to int. – Michał Krzywański May 15 '19 at 16:56
  • Result is first calculated then it is stored in `c`. Since `a` and `b` are ints result is also `int` and since integer overflows to -2 that value is placed in `c`. In other world your code is essentially `int c = -2`. For such code what chances do you expect when you declare `c` as `long` or `double`? – Pshemo May 15 '19 at 16:56
  • i assigend to long still same answer, even i explicit typecase same answer -2 – Raghu May 15 '19 at 16:56
  • change `a` or `b` to long. And also change `c` to long. – Michał Krzywański May 15 '19 at 16:59
  • @Pshemo, if you take example of int = byte+byte it works well. Why not in case of long=int+int ? – Raghu May 15 '19 at 17:01
  • 1
    Define "work well". What values ware you using with `int = byte+byte`? What result did you get? – Pshemo May 15 '19 at 17:03
  • 2
    To understand why the result is -2, you first need to know that data types like `int` are stored as binary numbers in [**two's complement**](https://en.wikipedia.org/wiki/Two%27s_complement), and then learn what that is. Read the Wikipedia article I just linked to, to learn what it is, and how it works. – Andreas May 15 '19 at 17:05
  • @michalk, if all are long the result is correct. but why not in case of int+int as the value is in range.. – Raghu May 15 '19 at 17:05
  • 1
    @Raghu Regarding `byte+byte` https://stackoverflow.com/questions/18483470/is-addition-of-byte-converts-to-int-because-of-java-language-rules-or-because-of – Pshemo May 15 '19 at 17:06
  • 2
    @Raghu *"why not in case of int+int as the value is in range"* But the value of `2147483647 + 2147483647`, which is `4294967294`, is **not** in range of an `int`, and the `int+int` operation results in an `int`, not a `long`, even if assigned to a `long`. The conversion to `long` happens *after* the addition operation. – Andreas May 15 '19 at 17:07

3 Answers3

2

Change int a , b , c to long a , b , c . Here, a and b were int and when they are added the number is int too. But it goes out of scope of int. So, change them to long to get correct answer.

Mercury1337
  • 333
  • 4
  • 13
2

Edit: live demo

Here's what's going on when you add those numbers:

int a = 2147483647;
int b = 2147483647;

In binary:

a = 0b01111111111111111111111111111111
b = 0b01111111111111111111111111111111

Adding those numbers together gives a number larger than Integer.MAX_VALUE (obviously):

long c = a + b; // -2

In binary:

  0b01111111111111111111111111111111
+ 0b01111111111111111111111111111111
------------------------------------
  0b11111111111111111111111111111110

  == -2 when taking into account 2's complement

The result is then upcast to a long.

To ensure the numbers are added as longs and not ints, make a and b both longs.

Benjamin Urquhart
  • 1,626
  • 12
  • 18
  • just an addition point to answer, java could have done typecast to long as it adds but didn't do because it follows the rule of target type like max(int, type of op1, type of op2). In the question it is max(int, int, int) so at runtime java thinks target type must be int.. – Raghu May 15 '19 at 17:40
0

The two ints are being added together as ints. The result is an int that is implicitly cast to a long or double, if c is a long or double. To avoid this, change the types of a and b to longs, so the addition is long addition.

Zach
  • 11
  • 2