Edexcel International Advanced Level Computer Science, point 5.3.1
Definition and function
Boolean logic works with values that are either true (1) or false (0). A Boolean expression combines such values using Boolean operators, and gives a true or false result.
Function: Boolean expressions are the basis of digital circuits (logic gates), and of the conditions in programs (if, while).
a. Boolean operators
| Operator | Result is true when | Expression (words) | Algebra notation |
|---|---|---|---|
| AND | both inputs are true | A AND B |
|
| OR | at least one input is true | A OR B |
|
| XOR (exclusive OR) | the inputs are different (exactly one is true) | A XOR B |
|
| NOT | the input is false (it inverts) | NOT A |
|
| NAND | not both are true (AND, then NOT) | A NAND B |
|
| NOR | neither is true (OR, then NOT) | A NOR B |
Check the notation shown in Appendix 2 of the specification, since the exam uses it: the dot or AND for AND, the plus or OR for OR, a bar over a letter for NOT, and the words NAND, NOR and XOR.
Each operator has its own logic gate symbol. NAND and NOR are AND and OR with a small circle on the output, which means NOT:
Truth tables for each
| A | B | AND | OR | XOR | NAND | NOR |
|---|---|---|---|---|---|---|
| 0 | 0 | 0 | 0 | 0 | 1 | 1 |
| 0 | 1 | 0 | 1 | 1 | 1 | 0 |
| 1 | 0 | 0 | 1 | 1 | 1 | 0 |
| 1 | 1 | 1 | 1 | 0 | 0 | 0 |
| A | NOT A |
|---|---|
| 0 | 1 |
| 1 | 0 |
In Python
a, b = True, False
print(a and b) # AND -> False
print(a or b) # OR -> True
print(a != b) # XOR -> True (different)
print(not a) # NOT -> False
print(not (a and b)) # NAND -> True
print(not (a or b)) # NOR -> False
Useful facts
- NAND and NOR are universal: any circuit can be built from NAND gates alone, or from NOR gates alone.
- De Morgan's laws:
("Break the bar, change the operator.")
b. Order of precedence
When an expression has several operators, they are carried out in this order:
- Brackets first.
- NOT.
- AND.
- OR.
So means , not . Always use brackets if there could be doubt (especially with XOR, NAND and NOR).
Interpreting expressions
Read an expression piece by piece.
means: " is true if both A and B are true, or C is false".
In a program:
if (age >= 17 and has_licence) or is_instructor:
allow_drive = True
- Inputs:
age >= 17,has_licenceandis_instructorare Boolean values. - Expression: with = old enough, = has licence, = is instructor.
Creating expressions
To write an expression from a description:
- Name the conditions (A, B, C).
- Work out how they combine: "both" is AND, "either" is OR, "not" is NOT, "one or the other, but not both" is XOR.
- Use brackets to show the grouping.
Example: An alarm sounds if the system is armed (A) and either the door sensor (D) or the window sensor (W) is triggered.
Example: A heater switches on if the temperature is low (T) and it is not a holiday (H).
Example: A light can be switched from either of two switches (A and B), so that changing either switch changes the light.
Watch the meaning of each symbol. A symbol may be true for the opposite of the condition in the description, and then you need NOT.
Example: A greenhouse fan runs if the greenhouse is too hot, or if the door is shut and the air is damp. The symbols given are: H is true when the greenhouse is not too hot, S is true when the door is shut, and D is true when the air is damp.
"Too hot" is the opposite of H, so it is :
Worked example
Task: (a) Write an expression for "a ticket is free if the person is under 5, or is a carer who is not travelling alone". (b) State the order of operations in .
(a) Let = under 5, = is a carer, = travelling alone. The ticket is free if is true, or ( is true and is not true):
(b) For : first NOT (), then both ANDs ( and ), then OR to join them. With brackets: .
Check with values , , , : , , , , so .
Exam tip: learn the truth table of every operator, and the order NOT, AND, OR. Use the notation from the exam paper. When writing an expression from a description, add brackets and test it with a few values. NAND and NOR are "the opposite" of AND and OR.
Check yourself
When is A XOR B true?
When A and B are different (exactly one of them is true).
What is the order of precedence of NOT, AND and OR?
NOT first, then AND, then OR (with brackets before all of them).
What is the value of 1 NAND 1?
0 (1 AND 1 is 1, and NOT 1 is 0).
Write an expression for "A is true and B is not true"
A AND NOT B, written A · B̅ (A and the inverse of B).
What does De Morgan's law say about NOT (A AND B)?
It equals (NOT A) OR (NOT B).