This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes why does this work (left shift operator) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "why does this work (left shift operator)" Watch "why does this work (left shift operator)" New topic
Author

why does this work (left shift operator)

anand raman
Ranch Hand

Joined: Jun 06, 2001
Posts: 66
hi guys
why should this thing work

I thought this would not compile, but it does.
My thoughts on the path of execution are
1. long l cause the int i to be implicitly be a long.
2. After this implicit conversion the left shift happens and the result is a long.
How can it be cast to a int again..
Can some one please correct me.
Thanks for ur time.
-Anand
Seany Iris
Ranch Hand

Joined: Jan 08, 2002
Posts: 54
I want to know the reason too.
Who can tell me? thanks!


help you means help me
Raghav Sam
Ranch Hand

Joined: Apr 12, 2001
Posts: 412
If you read the JLS (15.19), it says that for shift operators, only unary promotion is done. And that too 'only the five lowest-order bits of the right-hand operand are used as the shift distance, if the promoted type of the left-hand operand is int'. So,
in ur code,

only the 5 lower order bits of 99, i.e., 00011 or the whole thing boils down to:
9 << 3 which is 72. So,


The spec also says: 'The type of the shift expression is the promoted type of the left-hand operand.' which is int in our case.
HTH,
[ January 08, 2002: Message edited by: Raghav Sam ]

Be who you are and say what you feel, because those who mind don't matter and those who matter don't mind.<br />- Dr. Seuss
anand raman
Ranch Hand

Joined: Jun 06, 2001
Posts: 66
Thanks Raghav for the explanation.
Whats unary promotion.
-Anand
Seany Iris
Ranch Hand

Joined: Jan 08, 2002
Posts: 54
yes, I understand, thanks
I just make mistake about the question.
I think the question is:
int i = 9;long l = 99;int k = 0;k = l << i;
If it was this, it will not be compiled.
As what Raghav Sam said 'only the five lowest-order bits of the right-hand operand are used as the shift distance, if the promoted type of the left-hand operand is int.In other word,only the the result of the right-hand operand MOD 32(the length of int).
I don't know whether you can understand me. Hope it will help you.
Raghav Sam
Ranch Hand

Joined: Apr 12, 2001
Posts: 412
Unary promoion is done with some operators like +, -, <<, >>, >>> etc. The promotion is done on only one operand. And only byte, char & short are promoted to int. No other promotion is permitted. For eg., in this example,
i << l,
though i is int and l is long, i is not converted to long from int. If i was byte, then it will be promoted to int first. But still the result will be the same.
HTH,
 
 
subject: why does this work (left shift operator)
 
Similar Threads
shift operators and hex...HELP!!!!!
Conversion Long to byte[]
Shift Issue
MockExam2 Question
why does this work (left shift operator)