Boolean Logic and Logic Gates
Learn Boolean logic and the AND, OR and NOT logic gates that power every computer. Build truth tables, combine gates, and see how electronics make decisions β with worked examples and a quiz.
Key takeaways
- Boolean logic works with just two values: true (1) and false (0)
- The three basic logic gates are AND, OR and NOT
- AND is true only when both inputs are true; OR is true when at least one is true; NOT flips its input
- A truth table lists every possible input and the output it produces
- Combining simple gates lets computers make complex decisions and do arithmetic
Computers only know true and false
A computer cannot understand "maybe" or "kind of". Underneath every game, website and app, a processor is making millions of tiny yes/no decisions every second. The mathematics behind those decisions is called Boolean logic, named after George Boole, who worked it out in the 1840s β long before electronic computers existed.
Boolean logic uses just two values: true and false. We usually write them as 1 for true and 0 for false. That matches how data is stored, which you can read about in How Computers Store Data in Binary. Every complicated decision a computer makes is built from combining these two values with three simple rules called gates.
The three basic gates
A logic gate takes one or more true/false inputs and produces a single true/false output. There are three you must know.
AND
An AND gate is true only when every input is true. Think of a door that needs two keys turned at the same time β if either key is missing, it stays locked.
| A | B | A AND B |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 0 |
| 1 | 0 | 0 |
| 1 | 1 | 1 |
OR
An OR gate is true when at least one input is true. Picture a room with two light switches β flipping either one (or both) turns the light on.
| A | B | A OR B |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 1 |
NOT
A NOT gate has a single input and simply flips it. True becomes false; false becomes true.
| A | NOT A |
|---|---|
| 0 | 1 |
| 1 | 0 |
These tables that list every input combination are called truth tables, and they are the most reliable way to describe exactly what a gate or circuit does.
You already use this in code
If you have written an if statement, you have already used Boolean logic. In most languages, conditions combine with and, or and not:
age = 15
has_ticket = True
# AND: both must be true
if age >= 12 and has_ticket:
print("You may enter")
# OR: at least one true
if age < 5 or age >= 65:
print("Free entry")
# NOT: flip the value
if not has_ticket:
print("Please buy a ticket")
The and, or and not keywords here behave exactly like the gates above. This builds directly on Making Decisions with If β the only new idea is the precise truth-table behaviour behind each keyword.
Combining gates
The real power appears when you wire gates together. Suppose a security alarm should sound when a door is open and the system is armed, or whenever the panic button is pressed. In words and symbols:
alarm = (door_open AND armed) OR panic
Let's trace one case. Say door_open = 1, armed = 0, panic = 0:
door_open AND armed=1 AND 0=00 OR panic=0 OR 0=0
So the alarm stays silent β correct, because the system was not armed. Now try panic = 1 with the others still 0:
door_open AND armed=00 OR 1= 1
The alarm fires, because the panic button always wins. By chaining a few gates you have built a small decision-making machine.
A complete worked example
Here is a tiny Python program that simulates the three gates and prints their truth tables. Run it to confirm the behaviour matches the tables above.
def AND(a, b):
return a and b
def OR(a, b):
return a or b
def NOT(a):
return not a
# Print the AND and OR truth tables
print("A B | AND OR")
for a in (0, 1):
for b in (0, 1):
result_and = int(AND(a, b))
result_or = int(OR(a, b))
print(f"{a} {b} | {result_and} {result_or}")
# Build the alarm logic from gates
def alarm(door_open, armed, panic):
return int(OR(AND(door_open, armed), panic))
print("\nAlarm when door open + armed:", alarm(1, 1, 0)) # -> 1
print("Alarm when only panic: ", alarm(0, 0, 1)) # -> 1
print("Alarm when nothing happens: ", alarm(0, 1, 0)) # -> 0
Reading the output: the loop walks through all four A B combinations and prints the AND and OR results, reproducing the truth tables. The alarm function nests an AND inside an OR, exactly matching the wiring (door_open AND armed) OR panic, and the three test calls confirm it fires in the right situations.
Try it yourself
- Add a NOT. Change the alarm so it only fires when the system is armed and the door is not locked:
armed AND NOT locked. - Build XOR. XOR is true when the two inputs are different. Using only AND, OR and NOT, write
XOR(a, b)and check it against this table:0,0β0,0,1β1,1,0β1,1,1β0. - Draw it. On paper, sketch the alarm circuit as boxes labelled AND, OR and NOT with arrows showing how signals flow.
Challenge: Logic gates can do maths. A "half adder" adds two single bits and produces a sum and a carry. It turns out sum = A XOR B and carry = A AND B. Write a half_adder(a, b) function returning both values, and verify that 1 + 1 gives sum 0, carry 1 β which is binary for 2. You have just built the heart of how a processor does arithmetic.
Quick quiz
Test yourself and earn XP
How many values does Boolean logic use?
Boolean logic uses exactly two values, usually written true/false or 1/0.
When is an AND gate's output true?
AND outputs true only if every input is true.
When is an OR gate's output true?
OR outputs true if one OR both inputs are true; it is false only when both are false.
What does a NOT gate do?
NOT inverts its input: true becomes false, false becomes true.
What is a truth table?
A truth table shows the output for every possible combination of inputs.
FAQ
They fit together perfectly. Binary represents data with 0s and 1s; Boolean logic represents true and false the same way (1 = true, 0 = false). Inside a computer, a 1 is a higher voltage and a 0 is a lower one, and logic gates are tiny electronic circuits that take those voltages as inputs and produce a new one as output. That is literally how a chip 'thinks'.
Yes. Combining the basic three gives handy ones like NAND (NOT-AND), NOR (NOT-OR) and XOR (exclusive-OR, true when the inputs differ). Remarkably, every other gate can be built from NAND alone, which is why NAND is sometimes called a 'universal' gate.
Keep exploring
More in Coding