Win a copy of The Journey To Enterprise Agility this week in the Agile and Other Processes forum! And see the welcome thread for 20% off.
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

<<< (large value)  RSS feed

 
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Posts: 23568
138
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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
Sheriff
Posts: 23568
138
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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
Sheriff
Posts: 23568
138
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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 :-)
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!