Binary basics
Logic gates explained
AND, OR, NOT and four more rules turn bits into decisions. See every truth table, try each gate, and find out how they add numbers.
A logic gate is a tiny circuit that takes one or two bits in and gives one bit out, following a fixed rule. Feed an AND gate a 1 and a 0 and you get 0, because AND outputs 1 only when both inputs are 1. An OR gate outputs 1 when at least one input is 1, and a NOT gate flips its input. There are seven basic gates: AND, OR, NOT, NAND, NOR, XOR and XNOR. A processor is built from billions of them, and every sum or comparison it makes comes down to these rules.
Key takeaways
| A logic gate takes one or two bits in and gives one bit out, by a fixed rule. | |
| The seven basic gates are AND, OR, NOT, NAND, NOR, XOR and XNOR. | |
| Two inputs give four input pairs, so a two-input truth table has four rows. | |
| NAND and NOR are universal: either one on its own can build every other gate. | |
| A half adder is XOR for the sum and AND for the carry. Chain full adders to add whole numbers. |
Try every gate at once
Set inputs A and B to 0 or 1. Each row shows what that gate would output. The NOT row only looks at A.
The 7 basic logic gates
Each gate below has its rule in one sentence, its Boolean expression and its truth table, which lists the output for every possible input. In the expressions, a dot means AND, a plus means OR and a bar or apostrophe means NOT.

AND gate
Outputs 1 only if A and B are both 1. Expression: A · B. Think of two switches in a row: the lamp only lights when both are closed. Programs use AND to keep some bits and clear others, which is called masking.
| A | B | Output |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 0 |
| 1 | 0 | 0 |
| 1 | 1 | 1 |
OR gate
Outputs 1 if A or B, or both, are 1. Expression: A + B. This is two switches side by side: closing either one lights the lamp. A car door warning that beeps when any door is open works like an OR gate.
| A | B | Output |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 1 |
NOT gate
Has one input and outputs the opposite. Expression: A′ or A with a bar over it. It is also called an inverter. The small circle on its symbol means "invert", and the same circle turns AND into NAND and OR into NOR.
| A | Output |
|---|---|
| 0 | 1 |
| 1 | 0 |
NAND gate
NOT AND. Outputs 0 only when both inputs are 1, and 1 in every other case. Expression: (A · B)′. In CMOS chips a NAND gate needs only four transistors, while an AND gate needs six, because an AND is built as a NAND followed by a NOT. You can also make every other gate from NAND gates alone.
| A | B | Output |
|---|---|---|
| 0 | 0 | 1 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 0 |
NOR gate
NOT OR. Outputs 1 only when both inputs are 0. Expression: (A + B)′. Like NAND, it can build any other gate on its own. The Apollo Guidance Computer that flew to the Moon was built almost entirely from NOR gates.
| A | B | Output |
|---|---|---|
| 0 | 0 | 1 |
| 0 | 1 | 0 |
| 1 | 0 | 0 |
| 1 | 1 | 0 |
XOR gate
Exclusive OR. Outputs 1 when the inputs are different and 0 when they are the same. Expression: A ⊕ B. When you add two bits, XOR gives you the sum digit, and it also turns up in error checks and encryption. The XOR calculator runs it on whole numbers, text and hex.
| A | B | Output |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 0 |
XNOR gate
Exclusive NOR. Outputs 1 when the inputs are the same. Expression: (A ⊕ B)′. Because it answers "are these two bits equal?", a row of XNOR gates can compare two numbers bit by bit.
| A | B | Output |
|---|---|---|
| 0 | 0 | 1 |
| 0 | 1 | 0 |
| 1 | 0 | 0 |
| 1 | 1 | 1 |
Logic gates truth table: all gates side by side
The same four input pairs, with every two-input gate in one table. Reading across a row shows how the gates differ for the same inputs.
| A | B | AND | OR | NAND | NOR | XOR | XNOR |
|---|---|---|---|---|---|---|---|
| 0 | 0 | 0 | 0 | 1 | 1 | 0 | 1 |
| 0 | 1 | 0 | 1 | 1 | 0 | 1 | 0 |
| 1 | 0 | 0 | 1 | 1 | 0 | 1 | 0 |
| 1 | 1 | 1 | 1 | 0 | 0 | 0 | 1 |
With two inputs there are only four possible input pairs, and each one can map to 0 or 1, so there are 24 = 16 possible two-input gates in total. The six two-input gates above are the ones with names you will see in textbooks and chip catalogs. The 16 comes from the same doubling rule as everything else in binary, which the powers of 2 guide walks through.
How logic gates are built from transistors
A gate is made from transistors, which act as switches that a voltage can turn on or off. In the CMOS chips used in almost every phone and computer, a NOT gate takes two transistors: one connects the output to the high voltage, the other to ground, and the input decides which one is on.
A NAND gate takes four transistors. An AND gate is a NAND gate followed by a NOT gate, so it needs six. That is why chip designers think in NANDs and NORs: the inverted gates are the natural shape of the circuit, and the plain AND and OR cost extra parts. The common 7400 chip, sold since the 1960s, holds four two-input NAND gates in one package, as the Texas Instruments SN7400 datasheet shows.
Universal gates: building everything from NAND
NAND and NOR are called universal gates because each one can make every other gate. With NAND alone:
- NOT A is A NAND A. Tie both inputs together and the gate flips its input.
- A AND B is (A NAND B) NAND (A NAND B). The second NAND acts as a NOT on the first.
- A OR B is (A NAND A) NAND (B NAND B). Invert both inputs first, then NAND them.
- A XOR B takes four NAND gates.
The OR trick comes from De Morgan's law, which says that NOT (A AND B) equals (NOT A) OR (NOT B). It lets engineers swap AND for OR whenever they flip the inputs and output.
How logic gates add numbers
Adding two bits has four cases: 0 + 0 = 0, 0 + 1 = 1, 1 + 0 = 1 and 1 + 1 = 10 in binary. The right-hand digit of the answer is 1 when the inputs differ, which is XOR. The carry is 1 only when both inputs are 1, which is AND. One XOR gate and one AND gate make a circuit called a half adder.

