🔄 Quick Recap
In the last lesson, we saw how the CPU adds and subtracts using tiny switches called transistors.
The part of the CPU that does this job is called the ALU — the Arithmetic Logic Unit.
Well, here’s a fun fact: the ALU doesn’t just do arithmetic (math)…
It also does logic — which means it can make choices.
Now we’ll see how your computer thinks in logic, using simple switch-based tools called…
🧠 Logic Gates!
These are tiny circuits made of transistors (switches) that do super basic thinking.
There are three most important ones:
-
AND
-
OR
-
NOT
Let’s understand what they are by thinking of them like rules.
🔒 AND Gate — “Both Must Be On”
Imagine you have two light switches.
The rule is:
“Only turn the light on if both switches are ON.”
This is an AND gate.
Here’s how it works:
| Switch A | Switch B | Light (Output) |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 0 |
| 1 | 0 | 0 |
| 1 | 1 | 1 |
So:
-
1 AND 1 = 1
-
1 AND 0 = 0
- 0 AND 1 = 0
- 0 AND 0 = 0
The CPU uses this when it wants to check if two things are true at the same time.
💡 Example in Your Computer
Imagine a game that only lets you play if:
-
You have enough coins (A = 1)
-
AND your level is high enough (B = 1)
The game checks:
if (A AND B) = 1 → Let you in!
If either condition is not true, it won’t let you in.
That’s how simple logic makes decisions!
🚪 OR Gate — “At Least One is Enough”
Now imagine the rule is:
“Turn the light on if at least one switch is ON.”
This is an OR gate.
| Switch A | Switch B | Light (Output) |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 1 |
So:
-
0 OR 0 = 0
-
Everything else = 1
💡 When Do CPUs Use OR?
Let’s say your phone rings if you get a message OR a call.
Even if just one of them happens, it rings.
That’s OR logic in action.
🚫 NOT Gate — “Flip It!”
Now what if you had one switch, and the rule was:
“The light is ON when the switch is OFF, and OFF when the switch is ON.”
That’s a NOT gate. It just flips things:
-
NOT 0 = 1
-
NOT 1 = 0
| Input | Output |
|---|---|
| 0 | 1 |
| 1 | 0 |
It’s a very small idea…
But the CPU uses it all the time to make more complex logic.
🧠 Wait — What Are These Gates Really Made Of?
Each gate is made from transistors — remember, those tiny on/off switches.
Let’s break it down:
-
AND gate: Made from about 6 transistors
-
OR gate: Also about 6 transistors
-
NOT gate: Only needs 1–2 transistors
That’s how computers use millions of these gates to build smart programs.
🧠 Logic in the ALU
The ALU can do math and logic because inside it are:
-
Full adders (for math)
-
AND, OR, NOT gates (for logic)
That’s why it’s called the Arithmetic Logic Unit!
🧩 Let’s Try It Yourself
Let’s say you want to check this:
“Is this number even?”
How does the CPU do that?
It checks the last bit of the number:
-
If the last bit is 0 → It’s even
-
If the last bit is 1 → It’s odd
So, the CPU uses a bitwise AND to check:
If (number AND 1) is equal 0 , then the number is Even. Why? Because if the last bit is 0, and 0 AND 1 = 0.
But if the last bit is 1, then 1 AND 1 = 1, which means the number is odd.
🧠 Logic Gates Can Build Big Brains
By combining lots of gates, CPUs can:
-
Add and subtract
-
Make decisions
-
Compare things (like which number is bigger)
-
Create instructions like “if this, then that”
And all of it comes from just YES/NO switches!
📚 Recap: AND, OR, NOT — CPU’s Thinking Tools
-
These are called logic gates
-
AND means both things must be true
-
OR means at least one must be true
-
NOT flips the answer
-
Gates are made using transistors, the same switches inside all CPUs
-
These gates live inside the ALU
-
All computer “thinking” comes from just these three gates combined in clever ways.