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:
  • 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
 
anand raman
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
sorry for the spam.. never realized that the dumb browser was submitting it multiple times.
-Anand
 
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
For information on shifting, I recommend you read Cat and Mouse Game with Bits
Actually this question highlights the one understatement of the story.
In Java several steps happen in shifting.
1) If the left hand operand is smaller than an int, it is cast to an int.
2) If the left hand operand is an int, the right hand operand is then anded (&) with 0x1f or 31
in your example, 99 is anded with 31
99 - 00000000 00000000 00000000 01100011
31 - 00000000 00000000 00000000 00011111
Result - 00000000 00000000 00000000 00000011 - 3
so, 9 is shifted by 3
3) If the left hand operand is a long, the right hand operand is & with 0x3f or 63
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Please do not post twice the same topic.
You may continue this discussion at http://www.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=get_topic&f=24&t=013965.
I'm closing this thread.
 
Consider Paul's rocket mass heater.
    Bookmark Topic Watch Topic
  • New Topic