☰ خطة العام · تكنولوجيا المعلومات والاتصالات (بالإنجليزية)

Gates in decisions: NAND, NOR and XOR

الأسبوع 14 · الوحدة ٢ الموضوع العاشر: توظيف البوابات المنطقية في اتخاذ القرارات الحياتية، خاتمة الكتاب، وبوابات NAND وNOR وXOR المذكورة في مقدمة الوحدة · 3 دروس
2

Choosing the gate for a rule: AND, OR or NOT

ICT Secondary 3 Term 1, Unit 2, Topic 10: pp. 82–86
GoalChoose AND, OR or NOT for a rule, write its table and formula, and test the rule in PHP and VB.NET.
New words
AND rule · all the conditions are neededOR rule · at least one condition is enoughNOT rule · the result is the opposite of the conditionmixed rule · a rule that needs two gates, such as A AND NOT B
Explanation

Job 3 asks: how do the conditions combine? If all of them are needed, the gate is AND, with the formula Y = A·B. If at least one is enough, the gate is OR, with Y = A + B. If the result is the opposite of the condition, the gate is NOT, with Y = ¬A. Our three decisions fit in turn. The trip needs a signed slip and a free seat: AND. The library fine is waived if the book was on time or the card is gold: OR. The night lamp is on when it is not daylight: NOT. Some rules need two gates. A garden sprinkler runs when the soil is dry and it is not raining; with A for dry soil and B for rain, the formula is Y = A·¬B. After the table comes the test. A function in PHP or VB.NET must give the same answers as the table, row by row. Write the function, run it on every row, and compare.

The rule saysGateFormula
all the conditions are neededANDY = A·B
at least one condition is enoughORY = A + B
the result is the opposite of the conditionNOTY = ¬A
one condition and the opposite of anotherAND with NOTY = A·¬B
Worked examples
Php
1function tripOk(bool $slip, bool $seat): bool {
2    return $slip && $seat;
3}
4function feeWaived(bool $onTime, bool $gold): bool {
5    return $onTime || $gold;
6}
7function nightLight(bool $daylight): bool {
8    return !$daylight;
9}
10
11var_dump(tripOk(true, false));
12var_dump(feeWaived(false, true));
13var_dump(nightLight(true));
14var_dump(nightLight(false));
Output
bool(false)
bool(true)
bool(false)
bool(true)
The three decisions as PHP functions. The output lines are the answers for the trip with a slip but no seat, the fine for a late book with a gold card, the lamp in daylight and the lamp at night.
Vb
1Function Sprinkler(dry As Boolean, raining As Boolean) As Boolean
2    Return dry And Not raining
3End Function
4
5Console.WriteLine(Sprinkler(True, False) & " " & Sprinkler(True, True) & " " & Sprinkler(False, False) & " " & Sprinkler(False, True))
Output
True False False False
The sprinkler as a VB.NET function, with a test line inside a small console program. The output line is the answer for the four rows (dry, raining) = (True, False), (True, True), (False, False), (False, True).
ABA∧¬B
000
010
101
110
The sprinkler table. Only the row with dry soil and no rain gives 1.
Class activity

A greenhouse fan should run when it is hot or the door is open. Write the inputs, the output, the gate, the formula and the truth table. Then write the PHP function.

Practice
1
A rule says “both keys must be turned”. Which gate is it?
2
A rule says “at least one of the three alarms rings”. Which gate is it?
3
Which formula fits “the heater runs when the room is cold and the window is not open”, with A for cold and B for open?
4
What does the sprinkler function return when it rains and the soil is dry?