Reply
Regular Contributor
kingkoo
Posts: 89
0

Clarification required for Bitwise shift right signed operator

Hi guys

 

Reading the Apex Developer's Guide p65 (Winter 13) and would like to see if someone can explain something to me regarding the Bitwise shift right signed operator.

 

This is what it says:

x >> y

Shifts each bit in x to the right by y bits so that the low order bits are lost, and the new left bits are set to 0 for positive values of y and 1 for negative values of y.

 

So I was trying to figure out what is 50 >> -2

 

50 in binary is 00110010.

 

because y is negative, I assume what it means it shifts each bit in 00110010 to the right by 2 bits and the new left bits are set to 1 because y is negative.

 

I thought that would mean 

00110010 ->

10011001 ->

11001100

 

And that would be something other than 0.

 

However, when I tried to use Eclipse to do this

system.debug(50 >> -2);

 

the result is 0.

 

In fact, no matter what x and y are, as long as y is negative, the result seems to always be zero.

 

Am I not understanding something properly?

 

Thanks

King

 

Super Contributor
sfdcfox
Posts: 4,044
0

Re: Clarification required for Bitwise shift right signed operator

The docs are updated to API 27.0. It is likely that <=26.0, negative shifts are automatically zeroed out. I don't have an answer for this immediately, because I do not have a Spring 13 sandbox.

~ sfdcfox ~


I am a sandwich. That is all.