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.

Written by
Updated · 8 min read

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.
binarytranslator.ai

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.

Symbols of the 7 basic logic gates with their rules: AND outputs 1 only if both inputs are 1, OR if either is 1, NOT flips its input, NAND outputs 0 only if both are 1, NOR outputs 1 only if both are 0, XOR outputs 1 if the inputs differ and XNOR if they match.

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.

ABOutput
000
010
100
111

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.

ABOutput
000
011
101
111

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.

AOutput
01
10

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.

ABOutput
001
011
101
110

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.

ABOutput
001
010
100
110

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.

ABOutput
000
011
101
110

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.

ABOutput
001
010
100
111

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.

ABANDORNANDNORXORXNOR
00001101
01011010
10011010
11110001

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 half adder circuit: inputs A and B go to an XOR gate, which gives the sum, and an AND gate, which gives the carry. For 1 plus 1 the sum is 0 and the carry is 1, which is 10 in binary.

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:

GateOperatorExampleIn binary
AND&12 & 10 = 81100 & 1010 = 1000
OR|12 | 10 = 141100 | 1010 = 1110
XOR^12 ^ 10 = 61100 ^ 1010 = 0110
NOT~~12flips 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.

About the authors

Written byZachary PainterTechnical writer at GitLab

Zachary Painter is a technical writer at GitLab, where he writes developer documentation and UI text. He has written API documentation for REST and GraphQL APIs and reference docs for Kubernetes, Docker and command-line tools, earlier as a technical writer at Pomerium and a senior technical content writer at Stream. He holds a BA in English and German studies from the University of North Carolina at Greensboro. On binarytranslator.ai he writes guides and the how-to sections on tool pages.

All guides by ZacharyLinkedIn

Reviewed byMehran Mozaffari KermaniProfessor of computer engineering, University of South Florida

Mehran Mozaffari Kermani is a professor at the Bellini College of Artificial Intelligence, Cybersecurity and Computing at the University of South Florida. His research covers computer arithmetic, cryptographic hardware and fault detection in digital circuits, and he worked as an ASIC design engineer at AMD before he joined academia. He earned his PhD in electrical and computer engineering at the University of Western Ontario, was a postdoctoral fellow at Princeton and is a senior member of IEEE. On binarytranslator.ai he reviews the logic gate, binary arithmetic and floating-point tools.

ProfileLinkedInHow we review

Keep reading

All posts
In an 8-bit signed integer, 127 + 1 equals -128.Binary basics

MSB 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
In floating point, 0.1 + 0.2 equals 0.30000000000000004.Binary basics

Floating 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
The hexadecimal number 2F3 equals 755 in decimal.Binary basics

What is hexadecimal? The base 16 number system explained

Hexadecimal is base 16, with digits 0 to 9 and A to F. See how place values work, why programmers use hex for bytes, the values worth knowing and how octal compares.7 min read
Scroll to Top