Operators
In computer science, an operatorA character, or characters, that determine what action is to be performed or considered. is a character, or characters that determine the action that is to be performed or considered.
There are three types of operator that programmers use:
- mathematical operatorA mathematical function that is used to perform a calculation, eg add, subtract, multiply and divide.
- logical operatorAn operator that performs Boolean logic on input data to result a result that is true or false.
- Boolean operatorAND, OR and NOT. Used to build complex queries in a database.
Mathematical operations
Computers are designed to carry out calculations. Mathematical operators allow arithmetic to be performed on values.
| Mathematical operation | Operator | Example |
| Addition | | |
| Subtraction | | |
| Multiplication | | |
| Division | | |
| Integer division | (finds the whole number after the division) | If is 21, then the result of this is that is 4 |
| Remainder | (finds the remainder after the modulus division) | If is 21, then the result of this is that is 1 |
| Mathematical operation | Addition |
|---|---|
| Operator | |
| Example | |
| Mathematical operation | Subtraction |
|---|---|
| Operator | |
| Example | |
| Mathematical operation | Multiplication |
|---|---|
| Operator | |
| Example | |
| Mathematical operation | Division |
|---|---|
| Operator | |
| Example | |
| Mathematical operation | Integer division |
|---|---|
| Operator | (finds the whole number after the division) |
| Example | If is 21, then the result of this is that is 4 |
| Mathematical operation | Remainder |
|---|---|
| Operator | (finds the remainder after the modulus division) |
| Example | If is 21, then the result of this is that is 1 |
Logical operators
Logical operators allow for assignment and for comparisons to be made. They are used in condition testing.
| Logical operation | Operator | Example |
| Equivalence | | |
| Less than | | |
| Less than or equal to | | |
| Greater than | | |
| Greater than or equal to | | |
| Does not equal | | |
| Logical operation | Equivalence |
|---|---|
| Operator | |
| Example | |
| Logical operation | Less than |
|---|---|
| Operator | |
| Example | |
| Logical operation | Less than or equal to |
|---|---|
| Operator | |
| Example | |
| Logical operation | Greater than |
|---|---|
| Operator | |
| Example | |
| Logical operation | Greater than or equal to |
|---|---|
| Operator | |
| Example | |
| Logical operation | Does not equal |
|---|---|
| Operator | |
| Example | |
Boolean operators
Boolean operators are used to connect and compare the relationship between arguments. The result will be either TRUE or FALSE.
| Boolean operation | Operator | Example |
| Both statements must be true for the argument as a whole to be true. | | Returns if is any number between 5 and 20. |
| Only one of the statements needs be true for the argument as a whole to be true. | | Returns if is either 2 or 5. |
| The opposite of the argument is true. | | Returns if is not 10. |
| The argument is false if both statements are true. The argument is false if both statements are false. Otherwise the argument is true. | | Returns if one of or is greater than 10 and the other is not. |
| Boolean operation | Both statements must be true for the argument as a whole to be true. |
|---|---|
| Operator | |
| Example | Returns if is any number between 5 and 20. |
| Boolean operation | Only one of the statements needs be true for the argument as a whole to be true. |
|---|---|
| Operator | |
| Example | Returns if is either 2 or 5. |
| Boolean operation | The opposite of the argument is true. |
|---|---|
| Operator | |
| Example | Returns if is not 10. |
| Boolean operation | The argument is false if both statements are true. The argument is false if both statements are false. Otherwise the argument is true. |
|---|---|
| Operator | |
| Example | Returns if one of or is greater than 10 and the other is not. |
These can also be combined, for example:
If (x AND y) OR (NOT C)Truth tables
Truth tables are a way of showing all the possible outputs for inputs in a logical expression. Logic gates, used in electronics and therefore computer circuits, are based on truth tables. More complex expressions can combine several inputs and outputs. In the following tables, 1 means TRUE and 0 means FALSE.
AND truth table
| A | B | Output |
| 0 | 0 | 0 |
| 0 | 1 | 0 |
| 1 | 0 | 0 |
| 1 | 1 | 1 |
| A | 0 |
|---|---|
| B | 0 |
| Output | 0 |
| A | 0 |
|---|---|
| B | 1 |
| Output | 0 |
| A | 1 |
|---|---|
| B | 0 |
| Output | 0 |
| A | 1 |
|---|---|
| B | 1 |
| Output | 1 |
In an AND truth table, the output is only TRUE if both inputs are also TRUE.
OR truth table
| A | B | Output |
| 0 | 0 | 0 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 1 |
| A | 0 |
|---|---|
| B | 0 |
| Output | 0 |
| A | 0 |
|---|---|
| B | 1 |
| Output | 1 |
| A | 1 |
|---|---|
| B | 0 |
| Output | 1 |
| A | 1 |
|---|---|
| B | 1 |
| Output | 1 |
In an OR truth table, the output is TRUE if any of the inputs are TRUE.
NOT truth table
| A | Output |
| 0 | 1 |
| 1 | 0 |
| A | 0 |
|---|---|
| Output | 1 |
| A | 1 |
|---|---|
| Output | 0 |
A NOT truth table reverses the input value.
XOR truth table
| A | B | Output |
| 0 | 0 | 0 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 0 |
| A | 0 |
|---|---|
| B | 0 |
| Output | 0 |
| A | 0 |
|---|---|
| B | 1 |
| Output | 1 |
| A | 1 |
|---|---|
| B | 0 |
| Output | 1 |
| A | 1 |
|---|---|
| B | 1 |
| Output | 0 |
In an XOR truth table, the output is TRUE if only one of the input values is TRUE.
Boolean notation
Logical expressions can also be expressed using BooleanA data type in computing which only has two possible values, true or false. algebra notation. Instead of writing the words AND, OR, NOT or XOR it is often written using the following shorthand notation.
| Boolean expression | Shorthand notation |
| A AND B | A . B |
| A OR B | A + B |
| NOT A | A’ (can be also shown as Ā) |
| A XOR B | A ⊕ B |
| Boolean expression | A AND B |
|---|---|
| Shorthand notation | A . B |
| Boolean expression | A OR B |
|---|---|
| Shorthand notation | A + B |
| Boolean expression | NOT A |
|---|---|
| Shorthand notation | A’ (can be also shown as Ā) |
| Boolean expression | A XOR B |
|---|---|
| Shorthand notation | A ⊕ B |
Boolean identities and rules
| Rule | AND form | OR form |
| Commutative law | A . B = B . A | A + B = B + A |
| Associative law | (A . B) . C = A . (B . C) | (A + B) + C = A + (B + C) |
| Distributive law | (A . B) + C = (A + C) . (B + C) | (A + B) . C = (A . C) + (B . C) |
| Identity law | A . 1 = A | A + 0 = A |
| Zero and one law | A . 0 = 0 | A + 1 = 1 |
| Inverse law | A . A’ = 0 | A + A’ = 1 |
| Idempotent law | A . A = A | A + A = A |
| Absorption law | A . (A + B) = A | A + A . B = A A + A’ B = A + B |
| Double complement law | Ā = A |
| Rule | Commutative law |
|---|---|
| AND form | A . B = B . A |
| OR form | A + B = B + A |
| Rule | Associative law |
|---|---|
| AND form | (A . B) . C = A . (B . C) |
| OR form | (A + B) + C = A + (B + C) |
| Rule | Distributive law |
|---|---|
| AND form | (A . B) + C = (A + C) . (B + C) |
| OR form | (A + B) . C = (A . C) + (B . C) |
| Rule | Identity law |
|---|---|
| AND form | A . 1 = A |
| OR form | A + 0 = A |
| Rule | Zero and one law |
|---|---|
| AND form | A . 0 = 0 |
| OR form | A + 1 = 1 |
| Rule | Inverse law |
|---|---|
| AND form | A . A’ = 0 |
| OR form | A + A’ = 1 |
| Rule | Idempotent law |
|---|---|
| AND form | A . A = A |
| OR form | A + A = A |
| Rule | Absorption law |
|---|---|
| AND form | A . (A + B) = A |
| OR form | A + A . B = A A + A’ B = A + B |
| Rule | Double complement law |
|---|---|
| AND form | Ā = A |
Examples:
A AND A = A
How?
If A is 0, then the expression is 0 AND 0 = 0, which is equal to A.
If A is 1, then the expression is 1 AND 1 = 1, which is equal to A.
A OR A = A
How?
If A is 0, then the expression is 0 OR 0 = 0, which is equal to A.
If A is 1, then the expression is 1 OR 1 = 1, which is equal to A.
A AND NOT A = 0
How?
If A is 1, then NOT A = 0.
Then the expression becomes 1 AND 0 = 0.
If A is 0, then NOT A = 1.
Then the expression becomes 0 AND 1 = 0.
Simplifying Boolean expressions
You may be asked to simplify a Boolean expression, for example:
X = A AND B OR A AND NOT B
X = A AND (B OR NOT B)
X = A AND 1
X = A
More guides on this topic
- The CPU - Eduqas
- Primary storage - Eduqas
- Secondary storage and embedded systems - Eduqas
- Networks - Eduqas
- Internet and cybersecurity - Eduqas
- Data representation - Eduqas
- Storage and data organisation - Eduqas
- Operating systems - Eduqas
- Principles of programming - Eduqas
- Sorting, searching and validation - Eduqas
- Software development - Eduqas
- Impacts of digital technology on wider society - Eduqas