• 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

<<< (large value)

 
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

what is the result of k after the following statement?

the answer is 20 , explanation says its because 32002 is converted to "atleast int" and it'd look like (int k=5<<(32002%32)) to jvm..
my question is :
isn't 32002 in range of int? why should it convert?
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

isn't 32002 in range of int? why should it convert?



That is simply how it is defined -- for shifting of integers only the low 5 bits of the right operand are valid. If you want to do 32002 left bit shifts, you can do this instead...



Henry
 
Shrinath M Aithal
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:

isn't 32002 in range of int? why should it convert?



That is simply how it is defined -- for shifting of integers only the low 5 bits of the right operand are valid. If you want to do 32002 left bit shifts, you can do this instead...



Henry



I didn't mean it like that .. I said, the answer is 20, but the explanation given for that was like it is converted to "atleast int".. so according to explanation, 32002 is not in int range, and wants to convert to int.. doesn't that sound erratic? or am I reading the wrong way ?
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You may be taking it out of context, and since we don't have the full context.... I am assuming that the text is trying to say that an int only has 32 bits, so shifting 32 or more times, will simply assign it to zero. So, for ints, the range of allowable shifts is from 0 to 31.

Henry
 
Shrinath M Aithal
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok.. right since it is supposed to shift only for less than "allowable int range" of 32, it does 32002%32 and shifts twice..
But the explanation given by the http://www.santis.ch/training/java/jcp/selftester didn't convince me.. now I understood..

thank you
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Shrinath M Aithal wrote:But the explanation given by the http://www.santis.ch/training/java/jcp/selftester didn't convince me.. now I understood..



Thanks for quoting your source -- but next time, can you quote it first.

http://faq.javaranch.com/java/QuoteYourSources

Henry
 
Shrinath M Aithal
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:

Shrinath M Aithal wrote:But the explanation given by the http://www.santis.ch/training/java/jcp/selftester didn't convince me.. now I understood..



Thanks for quoting your source -- but next time, can you quote it first.

http://faq.javaranch.com/java/QuoteYourSources

Henry



sorry I almost forgot i hadn't mentioned the source.. when i was typing last post, i remembered and put it there :-)
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic