aspose file tools*
The moose likes Beginning Java and the fly likes value of a literal Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "value of a literal" Watch "value of a literal" New topic
Author

value of a literal

Jarrod Legion
Greenhorn

Joined: Dec 24, 2002
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
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
Jarrod
to answer your questions then to show you how the answer you got is correct:
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.


Dave
Jarrod Legion
Greenhorn

Joined: Dec 24, 2002
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
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
The highest order bit is the "sign bit" - if it is set, you get a negative number.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
ezez Lin
Greenhorn

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

Joined: Dec 24, 2002
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

Joined: May 10, 2001
Posts: 3244
Jarrod
That is indeed true. Glad it makes sense to you now.
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
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.


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Jarrod Legion
Greenhorn

Joined: Dec 24, 2002
Posts: 18
How did I ever overlook the cat and the mouse :roll:
Excellent reading for a newbie...thanks Dirk
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: value of a literal