You are viewing a free preview of this lesson.
Subscribe to unlock all 10 lessons in this course and every other course on LearningBro.
The XOR gate (pronounced "exclusive OR") is an important logic gate that appears frequently in GCSE Computer Science exams. It is similar to the OR gate but with one key difference — the output is 1 only when the inputs are different.
XOR stands for Exclusive OR. For a two-input XOR gate:
The crucial difference between OR and XOR is what happens when both inputs are 1:
| Input A | Input B | Output (A ⊕ B) |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 0 |
The block view of the gate:
graph LR
A((A)) --> XOR["XOR"]
B((B)) --> XOR
XOR --> Q((Q = A XOR B))
Compare this with the OR truth table — they are identical except for the last row. In OR, 1 and 1 gives 1. In XOR, 1 and 1 gives 0.
Exam Tip: The easiest way to remember XOR is: "the output is 1 when the inputs are DIFFERENT." If both inputs are the same (both 0 or both 1), the output is 0.
The XOR gate symbol looks like the OR gate but with an extra curved line on the left side of the gate:
+------\
A ---|| \
|| XOR )---- Output (A ⊕ B)
B ---|| /
+------/
The double curved line on the left distinguishes XOR from OR. Some exam boards show this as an OR gate with an additional arc across the inputs.
The Boolean expression for XOR is written as:
XOR can also be expressed using the fundamental gates (AND, OR, NOT):
A ⊕ B = (A ∨ B) ∧ ¬(A ∧ B)
In words: "A OR B, AND NOT (A AND B)." This means: at least one input must be true, but NOT both at the same time.
An equivalent expression is:
A ⊕ B = (A ∧ ¬B) ∨ (¬A ∧ B)
In words: "A AND NOT B, OR NOT A AND B." This means: exactly one input must be true.
| Input A | Input B | OR (A ∨ B) | XOR (A ⊕ B) |
|---|---|---|---|
| 0 | 0 | 0 | 0 |
| 0 | 1 | 1 | 1 |
| 1 | 0 | 1 | 1 |
| 1 | 1 | 1 | 0 |
The only difference is in the last row. OR includes the case where both inputs are true; XOR excludes it.
A staircase often has a light switch at the top AND at the bottom. The light changes state whenever either switch is flipped. This is XOR behaviour:
| Switch 1 | Switch 2 | Light |
|---|---|---|
| Down (0) | Down (0) | OFF (0) |
| Down (0) | Up (1) | ON (1) |
| Up (1) | Down (0) | ON (1) |
| Up (1) | Up (1) | OFF (0) |
"You can have cake OR ice cream, but not both." This is an exclusive OR — you must choose one or the other, not both together.
XOR has several important uses in computing:
The sum bit of binary addition follows the XOR pattern:
| A | B | Sum (A ⊕ B) | Carry (A ∧ B) |
|---|---|---|---|
| 0 | 0 | 0 | 0 |
| 0 | 1 | 1 | 0 |
| 1 | 0 | 1 | 0 |
| 1 | 1 | 0 | 1 |
A half adder circuit uses an XOR gate for the sum and an AND gate for the carry.
XOR is widely used in encryption because it is easily reversible:
A ⊕ K = C (encrypt) C ⊕ K = A (decrypt)
XOR is used in parity checks — a simple form of error detection where you XOR all the bits together to get a parity bit.
Most programming languages do not have a dedicated XOR keyword, but it can be achieved:
Python:
a = True
b = False
result = a ^ b # True (inputs are different)
JavaScript:
let a = true;
let b = false;
let result = a ^ b; // 1 (true, inputs are different)
In both languages, the ^ operator performs XOR on Boolean values or integers.
Subscribe to continue reading
Get full access to this lesson and all 10 lessons in this course.