GeeCON Prague 2014*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Reduction of the right operand Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Reduction of the right operand" Watch "Reduction of the right operand" New topic
Author

Reduction of the right operand

Xu Song
Greenhorn

Joined: Nov 16, 2001
Posts: 20
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

Joined: Nov 27, 2001
Posts: 1561
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;


I'm not going to be a Rock Star. I'm going to be a LEGEND! --Freddie Mercury
Alton Hernandez
Ranch Hand

Joined: May 30, 2003
Posts: 443
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

Joined: Aug 26, 2001
Posts: 7610
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!


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Xu Song
Greenhorn

Joined: Nov 16, 2001
Posts: 20
Hi,
Thanks a lot for your explanation.
Actually it's my first post,and I get the response so quickly.
xusoo
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Reduction of the right operand