| Author |
short s = 0x8000 gives compiler error
|
Mo Bustany
Greenhorn
Joined: Feb 03, 2003
Posts: 11
|
|
Page 150 of Sun Cert. by Sierra and Bates states that we get the cast from int to short for free. short s2 = 0x7fff; works but not this one: short s1 = 0x8000;
|
 |
Padmavathi Narayana
Greenhorn
Joined: Jun 16, 2003
Posts: 12
|
|
Hi Mo Bustany, I think the highest order bit is used as sign bit. So if the number is greater than 0x7fff and assigned to short if give give a loss of precision compilation error. Regards, Padma
|
 |
Darren McLeod
Greenhorn
Joined: Jul 07, 2003
Posts: 19
|
|
|
0x8000 fails because it is larger than the maximum value allowed for a short (2^15 - 1): 0x8000 = 32768 which is larger than 32767.
|
SCJP, SCBCD
|
 |
Marlene Miller
Ranch Hand
Joined: Mar 05, 2003
Posts: 1391
|
|
Don't forget that this literal 0x8000 is type int. Since it is type int, it has 32 bits. It looks like this, 0000 0000 0000 0000 1000 0000 0000 0000 Maybe this makes it more obvious that the value is decimal 32768.
|
 |
 |
|
|
subject: short s = 0x8000 gives compiler error
|
|
|