Logic Gates
The building blocks that turn binary data into decisions, calculations, and programs
Introduction
Imagine you are at a door with two switches. The door only opens when both switches are flipped up. This simple rule — both must be true — is the exact same logic your computer uses billions of times every second.
In the last chapter, you learned that computers speak binary — the language of 1s and 0s. But how does a computer actually do something with those 1s and 0s? The answer is logic gates. These tiny electronic circuits take binary input signals and produce binary output according to simple rules.
Logic gates are the fundamental building blocks of every digital device. By combining them in clever ways, engineers build circuits that can add numbers, compare values, store data, and execute entire programs — all starting from just a few basic rules.
How It Works
A logic gate is like a bouncer at a club. The bouncer checks who is at the door and decides whether to let them in. An AND gate says "you can come in only if BOTH of your friends are with you." An OR gate says "you can come in if EITHER friend shows up." A NOT gate says "you can come in ONLY if you are NOT on the list."
Household Object Analogy
Think of a hallway with two light switches (a three-way switch setup). You can flip either switch to turn the light on or off — that is an XOR gate (exclusive OR). The state of each switch is an input, and the light is the output. Logic gates work the same way but with electricity representing 0s and 1s.
The Basic Gates
| Gate | Symbol | Rule | Analogy |
|---|---|---|---|
| AND | && | Output is 1 only if BOTH inputs are 1 | Two keys needed to open a safe |
| OR | || | Output is 1 if AT LEAST ONE input is 1 | Two doors into a room — enter through either |
| NOT | ! | Output is the opposite of the input | Inverting a light switch |
| NAND | !& | Output is 0 only if BOTH inputs are 1 | Opposite of AND |
| NOR | !| | Output is 1 only if BOTH inputs are 0 | Opposite of OR |
| XOR | ^ | Output is 1 if inputs are DIFFERENT | Hallway three-way switch |
| XNOR | !^ | Output is 1 if inputs are the SAME | Opposite of XOR |
Deeper Dive
Logic gates are built from transistors, which act as tiny electronic switches. In a typical AND gate, two transistors are connected in series. Current only flows through both (producing a 1 output) when both transistors are switched on (both inputs are 1). An OR gate connects transistors in parallel, so current flows if either path is active.
By combining logic gates, engineers create combinational circuits like adders, multiplexers, and decoders. For example, a half adder uses one XOR gate and one AND gate to add two binary digits, producing a sum and a carry bit. An 8-bit adder chains eight of these together to add numbers up to 255.
Truth Tables at a Glance
A truth table lists every possible combination of inputs and shows the output. For an AND gate with inputs A and B: A=0,B=0 gives 0; A=0,B=1 gives 0; A=1,B=0 gives 0; A=1,B=1 gives 1. Truth tables are the "cheat sheets" of logic gate design.
Advanced
Modern CPUs use CMOS (Complementary Metal-Oxide-Semiconductor) technology to build logic gates from pairs of p-type and n-type transistors. CMOS consumes very little power because current only flows during switching, not when the gate is idle. This efficiency is why billions of transistors can fit on a single chip without overheating.
Logic gates are combined into sequential circuits using feedback loops. Flip-flops and latches store a single bit of data by feeding the output of a gate back into its input. These memory elements are the building blocks of registers, cache, and RAM.
The Arithmetic Logic Unit (ALU) inside every CPU is a massive network of logic gates. A modern ALU can perform addition, subtraction, multiplication, division, bitwise operations, and comparisons — all by routing binary data through carefully designed combinations of AND, OR, NOT, and XOR gates.
Vocabulary Table
| Term | Definition |
|---|---|
| Logic Gate | An electronic circuit that performs a boolean operation on one or more binary inputs |
| AND Gate | Outputs 1 only when all inputs are 1 |
| OR Gate | Outputs 1 when at least one input is 1 |
| NOT Gate | Outputs the opposite of the input (also called an inverter) |
| NAND Gate | Outputs 0 only when all inputs are 1 (opposite of AND) |
| NOR Gate | Outputs 1 only when all inputs are 0 (opposite of OR) |
| XOR Gate | Outputs 1 when inputs are different |
| Truth Table | A table showing all possible input combinations and their outputs |
| Transistor | A semiconductor device that acts as an electronic switch, the building block of gates |
| ALU | Arithmetic Logic Unit, the part of the CPU that performs calculations using logic gates |
Fun Facts
You can build any logic gate using only NAND gates or only NOR gates. They are called "universal gates" because they can create every other gate.
The first logic gates were built using mechanical relays in the 1930s. Today, a modern CPU contains billions of microscopic gates etched onto silicon.
"Boolean algebra" — the math behind logic gates — was invented by George Boole in 1847, nearly a century before the first electronic computer.
A simple AND gate can be made with just two diodes and a resistor. You can build one yourself with basic electronics components.
Logic gates switch state in less than a nanosecond (one billionth of a second). Light travels only about 30 cm in that time.
Interactive Diagram
Launch the interactive diagram to see this in action.
Open Interactive DiagramThe interactive diagram for this chapter demonstrates Logic Gates. It shows AND, OR, NOT, and other logic gates with input switches and output indicators.
What to explore:
- click the input switches to toggle them on/off; watch the output change based on the logic gate type; see the truth table update
- logic gates are the fundamental building blocks of all digital circuits, performing basic logical operations
Knowledge Check
1. Which logic gate outputs 1 only when BOTH inputs are 1?
Answer: AND gate
2. What is a truth table used for?
Answer: To show all input-output combinations for a logic gate
3. The AND gate, OR gate, and NOT gate are all built from which fundamental electronic component?
Answer: Transistors
