Skip to content
Tutorials Center
My learning

Topics

Lesson 9 of 57 ยท Logic Gates

NAND, NOR, XOR and XNOR

Learn NAND, NOR, XOR and XNOR gates: truth tables, symbols, XOR as parity and difference, XNOR as equality, and IEC symbol variants, with live Python tables.

AND, OR and NOT are enough to build any circuit, but designers constantly need four more gates. NAND and NOR are simply AND and OR followed by an inverter, and they turn out to be cheaper to build than AND and OR themselves. XOR and XNOR answer two very practical questions: "are these two bits different?" and "are these two bits the same?"

This lesson gives the truth table, the Boolean expression and the symbol of each gate, shows how XOR works as a parity and difference detector, and uses Python to print and compare all of the gates side by side. The following lesson shows why NAND and NOR are called universal.

Four more gates

NAND NOT of AND. Zero only when all inputs are 1.
NOR NOT of OR. One only when all inputs are 0.
XOR One when the inputs differ.
XNOR One when the inputs are equal.
1 / 2

NAND: NOT AND

The NAND gate is an AND gate followed by a NOT. Its output is 0 only when all inputs are 1, and 1 in every other case. The expression is Y = (AยทB)โ€ฒ, and the symbol is the AND shape with a bubble on the output.

A B AยทB Y = (AยทB)โ€ฒ
0 0 0 1
0 1 0 1
1 0 0 1
1 1 1 0

Note that NAND is not the same as the AND of inverted inputs. That is a different function, and the lesson on De Morgan's theorems explains how the two relate.

NOR: NOT OR

The NOR gate is an OR followed by a NOT. Its output is 1 only when all inputs are 0. The expression is Y = (A + B)โ€ฒ and the symbol is the OR shape with an output bubble.

A B A + B Y = (A + B)โ€ฒ
0 0 0 1
0 1 1 0
1 0 1 0
1 1 1 0

In real chips NAND and NOR are the natural gates: a CMOS NAND uses four transistors where an AND needs six, because the AND is a NAND plus an inverter. The next lesson goes into that.

XOR: exclusive OR

The XOR gate outputs 1 when its inputs are different. It is the OR gate that excludes the case where both are 1. The symbol for XOR is โŠ•, so Y = A โŠ• B, and as an AND/OR expression it is AยทBโ€ฒ + Aโ€ฒยทB ("A and not B, or not A and B").

A B Y = A โŠ• B
0 0 0
0 1 1
1 0 1
1 1 0

The symbol is the OR shape with an extra curved line across the input side. XOR is the heart of arithmetic circuits: it is exactly the sum bit when adding two single bits, since 1 + 1 = 10 in binary, with a sum of 0 and a carry of 1.

XOR as a controlled inverter

Hold one input at a fixed value and see what the other does:

  • A โŠ• 0 = A, so the gate acts as a buffer.
  • A โŠ• 1 = Aโ€ฒ, so the gate acts as an inverter.

So XOR is a NOT gate you can switch on and off with its second input. This trick is used when a circuit must optionally invert a value, for example to turn an adder into a subtractor.

XOR as parity

With more than two inputs, a chain of XOR gates outputs 1 when an odd number of inputs are 1. This is the odd parity function, because XOR is associative and each 1 flips the running result. The XOR of all bits is the foundation of parity checking, which is covered in a later lesson on error detection.

XNOR: equivalence

The XNOR gate is XOR followed by a NOT, and it outputs 1 when the inputs are equal. It is also called the equivalence gate: Y = (A โŠ• B)โ€ฒ = AยทB + Aโ€ฒยทBโ€ฒ.

A B Y = (A โŠ• B)โ€ฒ
0 0 1
0 1 0
1 0 0
1 1 1

To compare two multi-bit numbers for equality, XNOR each pair of bits and AND all the results: the numbers are equal only when every pair matches. That is the first step of the comparator circuit you will design later.

Every two-input gate in one table

The live table below defines all six two-input gates as small functions and prints them side by side. Compare each column with the tables above.

Python All the gates side by side

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Notice the symmetry: AND and NAND are opposites in every row, as are OR and NOR, and XOR and XNOR. Each pair has output counts that add to 4. Also, XOR and XNOR are the only gates whose output is 1 in exactly two rows.

XOR as parity, difference and a switch

The next example checks the three properties of XOR described above: it matches the AND/OR form, it inverts when the second input is 1, and a chain of XORs counts the 1s modulo 2.

Python XOR as difference, controlled NOT and parity

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The last line is a real use: XOR of two words has a 1 exactly at the positions where they differ, and counting those 1s gives the Hamming distance, a measure used in error-correcting codes.

XNOR as an equality checker

Here XNOR is used to compare two 4-bit numbers bit by bit. The two numbers are equal only if every XNOR output is 1, which is the AND of all of them.

Python Equality comparator from XNOR and AND

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

You can see in the output that a single 0 in the XNOR row is enough to make the final result 0.

Symbol variants

You will meet two drawing styles for gates:

  • Distinctive-shape symbols (ANSI/IEEE, used so far): AND has a flat back, OR a curved back, XOR a double curved back, and a bubble marks inversion.
  • Rectangular symbols (IEC): every gate is a plain box with a label inside: & for AND, โ‰ฅ1 for OR, =1 for XOR and 1 for a buffer, with a small circle or a triangle at the output for inversion.

Both mean the same thing. Textbooks and data sheets differ, so it helps to read both.

Also, a bubble can appear on an input, as in a "NOR with a bubbled input". It simply means that input is inverted before the gate acts. Bubble placement becomes an important tool in the lesson on bubble pushing.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. A NAND gate outputs 0 when:

    NAND is NOT of AND, and AND is 1 only when all inputs are 1. So NAND is 0 only in that single case.
  2. 2. What does a 2-input NOR gate output for inputs 0 and 0?

    NOR is 1 only when all inputs are 0, so 0 NOR 0 = 1.
  3. 3. Which statement about XOR is true?

    XOR is the 'different' detector: 01 and 10 give 1, while 00 and 11 give 0.
  4. 4. A 3-input XOR chain outputs 1 for which inputs?

    Each 1 flips the running result, so the output is 1 when the number of 1s is odd (parity).
  5. 5. Which circuit finds out whether two bits are equal?

    XNOR outputs 1 for 00 and 11, which is exactly 'the bits are equal'. XOR would give the opposite.

Summary

  • NAND = (AยทB)โ€ฒ is 0 only when all inputs are 1; NOR = (A + B)โ€ฒ is 1 only when all inputs are 0.
  • XOR (A โŠ• B = AยทBโ€ฒ + Aโ€ฒยทB) is 1 when inputs differ; with a control bit it is a switchable inverter, and a chain of XORs gives odd parity.
  • XNOR = (A โŠ• B)โ€ฒ is 1 when inputs are equal and is the building block of equality comparators.
  • Each gate has distinctive-shape and rectangular (IEC) symbols; a bubble means inversion.

Next: Universal Gates and Gate Counts, how NAND alone (or NOR alone) can build everything.