Right Shift Assignment Operator (>>=)
Right shifts the value of a variable by the number of bits specified in the value of an expression, maintains the sign, and assigns the result to the variable.
result >>= expression
- Any numeric variable.
- Any numeric expression.
Using this operator is almost the same as specifying
result = result >> expression, except that result is only evaluated once.
The >>= operator shifts the bits of result right by the number of bits specified in expression. The sign bit of result is used to fill the digits from the left. Digits shifted off to the right are discarded. The operator masks expression to avoid shifting result by too much. Otherwise, if the shift amount exceeded the number of bits in the data type of result, all the original bits would be shifted away to give a trivial result. To ensure that each shift leaves at least one of the original bits, the shift operators use the following formula to calculate the actual shift amount: mask expression (using the bitwise AND operator) with one less than the number of bits in result.
For example, after the following code is evaluated, temp has a value of -4: 14 (11110010 in binary) shifted right two bits equals -4 (11111100 in binary).
var temp temp = -14 temp >>= 2
To illustrate how the masking works, consider the following example.
var x : byte = 15; // A byte stores 8 bits. // The bits stored in x are 00001111 x >>= 10; // Actual shift is 10 & (8-1) = 2 // The bits stored in x are 00000011 // The value of x is 3 print(x); // Prints 3
Bitwise Left Shift Operator (<<) | Bitwise Right Shift Operator (>>) | Unsigned Right Shift Operator (>>>) | Assignment Operator (=) | Operator Precedence | Operator Summary | Coercion By Bitwise Operators