Win a copy of Spring in Action (5th edition) this week in the Spring forum!
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:
• Campbell Ritchie
• Bear Bibeault
• Devaka Cooray
• Liutauras Vilda
• Jeanne Boyarsky
Sheriffs:
• Knute Snortum
• Junilu Lacar
• paul wheaton
Saloon Keepers:
• Ganesh Patekar
• Frits Walraven
• Tim Moores
• Ron McLeod
• Carey Brown
Bartenders:
• Stephan van Hulst
• salvin francis
• Tim Holloway

# <<< (large value)

Ranch Hand
Posts: 82

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: 23811
140

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

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: 23811
140
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
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: 23811
140

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

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 :-)

 Did you see how Paul cut 87% off of his electric heat bill with 82 watts of micro heaters?