![]() |
This article has links to websites or programs outside of Scratch and Wikipedia. Remember to stay safe while using the internet, as we cannot guarantee the safety of other websites. |
A truth table is a table which displays the output of a Boolean logic circuit for various inputs.
Uses
Truth tables are used by circuit designers to predict the evaluation of various logic circuits. Logic circuits are usually simplified by circuit designers into a circuit of just NAND gates, which can be represented electronically with transistors. So the complex circuit can be converted into a physical circuit. This is how computer processors work, on a low level.
Operators
The following is a list of logic operators, or gates.
Operator names are usually capitalized. |
AND
The AND operator is very simple. It returns true if both conditions are true:
<<condition 1:: grey> and <condition 2:: grey>>
A | B | Result |
---|---|---|
T | T | T |
T | F | F |
F | T | F |
F | F | F |
NAND
The NAND operator is the opposite of the AND operator, only returning false when both conditions are true. NAND means "Not AND".
There is no block for it in Scratch, but its name suggests its workaround:
<not <<condition 1:: grey> and <condition 2:: grey>>>
A | B | Result |
---|---|---|
T | T | F |
T | F | T |
F | T | T |
F | F | T |
OR
The OR operator is very simple. It returns true if either or both conditions are true.
<<condition 1:: grey>or<condition 2:: grey>>
A | B | Result |
---|---|---|
T | T | T |
T | F | T |
F | T | T |
F | F | F |
It differs from the AND operator when one value is true and one is false. AND returns false, while OR returns true.
NOR
The NOR operator is opposite of the OR operator, only returning true when both conditions are false. NOR means "Not OR".
There is no block for it in Scratch, but its name suggests its workaround:
<not <<condition 1:: grey> or <condition 2:: grey>>>
A | B | Result |
---|---|---|
T | T | F |
T | F | F |
F | T | F |
F | F | T |
XOR
The XOR operator returns true if either, but not both conditions are true. XOR stands for "eXclusive OR".
It is not available with a block in Scratch, but can be simulated with the following script:
<not <<condition 1:: grey> = <condition 2:: grey>>>
A | B | Result |
---|---|---|
T | T | F |
T | F | T |
F | T | T |
F | F | F |
XNOR
The XNOR operator returns false if either, but not both conditions are true. XNOR stands for "eXclusive NOR". It is the opposite of the XOR operation.
It is not available with a block in Scratch, but can be simulated with the following script:
<<condition 1:: grey> = <condition 2:: grey>>
A | B | Result |
---|---|---|
T | T | T |
T | F | F |
F | T | F |
F | F | T |
It is sometimes called XAND ("eXclusive AND").
NOT
NOT is different from the other operators in that it only takes one parameter. It inverts the value.
<not<condition 1:: grey>>
A | Result |
---|---|
T | F |
F | T |
Common Symbols
Many high and low level programming languages use a set of common symbols which represent logic functions.
Symbol | Meaning | Example |
---|---|---|
& or && | and | true && false = false |
| or || | or | true || false = true |
! | not | !true = false |
(Note that the single and double versions of & have slightly different meanings. Similarly for |
.)
In Python and some other languages, the words and, or, and not are used directly.
Combining Logic Operations
Logic operations are much more useful if combined. For example, T||(T&&F) returns true. By combining logic circuits, one can create different truth tables. One quick way to generate these, as well as a display of schematic diagram of the circuit, is WolframAlpha, a computation engine. An example can be found here.
Short-Circuit Evaluation
For more information, see Short-circuit evaluation on Wikipedia. |
Some boolean operators can be evaluated more efficiently through something called short-circuit evaluation, where the second input of an operator is evaluated only if the result of the operation cannot be determined from the first input alone. For example, conventional (eager) evaluation would need to evaluate both inputs of an AND operator to find the output, but short-circuit evaluation only ever looks at the second input if the first input is true, since the overall output of the operation must be false if the first input is false.
Execution Differences
Short-circuit evaluation is more efficient than short-circuit evaluation, but it may yield a different result if evaluating an operator does something other than return true or false. This was the case in Scratch 1.4, where evaluating an operator could cause a script error, which would stop the project instead of returning a result. Consider the following case:
<<[1] = [0]> and <((0) / (0)) = [0]>>
Because the <<> and <>>
block uses eager evaluation in Scratch 1.4, the script would cause a script error. However, if it had used short-circuit evaluation, the <<> and <>>
would instead return false without causing a script error, because the first input is false, meaning that the overall result of the AND operation must be false regardless of the second input.
Workaround
Despite the fact that the () and () and () or () blocks do not have native short-circuit evaluation in Scratch,[1] there are rather simple workaround for short-circuit AND and OR evaluation:
For AND:
if <condition1::grey> then set [result v] to <condition2::grey> // if condition1 is true, then this will return whether condition2 is also true - since condition1 is true, the entire gate effectively depends on whether condition2 is true. else set [result v] to [false] // otherwise, this will directly return false. end
For OR:
if <condition1::grey> then set [result v] to [true] // if condition1 is true, this will directly return true. else set [result v] to <condition2::grey> // otherwise, this will return whether condition2 is true - since condition1 is false, the entire gate effectively depends on whether condition2 is true. end