• Post Reply Bookmark Topic Watch Topic
  • New Topic
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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

why does this work (left shift operator)

 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I want to know the reason too.
Who can tell me? thanks!
 
Ranch Hand
Posts: 412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Raghav for the explanation.
Whats unary promotion.
-Anand
 
Seany Iris
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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,
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic