liveBook · Manning

Logical XOR

As explained in The Well-Grounded Rubyist, Fourth Edition

Bitwise XOR is a binary operation that produces 1 where exactly one corresponding input bit is 1 and produces 0 where both corresponding input bits are equal.

Logical XOR produces 1 when exactly one corresponding input bit is 1, and 0 when both bits match. For strings, XOR operates byte by byte by treating each byte as an integer. section 10.11.3

XOR on two bytes

The byte for "a" is 97, or 1100001 in binary; # is 35, or 100011. Computing 97 ^ 35 produces 66, or 1000010. Applying ^ 35 to 66 returns 97, because XOR-ing the same value twice restores the original. section 10.11.3

Listing 10.6 An exclusive-or method for strings
class String
  def ^(key)             1
    kenum = key.each_byte.cycle      2
    each_byte.map {|byte| byte ^ kenum.next }.pack("C*") 3
  end
end

A Ruby String#^ method cycles through a key's bytes, XORs them with the receiver's bytes, and packs the results back into a string. section 10.11.3

>> str = "Nice little string."
=> "Nice little string."
>> key = "secret!"
=> "secret!"
>> x = str ^ key
=> "=\f\x00\x17E\x18H\a\x11\x0F\x17E\aU\x01\f\r\x15K"
>> orig = x ^ key
=> "Nice little string."

XOR-ing "Nice little string." with "secret!" produces an obfuscated string; applying the same operation again recovers the original. section 10.11.3

How The Well-Grounded Rubyist, Fourth Edition covers it

How deep it goes
Explains bit-level behavior, a numeric calculation, reversibility, and Ruby implementation with cycling keys.
Examples
Uses 97 ^ 35, a repeating-key string obfuscation example, and a reversible Ruby String#^ method.
What you can do after
Apply via Ruby to XOR string bytes with a repeating key and recover the original by repeating the operation.
How it connects
Connects bitwise operations with integers, bytes, strings, Enumerable, and Enumerator.

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 The Well-Grounded Rubyist, Fourth Edition. The section links point to the parts of the book it draws on.