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.
Tip: | 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
This article or section uses images made with Snap!, a Scratch Modification which allows script formatting. Differences include block multilining and zebra coloring. |
Generally, the better method for evaluating booleans is something called short-circuit evaluation. This means that arguments should be evaluated one by one rather than all at once. For example, consider (1=0)&&(0/0 = 1). Here, conventional (eager) evaluation would result in an error since it would try to evaluate 0/0. However, with short circuit evaluation, it will first evaluate (1=0) and see it is false. Since && returns true only if all values are true, it automatically returns false without evaluating the second argument.
Scratch uses eager evaluation. This can be tested with this script:
<<[1] = [0]> and <((0) / (0)) = [0]>>
which returns an error in Scratch. There is a rather simple workaround for short-circuit AND evaluation though:
set [T/F v] to [false] if <condition1::grey> then set [T/F 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 [T/F v] to <condition1::grey> // otherwise, this will directly return condition1 (false). end
For OR:
set [T/F v] to [false] if <condition1::grey> then set [T/F v] to <condition1::grey> // if condition1 is true, this will directly return condition1 (true). else set [T/F 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
Snap!, however, uses short-circuit evaluation, as shown:
Also, a multiple-input AND that uses short-circuit can be created as shown:
Note: | "bools..." is a variadic unevaluated boolean input, which means it allows multiple inputs (with a disclosure triangle), and the inputs are unevaluated, i.e. they are lambdas which should be run. This is important since the unevaluated property is what allows the block to be short-circuit; otherwise all the inputs would be evaluated anyway. |