LogicGates.org Open the simulatorSimulator
Roadmap

XOR, the odd one out

Lesson 4 of 6 in this stage, about 10 minutes

One or the other but not both, and why that is how computers add.

One or the other, but not both

The OR lesson warned that the gate is not the "or" of everyday speech: OR is happy when both inputs are 1. There is a gate for the everyday meaning too. It outputs 1 when one input or the other is 1, but not when both are, and not when neither is. It is called XOR, short for "exclusive or", because it excludes the case where both are on.

XOR gate. Click the inputs.

out 0
ab out
0 0 0
0 1 1
1 0 1
1 1 0

The cleanest way to remember it is not about "or" at all: XOR outputs 1 when its two inputs are different, and 0 when they are the same. Look at the table. 00 and 11, the two rows where the inputs match, give 0. 01 and 10, where they differ, give 1. The symbol is the OR shape with a second curved line drawn behind its back.

Adding without the carry

Here is why XOR earns a place among the seven. Add two single bits and there are four cases: 0 + 0 = 0, 0 + 1 = 1, 1 + 0 = 1, and 1 + 1 = 10, which is binary for two: a 0 in this column and a 1 carried to the next. Now look at just the digit that stays in this column: 0, 1, 1, 0. That is the XOR table. And look at the carry: 0, 0, 0, 1. That is the AND table.

So one XOR gate and one AND gate, side by side on the same two inputs, add two bits. The XOR gives the sum digit and the AND gives the carry. That little circuit has a name, the half adder, and it is the first thing built in the stage on arithmetic. Everything a computer does with numbers starts here.

Worked example. XOR the patterns 1101 and 1011 bit by bit.

Line them up and take one column at a time. Leftmost: 1 and 1 are the same, so 0. Next: 1 and 0 differ, so 1. Next: 0 and 1 differ, so 1. Rightmost: 1 and 1 are the same, so 0. The answer is 0110. Notice that every column is worked out on its own; nothing carries from one to the next, which is exactly what "add without carry" means.

A switch that flips

Set one input of an XOR gate to a fixed value and something useful happens to the other. Hold b at 0 and the output simply copies a: 0 gives 0, 1 gives 1. Hold b at 1 and the output is the opposite of a: 0 gives 1, 1 gives 0. So an XOR gate is a NOT gate that can be switched on and off by its second input. Engineers call this a controlled inverter, and it is how a circuit is told "use this number as it is" or "use its opposite" with a single control wire.

XOR as a controlled inverter: with flip = 1 the output is the opposite of a; with flip = 0 it is a.

out 0
aflip out
0 0 0
0 1 1
1 0 1
1 1 0
Why?: why not just use a NOT gate?

A NOT gate always inverts. There is no way to ask it not to. When a circuit needs to invert a value sometimes, depending on another signal, XOR does in one gate what would otherwise take two NOTs, two ANDs and an OR. Subtraction in a computer is done this way: the same adder is used, with the second number flipped by a row of XOR gates when the operation is a subtraction.

Counting 1s: parity

Chain XOR gates together and the pattern continues. Three inputs XORed give 1 when an odd number of them are 1 and 0 when an even number are. Click through the widget below: the output is 1 for exactly one input on or all three on, and 0 for none or two. This is called the parity of the inputs, and it is a cheap way to check that a set of bits arrived intact. Send the bits plus one extra bit that makes the count of 1s even; if the receiver counts an odd number, something was flipped on the way.

Three inputs XORed: 1 when an odd number of them are 1.

out 0
abc out
0 0 0 0
0 0 1 1
0 1 0 1
0 1 1 0
1 0 0 1
1 0 1 0
1 1 0 0
1 1 1 1
Common mistake: treating XOR like OR when both inputs are 1

The one row people get wrong is the bottom one. OR gives 1 for two 1s; XOR gives 0. If the inputs are the same, XOR is 0, whether they are both 0 or both 1. Say "different?" to yourself rather than "or", and the bottom row takes care of itself.

What to remember

  • XOR outputs 1 when its two inputs are different and 0 when they are the same.
  • It is the everyday "or": one or the other, but not both.
  • XOR is the sum digit when two bits are added; AND is the carry. Together they make a half adder.
  • XOR with a 1 flips the other input; XOR with a 0 leaves it alone. That is a controlled inverter.
  • A chain of XORs tells you whether the number of 1s is odd, which is called parity.

Check yourself

Get 5 right in a row and the lesson is done. A wrong answer costs the run, not the lesson.

0 right in a row. 0 / 0 this visit

XOR the patterns 1001 and 0001 bit by bit: the first bit of one with the first bit of the other, and so on.

1001 XOR 0001

Already know this? and come back to the quiz any time.

Go deeper