Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

The >>= (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 >>= rhs
  • lhs (Left-Hand Side): A mutable variable (var) of an integer type conforming to the BinaryInteger protocol.
  • rhs (Right-Hand Side): The scalar number of bit positions to shift lhs to the right. Must also conform to BinaryInteger.
The expression 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.
var unsignedByte: UInt8 = 0b11110000 // Decimal: 240
unsignedByte >>= 2

// Execution:
// 1. Shift right 2:  --111100
// 2. Pad with zeros: 00111100
// unsignedByte is now 0b00111100 (Decimal: 60)

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 with 0s.
  • If the number is negative (sign bit is 1), it pads with 1s.
// Positive Signed Integer
var positiveByte: Int8 = 0b01110000 // Decimal: 112 (Sign bit is 0)
positiveByte >>= 2

// Execution:
// 1. Shift right 2:  --011100
// 2. Pad with sign bit (0): 00011100
// positiveByte is now 0b00011100 (Decimal: 28)

// Negative Signed Integer
var negativeByte: Int8 = Int8(bitPattern: 0b11110000) // Decimal: -16 (Sign bit is 1)
negativeByte >>= 2

// Execution:
// 1. Shift right 2:  --111100
// 2. Pad with sign bit (1): 11111100
// negativeByte is now 0b11111100 (Decimal: -4)

Type Constraints and Safety

  • Type Matching: Both lhs and rhs must conform to the BinaryInteger protocol. They do not need to be the exact same type (e.g., shifting an Int8 by an Int is valid).
  • Negative Shift Amounts (Runtime Trap): If rhs is 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 rhs is greater than or equal to the bit width of lhs (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 becomes 0. For negative signed integers, the result becomes -1 (all bits set to 1).
Master Swift with Deep Grasping Methodology!Learn More