Skip to content
digiwleeaTools
LearnGlossaryAboutCheat sheetHow a CPU worksBuild a CPUToolsOpen the lab →

CNOT gate: truth table, and a SWAP from three CNOTs

A CNOT (controlled-NOT) gate has two inputs and two outputs: the control bit A passes through unchanged, and the target bit is flipped when A is 1, so the outputs are A and A XOR B. It is reversible, it is its own inverse, and three CNOTs in a row swap two bits.

On ordinary bits a CNOT is simply an XOR gate that also keeps a copy of its control input, which is what makes it reversible: from the outputs A and A XOR B you can always recover B. Apply it twice and the second XOR cancels the first. The same truth table defines the CNOT gate in quantum computing, where it acts on qubits; a classical simulator like this one shows its behaviour on 0s and 1s, not superposition or entanglement. The second half of this live circuit chains three CNOTs, the middle one pointing the other way, so that S1 = B and S2 = A: the classic SWAP construction. Add a second control bit and you get the Toffoli gate. All four input rows are checked before the page is built.

Live circuit: click the inputs to toggle them and watch the outputs update. It runs the real simulator, right here on the page.

Open this circuit in the lab →

Embed or cite this circuit

Free to embed on a course page, blog or worksheet: paste this code. The circuit stays live, and the small digiwleea badge links back here.

To cite it: digiwleea, “CNOT gate”, https://digiwleea.wleeaf.dev/tools/build/cnot-gate/

What you need

Step by step

  1. Place inputs A and B and probes C1, C2, S1, S2.
  2. CNOT: wire A straight to C1, and XOR A with B into C2.
  3. SWAP, first CNOT (control A, target B): x1 = A XOR B.
  4. Second CNOT (control x1, target A): A XOR x1 equals B. Probe it as S1.
  5. Third CNOT (control that result, target x1): x1 XOR B equals A. Probe it as S2, then toggle A and B: S1 and S2 always show them swapped.

CNOT and SWAP truth table

ABC1 = AC2 = A XOR BS1S2
000000
010110
101101
111011

C1 and C2 are the CNOT outputs; S1 and S2 are the three-CNOT swap. Every output pair occurs once, so both are reversible. The swap works because XOR is its own inverse: (A XOR B) XOR B = A.

You can also read the full theory in the lesson on the XOR gate. Prefer to build it yourself from scratch? Open the digiwleea lab and place the gates on the canvas, or start from the logic gate simulator.

More builds: Universal gates: NAND only, NOR only

All 246 build guides, by topic

Each of these is one step from a transistor to a working CPU. Build every gate from CMOS transistors and work all the way up in the free digiwleea lab, with the free theory reference alongside.

Open the CNOT gate in the lab →

Frequently asked

What is a CNOT gate?

A two-bit gate, controlled-NOT, that leaves its control bit unchanged and flips its target bit when the control is 1. Its outputs are A and A XOR B.

What is the truth table of a CNOT gate?

For inputs 00, 01, 10 and 11 the outputs are 00, 01, 11 and 10: the second bit flips only when the first bit is 1.

How do you make a SWAP gate from CNOT gates?

Use three CNOTs, alternating direction: the first targets B with control A, the second targets A with control B, the third targets B again. The result exchanges the two bits.

Is a CNOT gate the same as an XOR gate?

On classical bits, the CNOT's target output is A XOR B; the difference is that CNOT also keeps A as an output, which makes it reversible. On qubits the CNOT does more, but its effect on 0 and 1 inputs is the same truth table.