← Back to modules

Module

Logic

How computers answer yes/no questions and why chaining those answers together lets them decide, compare, and compute anything.

1. Boolean values

What is a boolean?

A boolean is a value that can hold exactly one of two states: true or false. The name comes from George Boole, a 19th-century mathematician who developed the algebra of logical reasoning now the mathematical foundation of every computer ever built.

You already reason in booleans constantly without thinking about it. Every yes/no question you ask yourself produces a boolean:

QuestionBoolean result
Is the door open?true / false
Is the password correct?true / false
Is the file found?true / false
Is the number greater than 10?true / false
Is the user logged in?true / false
Boolean and binary are the same thing.
In the Binary module you learned that computers store everything as 1s and 0s. A boolean maps directly onto that: 1 = true, 0 = false. A single boolean value fits in one bit. This connection is not a coincidence it is why logic gates (which you will see in Section 4) can be built from the same transistors that store binary data.

2. Boolean operations

NOT, AND, and OR

Boolean algebra has three fundamental operations. Every more complex expression in code, in circuits, in search queries is built from these three.

NOT ¬A

Inverts a single value. The output is always the opposite of the input.

ANOT A
0 (false)1 (true)
1 (true)0 (false)

AND A ∧ B

Returns true only when both A and B are true. A single false input makes the whole expression false.

ABA AND B
000 (false)
010 (false)
100 (false)
111 (true)

OR A V B

Returns true when at least one of A or B is true. Only false when both inputs are false.

ABA OR B
000 (false)
011 (true)
101 (true)
111 (true)

Quick check answer each question:

1. A boolean value can only be true or false.

2. (true AND false) evaluates to true.

3. (false OR true) evaluates to true.

4. NOT(false) evaluates to true.

5. (true AND (false OR true)) evaluates to true.

These three operations are the atoms of all logical reasoning. Every conditional in every program ever written ultimately evaluates to a chain of NOTs, ANDs, and ORs.


3. Operator precedence

Precedence and compound expressions

When you combine multiple operations without parentheses, precedence rules dictate the order of evaluation exactly like multiplication before addition in arithmetic.

PriorityOperatorEvaluated
1 (highest)NOT ¬First
2AND ∧Second
3 (lowest)OR VLast

Example 1 no parentheses

true OR false AND false
= true OR (false AND false) [AND evaluates before OR]
= true OR false
= true

Example 2 parentheses change the result

(true OR false) AND false
= true AND false [parentheses evaluated first]
= false

Example 3 NOT applied before AND

NOT false AND true
= (NOT false) AND true [NOT is highest priority]
= true AND true
= true

Tip: When in doubt, use parentheses. They make intent explicit and eliminate ambiguity for everyone reading your code including you, six months later.

4. Logic gates

How a computer processes logic

Boolean operations are not just abstract symbols they are physical circuits built into every chip. These circuits are called logic gates. Each gate takes electrical signals as inputs (high voltage = 1, low voltage = 0) and produces an output according to its truth table.

Modern CPUs contain billions of transistors, and all of them perform one of a handful of gate operations. The binary adder you saw in the Binary module, for example, is constructed entirely from AND and XOR gates no arithmetic hardware is needed beyond logic.

NOT

Inverter flips a single bit

AND

True only when both inputs are 1

OR

True when any input is 1

NAND

Universal gate can build any other

NOR

Universal gate can build any other

XOR

True when inputs differ used in adders

Why NAND and NOR are special.
A NAND gate alone can simulate NOT, AND, and OR. A NOR gate can too. This means you can build an entire computer using only one type of gate which simplifies chip fabrication enormously. Real chips are largely constructed from NAND gates for exactly this reason.

Explore all six gates below toggle the inputs and watch the truth table update in real time:

Output is true only when both A and B are true.

OUT = A AND B

Output0false
ABOutput
000
010
100
111

5. Boolean theorems

Laws and theorems of boolean algebra

Like ordinary algebra, boolean algebra has a set of identities that always hold true. These let you simplify complex logical expressions fewer gates, fewer conditions, simpler code.

LawAND formOR form
IdentityA AND true = AA OR false = A
Null / DominationA AND false = falseA OR true = true
IdempotentA AND A = AA OR A = A
ComplementA AND NOT A = falseA OR NOT A = true
Double NegationNOT (NOT A) = A

De Morgan's Laws

The most practically useful theorems in boolean algebra. They describe how NOT distributes over AND and OR and they are the reason you can rewrite any expression in terms of NAND or NOR alone.

NOT (A AND B) = NOT A OR NOT B

"Not both" is the same as "not A, or not B"

NOT (A OR B) = NOT A AND NOT B

"Neither A nor B" is the same as "not A, and not B"

De Morgan's in plain English.
"It is not raining AND snowing" is the same as "It is not raining, OR it is not snowing." You can verify this by trying all four combinations of raining (true/false) and snowing (true/false) both sides of the equation always agree.

Simplification example using double negation and complement

NOT (NOT A) AND (A OR NOT A)
= A AND (A OR NOT A) double negation
= A AND true complement law
= A identity law

Chips save power and space when expressions are simplified. Compilers apply these same laws automatically when optimising your code but knowing them yourself means you can write cleaner conditions from the start.