TheDocumentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
>>= (bitwise right shift assignment) operator shifts the binary representation of a mutable integer variable to the right by a specified number of bits and assigns the resulting value back to that same variable. It is a compound assignment operator that combines the bitwise right shift (>>) and assignment (=) operations into a single expression.
Syntax
lhs(Left-Hand Side): A mutable variable (var) of an integer type conforming to theBinaryIntegerprotocol.rhs(Right-Hand Side): The scalar number of bit positions to shiftlhsto the right. Must also conform toBinaryInteger.
a >>= b is semantically identical to a = a >> b.
Bitwise Mechanics
The behavior of the>>= operator depends strictly on whether the underlying integer type of lhs is unsigned or signed. Bits that are shifted past the rightmost boundary (the least significant bit) are permanently discarded.
1. Unsigned Integers (Logical Right Shift)
When applied to an unsigned integer (e.g.,UInt8, UInt32), >>= performs a logical right shift. As bits are shifted to the right, the newly vacated bit positions on the left (the most significant bits) are padded with zeros.
2. Signed Integers (Arithmetic Right Shift)
When applied to a signed integer (e.g.,Int8, Int), >>= performs an arithmetic right shift. To preserve the integer’s sign in Two’s Complement representation, the newly vacated bit positions on the left are padded with the original sign bit (the most significant bit prior to the shift).
- If the number is positive (sign bit is
0), it pads with0s. - If the number is negative (sign bit is
1), it pads with1s.
Type Constraints and Safety
- Type Matching: Both
lhsandrhsmust conform to theBinaryIntegerprotocol. They do not need to be the exact same type (e.g., shifting anInt8by anIntis valid). - Negative Shift Amounts (Runtime Trap): If
rhsis a signed integer type and evaluates to a negative value, Swift will trigger a runtime crash (fatal error). The shift amount must always be greater than or equal to zero. - Overshifting: If
rhsis greater than or equal to the bit width oflhs(e.g., shifting an 8-bit integer by 10), Swift does not invoke undefined behavior. Instead, it shifts all original bits out of bounds. For unsigned or positive signed integers, the result becomes0. For negative signed integers, the result becomes-1(all bits set to1).
Master Swift with Deep Grasping Methodology!Learn More





