aspose file tools*
The moose likes Mock Exam Errata and the fly likes Rules Roundup #305 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Mock Exam Errata
Bookmark "Rules Roundup #305" Watch "Rules Roundup #305" New topic
Author

Rules Roundup #305

Yuri Sforza
Greenhorn

Joined: Apr 15, 2002
Posts: 15
What does the unsigned right-shift operator
do to a negative number?
- keep it negative
- change its sign

Supposed answer is "change its sign", but what about
the following:
x >>> 0 (or 32, 64,...) ?

--
Y. Sforza
Davy Kelly
Ranch Hand

Joined: Jan 12, 2004
Posts: 384
Hey Yuri,

What does the unsigned right-shift operator
do to a negative number?
- keep it negative
- change its sign
Supposed answer is "change its sign", but what about
the following:
x >>> 0 (or 32, 64,...) ?

The unsigned rightshift operator would take a negative and make it positive.
e.g.
If you have bits like 1111 1111 and shift with >>> 4 you would get an output in bits like 0000 1111 because the unsigned rightshift moves the bits along the line by what ever many places and replaces all of the bits at the left hand side with 0's.
But the signed rightshift >> will replace the left most bits with whatever the signed bit (which is the left most bit) was so if you replace the above 1111 1111 >> 4 you get 1111 1111 because the laft most bit was a 1,
or 0111 1111 >> 4 will be 0000 0111 because left most bit was 0.
<< replaces all right bits with 0's.
I hope this helps.
Davy


How simple does it have to be???
Davy Kelly
Ranch Hand

Joined: Jan 12, 2004
Posts: 384
What would be the result?

Hint: try writing out the bits.
Davy
Yuri Sforza
Greenhorn

Joined: Apr 15, 2002
Posts: 15
Well, the topic was not about right-shift operator,
but about mock-exam error. Applying right-shift with
values like 0, 32, 64, ... will NOT change int variables,
so the imposed answer "change its sign" is not correct
as this is NOT ALWAYS the case.
--
Y. Sforza
Yuri Sforza
Greenhorn

Joined: Apr 15, 2002
Posts: 15
What would be the result?

To me more interesting would be:

What is the result (i.e. value of b after executing)?
--
Y. Sforza
Davy Kelly
Ranch Hand

Joined: Jan 12, 2004
Posts: 384
Yuri,
Please forgive me, i have never thought about trying this out, until now, I am going to have to go over my bitshift operators again to figure out why this happens.
in the above threads that i sent, this is what i was taught, and i tested them but not intensively.
sorry again.
Davy
Yuri Sforza
Greenhorn

Joined: Apr 15, 2002
Posts: 15
... have never thought about trying this out, until now, I am going to have to go over my bitshift operators again to figure out why this happens. ...

Well, sometimes not so obvious things happen "behind the scene".
As JLS (�15.19) says "... [for int type of the left-hand operand] only the five lowest-order bits of the right-hand operand are used as the shift distance... The shift distance actually used is therefore always in the range 0 to 31, inclusive."
Also you should be very carefull when using types "smaller" than int.
Example:
byte b = -128; // So bits are 10000000
byte >>>= 2; // 10000000 >>>2 -> 00100000 that is 32
Correct isn't it? No, the result is -32.
It was quite confusing to me first time (I think it would be
confusing for everyone with C/C++ background). The trick here
is byte being promoted to int.
--
Y. Sforza
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Rules Roundup #305