The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes >> operator 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 ">> operator" Watch ">> operator" New topic

>> operator

denish mehta
Ranch Hand

Joined: Dec 21, 2000
Posts: 135
Hey ppl

In which case >>> will return a -ve value ,if ever?
regards Denish
Art Metzer
Ranch Hand

Joined: Oct 31, 2000
Posts: 241
Since >>> replaces leftmost bits with zeros, I can only conceive of a returned negative value if, in a >>> b, ( ( a < 0 ) && ( ( b % 32 ) == 0 ) ), which in effect means no shifting really takes place at all.
Ravindra Mohan
Ranch Hand

Joined: Mar 16, 2001
Posts: 216
I too agree with Art.
I was just laudly thinking why do need to shift the signed bit
by >>> when we already have a >> signed shift in java.
Ravindra Mohan.
Junilu Lacar

Joined: Feb 26, 2001
Posts: 6529

byte b = (byte)(-1 >>> x) // where (x % 32) is from 1 to 24
will result in a negative value (-1)
In general, it is possible to get a negative result when a narrowing conversion is done on the result of the >>> operation since operands will first be promoted. When the narrowing conversion is done, the sign of the result will depend on what is in the sign bit position of the shorter type: negative if 1, positive if 0.

[This message has been edited by JUNILU LACAR (edited May 03, 2001).]

Junilu - [How to Ask Questions] [How to Answer Questions]
Kenneth Ho

Joined: Mar 22, 2001
Posts: 18
As the shift rule of x%32 where x is the number of bits shift, an 32-bit integer remains no change if u shift it 32 or 64 bits. For a 64-bit long type, shifting 64 or 128 bits has no effect.
// The shift operator cant change the value of number 1
(int) 1 >> 32;
(int) 1 >> 64;
(long) 1 >> 64;
(long) 1 >> 128;

--------------------<BR>Java will never die.<P>Kenneth
denish mehta
Ranch Hand

Joined: Dec 21, 2000
Posts: 135
Thanx a lot all u guys but please tell me one more thing
When ever "x%32" for int comes in to picture or please mention some link on ineternet where i can get complete info about this particular rule.
Regards Densih
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
Hi Denish,
See JLS §15.19 Shift Operators
Hope that helps.
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform

Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
I agree. Here's the link:
subject: >> operator
It's not a secret anymore!