A full adder also takes the carry from the column to its right, and it is built from two half adders and an OR gate. Line up 32 full adders, pass each carry along to the next, and you can add two 32-bit numbers. That is the same column-by-column method you learned for decimal sums, done in wires. The binary calculator shows the carries when you add two binary numbers.
Where logic gates are used
- Processors use them for arithmetic, comparisons and the choices that decide which instruction runs next.
- Memory can be built from them. Two NOR gates wired into each other form a latch, a circuit that holds one bit until it is told to change.
- Error checks use XOR. XOR all the bits of a message together and you get a parity bit, which changes if any single bit gets flipped in transit.
- Encryption uses XOR to mix data with a key. XOR the result with the same key again and the original comes back.
- Everyday devices use them too: a microwave that only starts with the door closed and a time set is checking an AND condition.
Logic gates in programming
Most programming languages have bitwise operators that apply a gate to every bit of two numbers at once. In C, Java, JavaScript and Python they look like this:
| Gate | Operator | Example | In binary |
|---|---|---|---|
| AND | & | 12 & 10 = 8 | 1100 & 1010 = 1000 |
| OR | | | 12 | 10 = 14 | 1100 | 1010 = 1110 |
| XOR | ^ | 12 ^ 10 = 6 | 1100 ^ 1010 = 0110 |
| NOT | ~ | ~12 | flips every bit |
Words like and and or in Python, or && and || in JavaScript, are the same rules applied to a single true or false value instead of every bit. If ~12 surprises you by giving -13 in Python and JavaScript, that is expected: the numbers are signed, and flipping every bit of 12 gives the two's complement pattern for -13. To see what any number looks like in bits first, use the decimal to binary converter.
Questions people ask
What are the 7 basic logic gates?
AND, OR, NOT, NAND, NOR, XOR and XNOR. NOT has one input and the other six have two.
What is the difference between OR and XOR?
They differ only when both inputs are 1. OR outputs 1 in that case, and XOR outputs 0. XOR means "one or the other, but not both".
Which logic gates are universal?
NAND and NOR. Either one, used on its own, can build every other gate, so in theory you could make a whole computer from one type.
What are all 16 logic gates?
With two inputs there are 16 possible rules. Besides the six named two-input gates, they include gates that always output 0 or always 1, gates that copy A or B, gates that invert A or B, the two forms of "A but not B" and the two forms of "if A then B". Only the named ones are sold as parts.
How do logic gates work?
Each gate is a few transistors wired so that the input voltages switch the output to high (1) or low (0) according to the gate's rule. A NOT gate, for example, uses two transistors and connects its output to whichever voltage is opposite to its input.
Where can I practice logic gates?
Use the gate tester at the top of this page, then try the XOR calculator and the binary calculator to see the same rules applied to whole numbers.
Keep reading
All posts
Binary basicsMSB and LSB: most and least significant bits, signed integers and overflow
The MSB is the leftmost, highest-value bit and the LSB the rightmost. See what each tells you, how signed integers use the MSB as a sign bit, and how integer overflow wraps values around.9 min read
Binary basicsFloating point numbers explained: why 0.1 + 0.2 is not 0.3
A floating point number is scientific notation in binary. See why 0.1 + 0.2 is 0.30000000000000004, how precise floats are, float vs double, and how to compare floats and handle money.9 min read
Binary basics
