2

Say I have two integers, one in decimal the other in hexadecimal:

int myInt01 = 0xc7d23020;
int myInt02 = 3352440864;

The are both the same number, but when I try to assign the decimal integer to a variable in Java, I get the error:

error: integer number too large: 3352440864

But when I assign the hexadecimal integer to a variable in Java, everything works as it should.

Why is it this way?

Brian
  • 1,726
  • 2
  • 24
  • 62

3 Answers3

5

That's because 0xc7d23020 is interpreted as -942526432 which is a valid int value, while 3352440864 is clearly a wrong int value because it's higher than the limit 2147483647;

Jts
  • 3,447
  • 1
  • 11
  • 14
2

I guess this is intended behavior: The binary representation of this number has the most significant bit set, in a 32 bit datatype. Since in Java int is a signed datatype, the most significant bit is reserved for the sign of the number. Thus, the number is not a valid positive number. This is what the compiler is telling you. However, since it is a valid negative integer, it does not complain when you assign it using the hex-representation.

You can try assigning a number that would need more than 32 bits to an integer variable using the hex-representation. This will lead to a compiler error.

Stefan Dollase
  • 4,530
  • 3
  • 27
  • 51
0

The max value that a int variable can hold is 2147483647. That's why it is giving that error. You can check that out yourself by doing this: System.out.println(Integer.MAX_VALUE);

To find out the minimum value that a int variable can hold System.out.println(Integer.MIN_VALUE);

Faraz
  • 6,025
  • 5
  • 31
  • 88
  • Yes, that's the reason! You need to use a `long` variable to hold that number, not an int. – Elias Garcia Feb 26 '16 at 02:40
  • So, why does the Hex value work but not the decimal value? – Brian Feb 26 '16 at 02:43
  • in fact the number specified is negative – Pablo Pazos Feb 26 '16 at 02:44
  • 1
    This answer almost answers the question. – Jonny Henly Feb 26 '16 at 02:44
  • @JonnyHenly Not really. Why does Java let me assign the base 16 but not the base 10? – Brian Feb 26 '16 at 02:46
  • @JonnyHenly according to this website http://www.binaryhexconverter.com/hex-to-decimal-converter 0xc7d23020 = 3352440864 – Faraz Feb 26 '16 at 02:46
  • @PabloPazos according to this website http://www.binaryhexconverter.com/hex-to-decimal-converter 0xc7d23020 = 3352440864 – Faraz Feb 26 '16 at 02:47
  • What's the two's compliment of that hex value? – Jonny Henly Feb 26 '16 at 02:48
  • @Brian I thought the question was why can't I hold that big number in an int variable. So I answered accordingly. I don't know the answer to the actual question sorry :P – Faraz Feb 26 '16 at 02:48
  • @JonnyHenly I don't remember I took those classes long time ago. But is that website wrong? – Faraz Feb 26 '16 at 02:49
  • No it's right for positive hex values, but a negative integer can be represented in bits differently. – Jonny Henly Feb 26 '16 at 02:51
  • It seems on Java 8 Integer can represent unsigned ints, but he didn't specified the java version, ref https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html IMO that number being positive or negative, depends on if it is signed or unsigned. – Pablo Pazos Feb 26 '16 at 02:52
  • The page you provided show dec to hex conversion in general, that is not how Java represents ints internally, this is a good ref: http://stackoverflow.com/questions/13422259/how-are-integers-internally-represented-at-a-bit-level-in-java – Pablo Pazos Feb 26 '16 at 02:58