I've been struggling with the shift operator in PHP, assuming that it precedes arithmetic operations like +, 1 etc. I've been unable to find any definition of this in the php manual.
Let's say that I have the value 1, which I want to 3, then to 7 etc., filling bits from the right (LSB).
I tried:
$X = 1;
Then, in a loop:
$X <<= 1 + 1; // returns 4 instead of 3.
$X <<= 1 + 1; // returns 16 instead of 7.
So evidently 1+1 is calculated BEFORE the shift. My solution:
$X = 1;
$X <<= 1; // $X=2
$X += 1; // $X=3 as expected
$X <<= 1; // $X=6
$X += 1; // $X=7 as expected
Which is a bit more clumsy, but this returns the right numbers. Shouldn't the first method produce the correct result, shifting first and THEN do the arithmetic?