☰ Year plan · Communication Technology

The NOT gate

Week 13 · Unit 2 Topics 7–9: the NOT gate and its simulation projects with VB.NET and PHP · 3 lessons
3

NOT on a web page, and the three gates side by side

ICT Secondary 3 Term 1, Unit 2, Topics 7–9: pp. 65–81
GoalRead the NOT page, avoid the traps of NOT in code, and compare the three gates in one table.
New words
! · PHP’s NOT: turns true into false and false into trueNot · VB.NET’s NOT: for True and False it reverses the valuebitwise · working on the individual bits of a whole numbersummary table · one table that puts the three gates side by side
Explanation

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.

GateInputsRowsOutput is 1 whenFormula
ANDtwo or more2ⁿall inputs are 1Y = A·B
ORtwo or more2ⁿat least one input is 1Y = A + B
NOTone2the input is 0Y = ¬A
Worked examples
Php
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']);
Output
off lit
on unlit
off lit
off lit
The NOT page as a function. The output lines are for a first visit, on, off, and the text maybe.
Php
1echo "[", !1, "]\n";
2echo "[", (int)!1, "]\n";
3var_dump(!1);
Output
[]
[0]
bool(false)
The trap in PHP: the empty text and the cast that shows 0.
Vb
1Dim t As Boolean = True
2Dim n As Integer = 1
3Console.WriteLine(Not t)
4Console.WriteLine(Not n)
5Console.WriteLine(1 - n)
Output
False
-2
0
The trap in VB.NET, inside a small console program: Not on a Boolean, Not on a whole number, and the safe way 1 − n.
ABA∧¬B
000
010
101
110
NOT combined with AND. The lamp is lit only when A is 1 and B is 0.
Class activity

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.

Practice
1
What does echo !1; print in PHP?
2
What does Not 1 give in VB.NET when 1 is a whole number?
3
Which PHP expression shows 0 for the bit 1?
4
Which of the three basic gates has only one input?