- Work out the result of AND, OR, NOT and XOR
- Build and read truth tables
- Evaluate a logical expression
- Explain how logic gates are used inside a computer
Elvin tells his friends: “If the weather is good on Saturday and I finish my homework, I'll go to the Baku Boulevard.” This sentence has two conditions joined by the word “and”. A computer makes decisions in exactly this way — it joins simple conditions with logical operations. Only instead of “yes” and “no” it uses 1 and 0.
Statements: true and false
A sentence that is definitely either true or false. A true statement has the value 1, and a false one has the value 0.
For example, “Baku is the capital of Azerbaijan” is a true statement (1), and “5 > 8” is a false statement (0). “What's the weather like today?” is not a statement at all, because a question can be neither true nor false.
The basic logical operations
- NOT (negation) flips the value: NOT 1 = 0, NOT 0 = 1.
- AND (conjunction) gives 1 only when both statements are true.
- OR (disjunction) gives 1 when at least one statement is true.
- Exclusive OR (XOR) gives 1 only when exactly one statement is true, that is, when the inputs differ.
A table that shows the result of an operation for every possible case is called a truth table. With two inputs there are only 4 cases:
| A | B | A AND B | A OR B | A XOR B |
|---|---|---|---|---|
| 0 | 0 | 0 | 0 | 0 |
| 0 | 1 | 0 | 1 | 1 |
| 1 | 0 | 0 | 1 | 1 |
| 1 | 1 | 1 | 1 | 0 |
| A | NOT A |
|---|---|
| 0 | 1 |
| 1 | 0 |
Logic gates
Inside a computer, logical operations are carried out by logic gates. These are tiny electronic circuits built from transistors: signals of 0 or 1 go into the inputs, and the result comes out of the output. A modern processor contains billions of transistors, and the gates built from them add numbers, compare them and store them in memory.
Besides the basic gates, their combinations are widely used: NAND (NOT AND) flips the result of AND, and NOR (NOT OR) flips the result of OR. Interestingly, any other logic circuit can be built from NAND gates alone.
Logical expressions and binary addition
Operations combine into a logical expression. Without brackets, NOT is done first, then AND, and OR last — much like multiplication comes before addition in math.
Find the value of (A OR B) AND NOT C when A = 1, B = 0, C = 1.
Show solutionHide solution
2) NOT C = NOT 1 = 0.
3) 1 AND 0 = 0.
The expression is false, because one of the inputs of AND is 0.
We need to add two bits, A and B. Which gates give the sum digit (S) and the carry to the next position (C)?
Show solutionHide solution
0 + 0 = 00₂ → S = 0, C = 0
0 + 1 = 01₂ → S = 1, C = 0
1 + 0 = 01₂ → S = 1, C = 0
1 + 1 = 10₂ → S = 0, C = 1
The S column matches the XOR table, and the C column matches the AND table.
So S = A XOR B and C = A AND B. By combining such circuits, a processor adds numbers of any length.
print('A B | AND OR XOR')
for a in (0, 1):
for b in (0, 1):
print(f'{a} {b} | {a & b} {a | b} {a ^ b}')▸ Expected output
A B | AND OR XOR 0 0 | 0 0 0 0 1 | 0 1 1 1 0 | 0 1 1 1 1 | 1 1 0
& is AND, | is OR and ^ is XOR on bits. The program builds the whole truth table by itself.Key points
- A statement is either true (1) or false (0).
- AND gives 1 only when both inputs are 1; OR gives 1 when at least one input is 1.
- NOT flips the value; XOR gives 1 when the inputs differ.
- Without brackets the order is NOT, then AND, then OR.
- Logic gates are built from transistors; XOR and AND together add two bits.
Check yourself
10 questions. Every correct answer earns XP.