solo
solo

Reputation: 378

Explanation about a result

Hi i hav a little problem about some code that i can't give an explanation about the result i have.

//what happens?
public static void what() {
    int number = 2147483647;
    System.out.println(number + 33);
}

//Here is my solution for the probleme
public static void what() {
    long number = 2147483647;
    System.out.println(number + 33);
}

The first code with the int number as variable gives me -2147483616 as result. So when i change the int to long i get the good result expected. So question is who can help me give and explanation of why int number + 33 = -2147483616

Upvotes: 0

Views: 185

Answers (5)

fdfdfd
fdfdfd

Reputation: 501

The primitive type int is a 32-bit integer that can only store from -2^31 to 2^31 - 1 whereas long is a 64-bit integer so it can obviously store a much larger value.

When we calculate the capacity of int, it goes from -2147483648 to 2147483647.

Now you are wondering.. why is it that when the number exceeds the limit and I add 33 to it, it will become -2147483616?

This is because the data sort of "reset" after exceeding its limit.

Thus, 2147483647 + 1 will lead to -2147483648. From here, you can see that -2147483648 + 32 will lead to the value in your example which is -2147483616.

Some extra info below:

Unless you really need to use a number that is greater than the capacity of int, always use int as it takes up less memory space.

Also, should your number be bigger than long, consider using BigInteger.

Hope this helps!

Upvotes: 0

TilS
TilS

Reputation: 88

Java integers are based on 32 Bits. The first bit is kept for the sign (+ = 0 / - = 1).

So 2147483647 equals 01111111 11111111 11111111 11111111.

Adding more will force the value to turn to negative because the first bit is turned into a 1.

10000000 00000000 00000000 00000000 equals -2147483648.

The remaining 32 you are adding to -2147483648 brings you to your result of -2147483616.

Upvotes: 7

user555045
user555045

Reputation: 64904

Consider the calculation of the second snippet, and what the result actually means.

long number = 2147483647;
number += 33;

The result in decimal is 2147483680, in hexadecimal (which more easily shows what the value means) it is 0x80000020.

For the first snippet, the result in hexacimal is also 0x80000020, because the result of arithmetic with the int type is the low 32 bits of the "full" result. What's different is the interpretation: as an int, 0x80000020 has the top bit set, and the top bit has a "weight" of -231, so this result is interpreted as -231 + 32 (a negative number). As a long, the 32nd bit is just a normal bit with a weight of 231 and the result is interpreted as 231 + 32.

Upvotes: 0

tobsob
tobsob

Reputation: 602

You have reached the maximum of the primitive type int (2147483647). If int overflows, it goes back to the minimum value (-2147483648) and continues from there.

Upvotes: 0

mwarren
mwarren

Reputation: 759

The primitive int type has a maximum value of 2147483647, which is what you are setting number to. When anything is added to this value the int type cannot represent it correctly and 'wraps' around, becoming a negative number.

The maximum value of the long type is 9223372036854775807 so the second code snippet works fine because long can hold that value with no problem.

Upvotes: 0

Related Questions