aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes question to Bates on Shift Operators Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "question to Bates on Shift Operators" Watch "question to Bates on Shift Operators" New topic
Author

question to Bates on Shift Operators

Mohit Goyal
Ranch Hand

Joined: Nov 09, 2003
Posts: 65
In the book it is given that the >>> shift operator always results in a +ve number but when i run the following code
class A {
public static void main (String[] args) {
byte x=-128;
System.out.println(x);
x>>>=4;
System.out.println(x);
}
}
output :
-128
-8
Well as u see the result is negative .
cud u explain it
and by the way the link to Bonus Master Exam on the CDrom accompanying the book is not working
I even went to the learnkey site and even Onlineexpert.com but didn't find the exam anywhere.
Can u help me downloading it as i have very few days left in my exam
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
Do the bit manipulation. The byte is promoted to an int. Then it is shifted. Then it is chopped back to a byte. Are you sure that K&B said what you think it said?
1111 1111 1111 1111 1111 1111 1000 0000 = -128 upgraded to an int
0000 1111 1111 1111 1111 1111 1111 1000 = >>>4
1111 1000 = back to a byte -8


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Vad Fogel
Ranch Hand

Joined: Aug 25, 2003
Posts: 504
Hi Mohit, I'm no Bert, you see , but can try to clear some of your doubts while Bert might be busy.

You have a byte variable x here which is represented as 1000 0000 in binary. Here's what happens:
>>>= assignment operator first promotes x to become integer (widening conversion), and no data are lost. And that means that the temporary value of x is:
1111 1111 1111 1111 1111 1111 1000 0000
Still negative and still equal to -128.
Then, 4 bits are shifted to the rigth like this:
0000 1111 1111 1111 1111 1111 1111 1000
And this is still temporary value of x!
The compound assignment operator >>>= (or any other compound one) makes sure that x is converted back to byte. And that means that all bits to the left of the first 8 bits (for byte) get chopped off like this:
1111 1000
You see, the sign is still negative, and this is your -8 in byte representation. You may wanna read more from K&B on compound assignments and shifting, also take Dan's exams, they are perfect!
With regards to K&B MasterExam, I had to insert the book's CD and register online. Then, it was pretty easy to download and install the exam.
I hope this helps.
Harwinder Bhatia
Ranch Hand

Joined: Oct 17, 2003
Posts: 150
Mohit
I don't have the book but I can explain this problem:

Hope that helps.
Cheers
Harwinder
Vad Fogel
Ranch Hand

Joined: Aug 25, 2003
Posts: 504
K&B, p.171:

One important implication of using >>> vs. >> is that,
except for a few special cases that we´┐Żll discuss next, the result of an unsigned right shift
is always positive, regardless of the original sign bit.
You also need to know that all operands in a bit shift are promoted to at least an int.
Harwinder Bhatia
Ranch Hand

Joined: Oct 17, 2003
Posts: 150
I guess all 3 us (Thomas, Vad and I) were typing the response at the same time .. the first 2 explained it well enough
Didn't mean to bog you down with too much of information
Cheers
Harwinder
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
...except for a few special cases. I think that says it all. This is obviously one of those few special cases.
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8829
    
    5
Man, ya gotta get up pretty early to answer a bit shifting question aroud here
Having looked at the book, I think we have ourselves an "enhancement". We'll make future printings of the book a little more specific on the details of shifting primitive types shorter than ints.
But here's a pop-quiz:
If the operands are already ints or longs, then in what "special cases" will the >>> operator return a negative value?


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
I know! But I'll let someone studying for the exam answer.
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
clue: try shifting an int by 32 or a long by 64


SCJP2. Please Indent your code using UBB Code
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8829
    
    5
Hey Jose,
that's a pretty generous clue :roll:
Mohit Goyal
Ranch Hand

Joined: Nov 09, 2003
Posts: 65
Hi well i know the ans to that myself
if u shift an int by more than 31 or a long by more than 63 then the no of bits by which the original number will shift is (right oper)%32 fir int and (right oper)%64 for long.
so if we shift an int by 32 using >>> then the num will remain -ve
 
 
subject: question to Bates on Shift Operators