All original content is created in Ukrainian. Not all content has been translated yet. Some posts may only be available in Ukrainian.Learn more

What is XOR and how does it work?

Post cover: What is XOR and how does it work?
Table of contentsClick link to navigate to the desired location
This content has been automatically translated from Ukrainian.
XOR (exclusive OR) is a simple yet useful logical operation used in programming, cryptography, and data processing. It works on the principle that if two values are the same, the result will be 0, and if they are different – 1.

How does XOR look in practice?

Imagine you have two switches:
  • If both are off – the light is off.
  • If both are on – it is also off.
  • But if one is on and the other is off – the light turns on.
This is how XOR works: if the elements are different – the result is 1, if they are the same – 0.

Where is XOR used?

Encryption – if you take text and XOR it with a key, you get an encrypted version. Repeating the XOR with the same key returns the original text.
Bit manipulation – XOR helps in toggling individual bits in numbers.
Difference checking – used in algorithms for comparing two sets of data.
In simple terms, XOR is like the rule "only one of two," which helps in many information processing tasks. Let's look at a simple example written in Ruby (ruby 3.4.2)

XOR Example (Ruby)

def xor_encrypt(text, key)
  text.bytes.map.with_index { |char, i| char ^ key.bytes[i % key.size] }.pack('C*')
end

def xor_decrypt(encrypted_text, key)
  xor_encrypt(encrypted_text, key)
end

text = "Hello, XOR!"
key = "key123"

encrypted = xor_encrypt(text, key)
puts "🔒 Encrypted: #{encrypted.inspect}"

decrypted = xor_decrypt(encrypted, key)
puts "🔓 Decrypted: #{decrypted}"
In the terminal, it will print something like this:
...
🔒 Encrypted: "#\x00\x15]]\x1FK=6c\x13"
...
🔓 Decrypted: Hello, XOR!
Here, each character of the text is combined with the key using XOR.
Repeating the operation with the same key returns the original text.
This is one of the simplest encryption methods used in many algorithms.

This post doesn't have any additions from the author yet.

06 Feb 15:31

Fix error [DEPRECATION] #adapters is deprecated. Use #profiles instead. (Codecov / docile)

meme code
meme code@memecode
What is a Promise in JavaScript and how to quickly understand its essence?
18 Feb 11:01

What is a Promise in JavaScript and how to quickly understand its essence?

meme code
meme code@memecode
The structure of Promise (JavaScript) and how to work with it
18 Feb 14:33

The structure of Promise (JavaScript) and how to work with it

meme code
meme code@memecode
What is Memoization (examples in Ruby and Ruby on Rails)?
20 Feb 18:16

What is Memoization (examples in Ruby and Ruby on Rails)?

meme code
meme code@memecode
What is debounce in JavaScript and why is it important?
21 Mar 16:39

What is debounce in JavaScript and why is it important?

meme code
meme code@memecode
What is CFB (Cipher Feedback)?
21 Mar 16:53

What is CFB (Cipher Feedback)?

meme code
meme code@memecode
Embedded programming: what it is and how to get started
24 Mar 16:48

Embedded programming: what it is and how to get started

meme code
meme code@memecode
Pessimistic Lock in Rails: what it is and when to use it. What are the alternatives?
31 Mar 17:45

Pessimistic Lock in Rails: what it is and when to use it. What are the alternatives?

meme code
meme code@memecode
Why does PostgreSQL skip IDs when saving new records? (Heroku)
31 Mar 19:13

Why does PostgreSQL skip IDs when saving new records? (Heroku)

meme code
meme code@memecode
[Codecov] What is the difference between patch and project coverage?
09 Apr 16:03

[Codecov] What is the difference between patch and project coverage?

meme code
meme code@memecode
How do Scratch courses help children develop soft skills?
11 Apr 18:24

How do Scratch courses help children develop soft skills?

meme code
meme code@memecode
24 Apr 20:17

Fixing minikube "You are trying to run the amd64 binary on an M1 system."

meme code
meme code@memecode