This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

why does this work (left shift operator)

 
anand raman
Ranch Hand
Posts: 66
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 54
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to know the reason too.
Who can tell me? thanks!
 
Raghav Sam
Ranch Hand
Posts: 412
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
anand raman
Ranch Hand
Posts: 66
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Raghav for the explanation.
Whats unary promotion.
-Anand
 
Seany Iris
Ranch Hand
Posts: 54
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 412
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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,
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic