From Test-Scratch-Wiki

Translate: - English 

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 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.

Warning Tip: Operator names are usually capitalized.

AND

The AND operator is very simple. It returns true if both conditions are true:

AND
A B Result
T T T
T F F
F T F
F F F

NAND

The NAND operator is the opposite of the [[Eng:#AND|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) and (condition 2)>>
NAND
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:

OR
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 [[Eng:#OR|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) or (condition 2)>>


NOR
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) = (condition 2)>>


XOR
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 [[Eng:#XOR|XOR]] operation.

It is not available with a block in Scratch, but can be simulated with the following script:

<(condition 1) = (condition 2)>


XNOR
A B Result
T T T
T F F
F T F
F F T

It is sometimes called XAND.

NOT

NOT is different from the other operators in that it only takes one parameter. It inverts the value.


NOT
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

BYOB3.png This article or section uses images made with BYOB, 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>
 if <condition2>
  set [T/F v] to [true]
 end
end

For OR:

set [T/F v] to [false]
if <condition1>
set [T/F v] to [true]
else
  if <condition2>
   set [T/F v] to [true]
  end
end

Snap!, however, uses short-circuit evaluation, as shown:

Testing evaluation order in Snap!

Also, a multiple-input AND that uses short-circuit can be created as shown:
SSand.png

Note 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 our block to be short-circuit; otherwise all the inputs would be evaluated anyway.

See Also

External Links