programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# value of a literal

Greenhorn
Posts: 18
Can someone clarify the value of c below?
short a = 32767;
short c = (short) (a + 2);

So, my compiler(Sun ONE) says c = -32767, how?

This is what I know:
1) There are no such things as short and byte literals.
2) short is 16 bits in size and its range is (-2^15) to (2^15) -1.
Thanks,
-Jarrod

Ranch Hand
Posts: 3244
Jarrod
Yes you can have short and byte literals. Any integral primitive can be expressed as a literal.
A shorts range is from -32768 to 32767.
In your piece of code what happened is that the initial value of a is set as 32767. In the next line you add 2 to that value (keep in mind that operations done on integral types promote the types to at least an int) so the result of a + 2 is an int with a value of 32769. If you look at this in binary form it looks like this:
00000000 00000000 10000000 00000001
When you cast it to a short it gets truncated and only the least significant (2 bytes on the right) are returned. So you get:
10000000 00000001
Which when converted back to a decimal is -32767. Hope that helps, if not post again and well clear it up for yuo.

Jarrod Legion
Greenhorn
Posts: 18
Hi Dave,
I quite didn't understand the last bit of your explanation:

10000000 00000001
Which when converted back to a decimal is -32767.

When I convert this binary -> 10000000 00000001
I get this in decimal -> (2^15) + (2^0) = 32769
How did you get -32767?
Another scenerio:
In the above example if a = 32766, then c = -32768
So, if I were to work through this with your above steps, I would get a+2 = 32768. In binary it would be:
00000000 00000000 10000000 00000000
After casting it to short I get:
10000000 00000000
Then if I were to convert this to decimal (according to you) I should get -32768, right?
But I get 32768.
Since short has a range of up to 32767, shouldn't I get a 'loss of precision' error instead on both of the examples?
Thanks for your help and Merry Christmas!!
-Jarrod

author
Sheriff
Posts: 14112
The highest order bit is the "sign bit" - if it is set, you get a negative number.

Greenhorn
Posts: 1
In java, short is represented by 16 bit 2's complement, so
10000000 00000001 -> -(2^15) + (2^0) = -32767

Jarrod Legion
Greenhorn
Posts: 18
Thanks guys.
Could you please confirm my understanding of the material?:
The highest order bit of the primitive data type represents the 'sign bit'. If it is 0, the number is always positive. If it is 1, the number is always negative.
And if those are true, Dave's explanation indeed makes perfect sense!!
-Jarrod

Dave Vick
Ranch Hand
Posts: 3244
Jarrod
That is indeed true. Glad it makes sense to you now.

Sheriff
Posts: 7023
For further reading and an easy introduction to beginning to better understand bits, take a look at The Cat and Mouse Games with Bits Story in The JavaRanch Campfire Stories.

Jarrod Legion
Greenhorn
Posts: 18
How did I ever overlook the cat and the mouse :roll:
Excellent reading for a newbie...thanks Dirk

 Why am I so drawn to cherry pie? I can't seem to stop. Save me tiny ad! Why should you try IntelliJ IDEA ? https://coderanch.com/wiki/696337/IntelliJ-IDEA