• Post Reply Bookmark Topic Watch Topic
  • New Topic

<<< (large value)  RSS feed

 
Shrinath M Aithal
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?
 
Henry Wong
author
Sheriff
Posts: 23292
125
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: 23292
125
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: 23292
125
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 :-)
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!