File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Reduction of the right operand

 
Xu Song
Greenhorn
Posts: 20
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Andres Gonzalez
Ranch Hand
Posts: 1561
  • 0
  • Mark post as helpful
  • send pies
  • 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;
 
Alton Hernandez
Ranch Hand
Posts: 443
  • 0
  • Mark post as helpful
  • send pies
  • 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 ]
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic