0Pricing
Swift Academy · Lesson

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

  1. Arithmetic and Compound Assignment Operators
  2. Comparison and Identity Operators
  3. Logical Operators and Short-Circuiting
  4. Bitwise and Overflow Operators
← Back to Swift Academy