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 (exclusive OR) takes two inputs and produces an output of 1 only when the inputs are different. XOR is a crucial gate in computer science, used extensively in encryption and arithmetic circuits. This is covered in OCR J277 Section 2.5.
XOR stands for exclusive OR. Unlike the standard OR gate (which outputs 1 when at least one input is 1), XOR outputs 1 only when exactly one input is 1 — that is, when the inputs are different from each other.
In Boolean algebra, XOR is written as:
The word "exclusive" means it excludes the case where both inputs are 1.
| A | B | A XOR B |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 0 |
Compare this with the OR truth table — the only difference is the last row. When both inputs are 1, OR outputs 1 but XOR outputs 0.
| A | B | OR | XOR |
|---|---|---|---|
| 0 | 0 | 0 | 0 |
| 0 | 1 | 1 | 1 |
| 1 | 0 | 1 | 1 |
| 1 | 1 | 1 | 0 |
OCR Exam Tip: The key difference between OR and XOR is the last row. OR gives 1 when both inputs are 1; XOR gives 0. Think of XOR as "one or the other, but not both."
The XOR gate symbol looks like the OR gate but with an extra curved line on the input side:
flowchart LR
A((A)) --> XOR["XOR gate"]
B((B)) --> XOR
XOR --> Q((Q))
The double curved line on the left distinguishes XOR from OR. In the exam, you must draw this extra line to get the mark.
Python does not have a dedicated xor keyword for Boolean values, but you can use the != operator or the bitwise ^ operator:
# XOR using != (not equal)
a = True
b = False
result = a != b # True (because they are different)
print(result)
# XOR using bitwise ^ operator
x = 1
y = 0
print(x ^ y) # 1
In OCR pseudocode, XOR may be expressed as:
a = true
b = false
if a != b then
print("Inputs are different")
endif
Alternatively, XOR can be built from other operators:
// A XOR B = (A AND NOT B) OR (NOT A AND B)
result = (a AND NOT b) OR (NOT a AND b)
XOR is one of the most useful gates in computing:
| Application | How XOR is used |
|---|---|
| Half adder | XOR calculates the sum bit when adding two binary digits |
| Encryption | XOR is used in many encryption algorithms (e.g., one-time pad, stream ciphers) |
| Parity checking | XOR of all bits gives the parity bit for error detection |
| Comparison | XOR outputs 1 when two bits differ, useful for detecting changes |
| Toggle operations | XORing a value with 1 flips it; XORing with 0 keeps it the same |
When you add two single binary digits, the sum bit is calculated using XOR:
| A | B | Sum (A XOR B) | Carry (A AND B) |
|---|---|---|---|
| 0 | 0 | 0 | 0 |
| 0 | 1 | 1 | 0 |
| 1 | 0 | 1 | 0 |
| 1 | 1 | 0 | 1 |
The XOR gate gives the sum and the AND gate gives the carry. Together they form a half adder circuit.
A simple encryption technique uses XOR:
| Step | Bit 1 | Bit 2 | Bit 3 | Bit 4 | Bit 5 | Bit 6 | Bit 7 | Bit 8 |
|---|---|---|---|---|---|---|---|---|
| Plaintext | 1 | 0 | 1 | 1 | 0 | 1 | 0 | 0 |
| Key | 0 | 1 | 1 | 0 | 1 | 1 | 0 | 1 |
| Encrypted (plaintext XOR key) | 1 | 1 | 0 | 1 | 1 | 0 | 0 | 1 |
To decrypt, XOR the encrypted data with the same key again:
| Step | Bit 1 | Bit 2 | Bit 3 | Bit 4 | Bit 5 | Bit 6 | Bit 7 | Bit 8 |
|---|---|---|---|---|---|---|---|---|
| Encrypted | 1 | 1 | 0 | 1 | 1 | 0 | 0 | 1 |
| Key | 0 | 1 | 1 | 0 | 1 | 1 | 0 | 1 |
| Decrypted (original plaintext restored) | 1 | 0 | 1 | 1 | 0 | 1 | 0 | 0 |
This works because XOR is its own inverse: (A XOR B) XOR B = A.
OCR Exam Tip: The self-inverse property of XOR makes it extremely useful in cryptography. If asked why XOR is used in encryption, explain that applying XOR with the same key twice restores the original data.
| Operation | Bit 1 | Bit 2 | Bit 3 | Bit 4 |
|---|---|---|---|---|
| 1 | 0 | 1 | 1 | |
| XOR | 1 | 1 | 0 | 1 |
| Result | 0 | 1 | 1 | 0 |
Each column: 1 XOR 1 = 0, 0 XOR 1 = 1, 1 XOR 0 = 1, 1 XOR 1 = 0.
OCR GCSE Computer Science (J277) only examines AND, OR, and NOT. XOR is covered at A Level (OCR H446 / J417), not at GCSE. This lesson is included to give you a complete picture of Boolean logic, but for your Paper 2 answers you must express everything using only AND, OR, and NOT.
The good news is that XOR can always be rewritten using only the three J277 operators:
A XOR B = (A AND (NOT B)) OR ((NOT A) AND B)
So if a question describes a situation that feels like XOR — for example "exactly one of two conditions is true" — you should always answer using this expansion in AND/OR/NOT form.
Suppose a question says: "A message is flagged when exactly one of two filters triggers — filter A (A = 1) or filter B (B = 1) but not both."
Using only AND, OR, NOT (as required by J277), the expression is:
flag = (A AND (NOT B)) OR ((NOT A) AND B)
Truth table (using AND/OR/NOT columns only):
| A | B | NOT A | NOT B | A . (NOT B) | (NOT A) . B | flag |
|---|---|---|---|---|---|---|
| 0 | 0 | 1 | 1 | 0 | 0 | 0 |
| 0 | 1 | 1 | 0 | 0 | 1 | 1 |
| 1 | 0 | 0 | 1 | 1 | 0 | 1 |
| 1 | 1 | 0 | 0 | 0 | 0 | 0 |
Subscribe to continue reading
Get full access to this lesson and all 10 lessons in this course.