Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!

# >> operator

denish mehta
Ranch Hand
Posts: 135
Hey ppl

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

Art Metzer
Ranch Hand
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.
Art

Ravindra Mohan
Ranch Hand
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.
Rgds,
Ravindra Mohan.

Junilu Lacar
Bartender
Posts: 7569
52
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.
Junilu

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

Kenneth Ho
Greenhorn
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;

denish mehta
Ranch Hand
Posts: 135
Thanx a lot all u guys but please tell me one more thing
Regards Densih

Jane Griscti
Ranch Hand
Posts: 3141
Hi Denish,
See JLS §15.19 Shift Operators
Hope that helps.
------------------
Jane Griscti
Sun Certified Programmer for the Javaï¿½ 2 Platform