NOT on a web page, and the three gates side by side
The NOT page has one drop-down list for the switch, an Apply button, and a lamp line that shows the opposite of the choice. In the function below, the switch is on only if the list sent exactly on; then the lamp is unlit, and otherwise lit. The book prints no code for this page, so the function is our own. Now the traps. In PHP the sign ! reverses a condition, but echo prints false as an empty text, so echo !1 prints nothing. To see a 0, write (int)!1. In VB.NET the word Not reverses True and False, and they print as True and False. But on a whole number Not works on the bits: Not 1 is −2, not 0. So never write Not 1 to mean “NOT of the bit 1”. Use a Boolean value, or write 1 − a. Last, NOT can be combined with another gate. The column A AND NOT B is 1 only when A is 1 and B is 0. And the summary table puts the three basic gates side by side.
| Gate | Inputs | Rows | Output is 1 when | Formula |
|---|---|---|---|---|
| AND | two or more | 2ⁿ | all inputs are 1 | Y = A·B |
| OR | two or more | 2ⁿ | at least one input is 1 | Y = A + B |
| NOT | one | 2 | the input is 0 | Y = ¬A |
1function notPage(array $post): string { 2 $switch = "off"; 3 if (isset($post['apply'])) { 4 $switch = (($post['list1'] ?? "") === "on") ? "on" : "off"; 5 } 6 $lamp = ($switch === "on") ? "unlit" : "lit"; 7 return "$switch $lamp"; 8} 9 10echo notPage([]), "\n"; 11echo notPage(['apply' => 'Apply', 'list1' => 'on']), "\n"; 12echo notPage(['apply' => 'Apply', 'list1' => 'off']), "\n"; 13echo notPage(['apply' => 'Apply', 'list1' => 'maybe']);
off lit on unlit off lit off lit
1echo "[", !1, "]\n"; 2echo "[", (int)!1, "]\n"; 3var_dump(!1);
[] [0] bool(false)
1Dim t As Boolean = True 2Dim n As Integer = 1 3Console.WriteLine(Not t) 4Console.WriteLine(Not n) 5Console.WriteLine(1 - n)
False -2 0
| A | B | A∧¬B |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 0 |
| 1 | 0 | 1 |
| 1 | 1 | 0 |
First predict, then check with a pencil: what do echo (int)!0; and echo (int)!!1; print in PHP, and what does Console.WriteLine(Not False) print in VB.NET? Then write the truth table of (NOT A) AND B.