--- Simple logic gates ---
2.1.11
Define the Boolean operators: AND, OR, NOT, NAND, NOR and XOR.
Teaching Note:
LINK Introduction to programming, approved notation sheet.
Sample Question (in this case, sort of...) - FORMER CURRICULUM
The method logic1(), shown below, returns the output from a particular logic gate.
The logic gate has two inputs, a and b.
//parameters a and b can only have the values of 0 or 1 public boolean logic1(int a, int b){ if(!(a==b)) return true;
else return false; }
(a) Identify the logic gate represented by the above method. [1 mark]
(b) Construct the method logic2(), which would similarly return the output from
a NAND gate. [2 marks]
JSR Notes - FORMER CURRICULUM - 4.2.1 Define the Boolean operators and, or, not, nand, nor, and xor, by drawing the appropriate truth table. :
There are two ways of "defining" boolean operators, one is with truth tables (see the next assessment statement) and one is the dictionary way. This assessment statement deals with the dictionary way. But even the "dictionary" way can be either in terms of logic gates in the circuitry of computers, or in terms of boolan programming constructs.
In Terms of Logic Gates
First of all, read and study pages 230 - 237 on the Red & Yellow IB CS text book.
The logic gate is on (has electricity flowing through it) when... | |
AND | both input switches are closed (i.e. electricity is flowing through both of them) |
OR | one input switch or the other is closed |
NAND | at most, one switch can be closed |
NOR | both input switches are open |
NOT | the input is not on (or the gate is turned off if its input was on) |
XOR | one switch or the other is closed, but not bot |
In Terms of boolean types: (These are the definitions asked for by the assessment statement.)
NOT - inverts the value of a boolean expression.
AND - both boolean variables must be true for the overall expression to be true.
NAND - the two boolean variables can't both be true. (alternatively, "returns the opposite boolean value of an AND expression").
OR - at least one of the boolean variables must be true for the overall expression to be true.
NOR - both boolean variables must be false for the overall expression to be true. (alternatively, "returns the opposite boolean value of an OR expression.")
XOR - only one of the boolean variables can be true for the overall expression to be true.
In Order of Operations -
Check out this great video explaining logic gates with dominoes. (Thanks Mr Walker!)
In Terms of Programming
--------> Do make a point of going to Netbeans and playing around with boolean logic again to remind yourself how it works.
5 package mrrayworthoctober; 6 7 /** 8 * 9 * @author John Rayworth 10 */ 11 public class ForBooleanOperators { 12 public static void main(String[] args) { 13 boolean a = false; 14 boolean b = false; 15 boolean c = false; 16 17 // AND 18 if(a && b){ 19 System.out.println("a and b are true"); 20 21 } 22 23 // OR 24 if(a || b){ 25 System.out.println("Either a or b are true (and possibly both are.)"); 26 } 27 28 // NOT 29 if(!a){ 30 System.out.println("a is the opposite of what it was first declared."); 31 } 32 33 // NAND 34 if(!(a && b){ { 35 System.out.println("It is not the case that a and be are true."); 36 } 37 38 // NOR 39 if(!(a || b)){ 40 System.out.println("It is not the case that one or the other (or both) are true"); 41 } 42 43 // XOR 44 if(a ^ b){ 45 System.out.println("Either a or b are true, but not both."); 46 } 47 } 48 49 } 50
Here is an example of a code fragment where the various boolean operators are used. (The idea here is to give a good example of a bunch of them, but particularly xor; in this
case the company will only hire the individual if they have over 10 years experience or over 10 years of university, but not both, because they could not afford to pay such an
employee.
21 if(companyDivision.equals("a")){ 22 if(yearsExp > 10 && yearsUni > 10){ 23 System.out.println("You are hired, congratuations."); 24 } 25 else{ 26 System.out.println("Sorry."); 27 } 28 } 29 else if(companyDivision.equals("b")) 30 { 31 if(yearsExp > 10 ^ yearsUni > 10){ 32 System.out.println(fullName + "You are hired, congratuations."); 33 } 34 else{ 35 System.out.println("Sorry."); 36 } 37 } 38 else if(companyDivision.equals("c")) 39 { 40 if(yearsExp > 10 || yearsUni > 10){ 41 System.out.println(fullName + "You are hired, congratuations."); 42 } 43 else{ 44 System.out.println("Sorry."); 45 } 46 } 47 else{ 48 System.out.println("Sorry, you did not type in a or b or c."); 49 } 50 }
A Little More Detail (though what will be assessed is essentially above)
-A processor contains logic gates, which are essentially circuits that can be open or closed.
-Closed circuit = complete circuit, electricity flows through. Open circuit = incomplete, electricity does not flow through.
-Closed logic gates represented by 1, open represented by 0
-There can be 2 logic gates next to each other
The logic gate is on (meaning it has electricity flowing through it) when:
-AND: Both input switches are closed. At the programming level, both statements must be true for an and statement to evaluate to true. (a && b) in Java
-OR: one input switch or the other is closed. At the programming level, one of the 2 statements are true (if the or statement evaluates to true). (a || b) in Java.
-NAND: Both input switches are not closed. !(a && b) in Java.
-NOR: !(a || b) in Java
-NOT: the input is not on (or the gate is turned off if its input was on). Not gate stops flow of electricity if it was flowing before, and makes electricity flow if it wasn’t. At the programming level, the statement is not true. !(a) in Java.
-XOR: one switch is closed, but not both. At the programming level, one statement or the other can be true. (a ^ b) in Java.
- c = a + b – (3*4) is known as an expression, which is evaluated to a single value through the order of operations. If a = 2 and b = 3, it simplifies to -7.
-The expression if((x+y)*z == 12 && q.equals(r)) evaluates to a Boolean in a similar fashion:
If q = “hello” and r = “world,” q.equals(r) evaluates to false, meaning the expression is simplified to (x+y)*z == 12 && false.
If x = 2, y = 3 and z = 4, the expression then simplifies further to 5*z == 12 && false, which simplifies to 20 == 12 && false, which simplified to false && false, which finally evaluates to false.
Jaime: Take a look at # 4 in the PBS YouTube CrashCourse series.