- General Development
- Schema Development
- Apex Code Development
- Visualforce Development
- Formulas & Validation Rules
- Force.com Sites & Site.com
- Chatter Development
- Java Development
- .NET Development
- Perl, PHP, Python & Ruby
- Desktop Integration
- APIs and Integrations
- Visual Workflow
- Apple, Mac and OS X
- VB and Office Development
- AppExchange Directory & Packaging
- Salesforce Labs & Open Source Projects
- Other Salesforce Applications
- Jobs Board
01-28-2013 05:42 PM
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
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?
01-28-2013 07:54 PM
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.