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 bitwise OR assignment operator in Swift. It is a compound assignment operator that performs a bitwise OR operation (|) between two integer operands and assigns the resulting value back to the left-hand operand.
Mechanics
The operator evaluates the binary representations of both operands. It compares the bits at each corresponding position and applies the following logic:- If the bit in either the left-hand operand OR the right-hand operand is
1, the resulting bit is set to1. - If the bits in both operands are
0, the resulting bit is set to0.
lhs) must be a mutable variable (var), while the right-hand side (rhs) can be a variable, constant, or literal.
Syntax and Evaluation
Type Constraints and Semantics
- Protocol Conformance: Both operands must conform to the
BinaryIntegerprotocol. This includes standard library integers (e.g.,Int,UInt8,Int64) as well as custom arbitrary-precision integers. The operator cannot be applied to floating-point types. - Strict Type Matching: Swift enforces strict type safety. The
lhsandrhsmust be of the exact same integer type. Swift will not implicitly cast aUInt8to anIntto perform the operation; explicit initialization is required if the types differ. - Evaluation Semantics: The expression
a |= bproduces the same logical result asa = a | b, but with a critical distinction in evaluation: the compound assignment operator evaluates the left-hand side expression only once. This is highly relevant when the left-hand side is a computed property, a function returning aninoutreference, or an array subscript. - Thread Safety: The
|=operator performs a non-atomic read-modify-write sequence. It is not thread-safe and should not be treated as an atomic operation in concurrent contexts.
Master Swift with Deep Grasping Methodology!Learn More





