Bitwise and Overflow Operators
Manipulate bits and use overflow operators like &+ safely.
Bitwise Operators
Bitwise operators work on the individual binary digits of integers. Swift offers AND &, OR |, XOR ^, NOT ~, and shifts << / >>.
let a: UInt8 = 0b1100
let b: UInt8 = 0b1010
print(a & b)
print(a | b)Bitwise AND
& sets each result bit to 1 only when both input bits are 1. It is often used to mask out bits.
let value: UInt8 = 0b10110101
let mask: UInt8 = 0b00001111
let low = value & mask
print(low)All lessons in this course
- Arithmetic and Compound Assignment Operators
- Comparison and Identity Operators
- Logical Operators and Short-Circuiting
- Bitwise and Overflow Operators