liveBook · Manning

Logical XOR

As explained in Classic Computer Science Problems in Swift: Essential techniques for practicing programmers

The exclusive-or operation that returns true for exactly one true operand and false when both operands have the same truth value.

Logical XOR, or exclusive OR, is true when exactly one operand is true and false when both operands share the same truth value. At the bit level, it produces 1 for 0 ^ 1 and 1 ^ 0, but 0 for 0 ^ 0 and 1 ^ 1. section 1.3

XOR becomes useful in byte-oriented encryption because its reversible combinations preserve each operand:

  • A ^ B = C; recombining C with B recovers A, while recombining C with A recovers B. section 1.3
  • Swift uses ^ for XOR, including element-by-element operations on UInt8 values. section 1.3

One-time pad

encryptOTP() XORs each byte of a String with a randomly generated dummy key and returns the dummy data plus the encrypted product. decryptOTP() XORs those two keys again and converts the resulting bytes back to a String. section 1.3

How Classic Computer Science Problems in Swift: Essential techniques for practicing programmers covers it

How deep it goes
Defines Boolean and bitwise behavior, demonstrates reversibility, and applies it in Swift encryption functions.
Examples
Demonstrates random OTP keys, encryptOTP(), decryptOTP(), and encrypting “¡Vamos Swift!” as byte sequences.
What it assumes
Assumes familiarity with Swift values, arrays, strings, bytes, and indexing while explaining XOR’s truth-table behavior.
What you can do after
Apply XOR through Swift’s ^ operator to combine and reverse byte sequences in a one-time pad implementation.
How it connects
Connects XOR with bitwise operations, UInt8 values, String encoding, and one-time pad encryption.

Also explained in

Compare how 3 books explain logical XOR →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Classic Computer Science Problems in Swift: Essential techniques for practicing programmers. The section links point to the parts of the book it draws on.