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.
&<< operator is the masking bitwise left shift operator in Swift. It shifts the binary representation of an integer to the left by a specified number of bits. Unlike the standard bitwise left shift operator (<<), which triggers a runtime trap on negative shift amounts and returns 0 when the shift amount is greater than or equal to the type’s bit width, &<< masks the shift amount. This guarantees a valid operation without runtime traps and causes overshifts to wrap cyclically.
Syntax
Masking Mechanics
When evaluatinglhs &<< rhs, Swift does not directly shift lhs by rhs. Instead, it calculates an effective shift amount by performing a bitwise AND operation between the shift amount (rhs) and one less than the bit width of the value being shifted (lhs.bitWidth - 1).
Because Swift’s integer types always have bit widths that are powers of two (8, 16, 32, 64), this masking operation effectively constrains the shift amount to the strict range of 0..<(lhs.bitWidth).
Formula:
Effective Shift = rhs & (lhs.bitWidth - 1)
Evaluation Examples
1. Standard Shift (Shift amount < Bit Width)
When the shift amount is positive and strictly less than the bit width,&<< behaves identically to <<.
2. Overshift (Shift amount >= Bit Width)
When the shift amount exceeds the bit width, the standard<< operator performs a “smart shift” and evaluates to 0. The &<< operator instead masks the value, effectively wrapping the shift amount.
3. Exact Bit Width Shift
Shifting by the exact bit width results in an effective shift of zero, leaving the original value unchanged.4. Negative Shift Amounts
If the right-hand side (rhs) is a signed integer and evaluates to a negative number, the standard << operator triggers a fatal error. The &<< operator prevents this by masking the two’s complement binary representation of that negative number against bitWidth - 1. It does not reverse the direction to a right shift.
Master Swift with Deep Grasping Methodology!Learn More





