0Pricing
Swift Academy · Lesson

Bitwise and Overflow Operators

Manipulate bits and use overflow operators like &+ safely.

Bitwise and Overflow Operators is a free Swift Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Swift Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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)

Bitwise OR

| sets each result bit to 1 when either input bit is 1. Useful to combine flags.

let flagsA: UInt8 = 0b0001
let flagsB: UInt8 = 0b0100
let combined = flagsA | flagsB
print(combined)

Bitwise XOR

^ sets a bit to 1 only when the two input bits differ. XOR with the same value twice restores the original.

let original: UInt8 = 0b1010
let key: UInt8 = 0b0110
let encoded = original ^ key
let decoded = encoded ^ key
print(encoded, decoded)

Bitwise NOT

The unary ~ inverts every bit. For a UInt8, all 8 bits flip.

let bits: UInt8 = 0b00001111
let inverted = ~bits
print(inverted)

Left Shift

<< moves bits to the left, filling with zeros. Each left shift by 1 multiplies an unsigned value by 2.

let n: UInt8 = 0b00000011
print(n << 1)
print(n << 2)

Right Shift

>> moves bits to the right. For unsigned integers it fills with zeros, effectively dividing by powers of two.

let n: UInt8 = 0b00010000
print(n >> 1)
print(n >> 4)

Overflow with Normal Operators

Standard arithmetic traps on overflow to keep your program safe: exceeding a type maximum normally crashes rather than silently wrapping.

let maxByte = UInt8.max
print("Max UInt8:", maxByte)
let safe = Int(maxByte) + 1
print("Promoted sum:", safe)

Overflow Addition &+

When you intentionally want wraparound, use the overflow operators. &+ wraps past the maximum back to the minimum instead of crashing.

let maxByte: UInt8 = 255
let wrapped = maxByte &+ 1
print(wrapped)

Overflow Subtraction and Multiplication

&- and &* wrap the same way. 0 &- 1 for a UInt8 wraps to 255.

let zero: UInt8 = 0
print(zero &- 1)
let big: UInt8 = 200
print(big &* 2)

When to Use Overflow Operators

Use &+ &- &* only when wraparound is the intended behavior, such as hashing or cyclic counters. Otherwise prefer the trapping operators for safety.

var counter: UInt8 = 254
for _ in 0..<4 {
    counter = counter &+ 1
    print(counter)
}

Quick Check

Reason about overflow wraparound.

Recap: Bitwise and Overflow Operators

You learned the bit operators & | ^ ~ and shifts << >>, that normal arithmetic traps on overflow for safety, and that &+ &- &* deliberately wrap around the type bounds.

let mask: UInt8 = 0b1111
let v: UInt8 = 0b10101010
print("Masked:", v & mask)
print("Wrap:", UInt8(0) &- 1)

Frequently asked questions

Is the “Bitwise and Overflow Operators” lesson free?

Yes — the full text of “Bitwise and Overflow Operators” is free to read here on the web, and the Swift Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Swift Academy course, upgrade to CoddyKit PRO.

What will I learn in “Bitwise and Overflow Operators”?

Manipulate bits and use overflow operators like &+ safely. You practise Swift Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Swift Academy?

No prior experience is required. Swift Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Bitwise and Overflow Operators” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Swift Academy lesson?

Yes. Every Swift Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

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