• 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

Reduction of the right operand

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
As I know,if the shift is being done as an int type,then the right operand should be less than 32.For instance,
int ival=0x70;
ival=ival>>33;
-----
result:ival is 0x38,since the behavior equals to ival>>(33%32).
Question:
byte bval=0x70;
bval=(byte)(bval>>9);
-----
Why the result is 0x00,not 0x38?
Thanks.
xusoo
 
Ranch Hand
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Isn't because a byte is 8 bits and you're shifting 9?
As I know,if the shift is being done as an int type,then the right operand should be less than 32
Not necessarily, you yourself put an example:
ival=ival>>33;
 
Ranch Hand
Posts: 443
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by xusoo:

Question:
byte bval=0x70;
bval=(byte)(bval>>9);
-----
Why the result is 0x00,not 0x38?
Thanks.
xusoo


I believe you are expecting 9%8 as the value of the right operand in this equation.
Integral types byte,short,& char are all promoted to int before any operations are performed (that is why you put an explicit cast to byte there). So the maximum value of the right operand in a shift operations is the same as int, i.e. 31.
Hope this helps.
[ August 25, 2003: Message edited by: Alton Hernandez ]
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
xusoo,
Welcome to Javaranch, a friendly place for Java greenhorns
We ain't got many rules 'round these parts, but we do got one. Please change your displayed name to comply with the JavaRanch Naming Policy.
Thanks Pardner! Hope to see you 'round the Ranch!
 
Xu Song
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Thanks a lot for your explanation.
Actually it's my first post,and I get the response so quickly.
xusoo
 
reply
    Bookmark Topic Watch Topic
  • New Topic