Loading...
Hello, my name is Dr.
Kas.
Welcome to computing.
I am so pleased that you have decided to join me for the lesson today.
In this lesson, we are going to be looking at creating an XOR function.
Welcome to today's lesson from the unit programming subroutines.
This lesson is called creating an XOR Function.
By the end of today's lesson, you should be able to write a function that replicates the output of an XOR gate.
Some of the key words that we will be using in today's lesson are logic gate.
Logic gate, an electronic component that carries out a logical operation.
Truth table, truth table, a table showing the outputs for all possible combinations of inputs to a logic gate.
XOR, XOR a Boolean operation that outputs true if either but not both of the input values are true.
This lesson is split into two parts.
Explain the output of an XOR gate, and write a function for an XOR operator.
We're going to start by looking at the first section where you will learn how to explain the output of an XOR gate.
There are three fundamental logic gates.
Each logic gate can be represented as a symbol as shown in the images below.
The first image shows you the representation of an AND gate.
The second image shows you a representation of the OR gate.
And the third image shows you the representation of a NOT gate.
Logic gates take one or more inputs.
They produce an output.
As you can see in this image below two inputs to this logic gate, which is an illustration of an and gate gives you an output.
Inputs and outputs are either true or false, so they can only have two states.
This is sometimes written as one for a true, and zero for a false.
A truth table shows the outputs for all possible combinations of inputs to a logic gate.
All possible inputs are written in a column.
Each input is then tested and the output recorded to complete the table.
An example of a truth table is shown here on the slide.
Let's investigate this a little more.
Let's start with the AND gate.
For this logic gate.
The output is true when both inputs are set to true.
For example, if it is sunny and it is the weekend, then we can go to the beach.
The diagrammatic representation of the AND gate is shown here in the image.
The truth table shows the different combinations of inputs that are possible and the output for each set of inputs.
So as we said, when both inputs are true, only then is the output true.
So you can see when both inputs are false, the output is false.
Both inputs are zero, the output is zero.
When either of the inputs is zero, the output is still zero.
The output is one only when both inputs are one.
For an OR gate, the output is true when one or both inputs is set to true.
For example, if the shop has cola or lemonade, then we'll buy a drink.
The diagrammatic representation of the OR gate is shown here on the slide.
And the truth table shows all possible combinations of inputs and the corresponding outputs.
You can see from the truth table that the output is true or the output is one, when either one of the inputs is true or when both the inputs are true.
When both the inputs are false or both the inputs are zero, the output is set to false or zero.
The NOT gate has the output set to true when the input is false.
For example, if it is not the weekend, then I go to school.
The diagrammatic representation of the NOT gate is shown here on the slide and the truth table shows the input and the output combinations.
As the NOT gate has just one input going in when the input is true or one, the output is false or zero.
When the input is false or zero, the output is true or one.
Right, let's do a quick check.
Which logic gate's output is true when one or both inputs are true? Why don't you pause the video here and have a quick think.
Did you choose option B, OR? You were right So as we saw the OR gate has its output set to true when either of the inputs or both inputs are set to true, well done.
Jacob says, But what if I want a logic gate that is true when either input is true but not both?" Jun says, Well, you need an XOR gate!".
Let's look at this a little more.
The output of an XOR gat is true when either but not both of the inputs are true.
XOR stands for exclusive OR.
And the representation, the diagrammatic representation is shown here on the slide.
As you can see in the truth table, when either of the inputs A and B are true.
So if input A is true and B is false, so input A is one, B is zero, the output is one.
When input A is false or zero and input B is true or one, the output is one.
However, when both inputs are false, so both are set to zero, the output is set to zero.
Also when both inputs are set to true, that is both inputs are one, the output is still false or zero and that is your XOR gate.
Let's do another check.
Which is the correct truth table for an XOR logic gate.
Post the video here.
Have a look at the three options and think about this.
I am sure you will be able to work this one out.
Well done.
Did you come up with option A? That is the correct answer.
As you can see, the output is set one, only when either of the inputs is true.
When both inputs are true, the output is false and both inputs are false.
The output is still false.
Well done.
Right, that brings us to the end of this section.
Let's make sure you've understood what we covered in this section by doing this task.
Task A is split into three parts.
The first part asks you to identify which of the following gates is an XOR gate.
Part two asks you to complete the truth table for an XOR gate.
Finally, part three asks you to explain the output of an XOR gate.
Pause the video here, have a look at these three parts, think about what we covered in the section and have a go at answering the three questions.
I am sure you'll be able to work these out.
Well done on working through these three parts.
Now let's look at these solutions.
So the first part, the third option shows you what an XOR gate looks like.
For the second part where you had to complete the truth table for an XOR gate, this is what the solution is.
So when both inputs are zero, the output is zero.
When either of the inputs is one, the output is one, and when both inputs are one, the output is zero.
Finally, for the third part where you had to explain the output of an XOR gate.
The output of an XOR gate is true when either but not both of the inputs are true.
Excellent, let's move on.
Right, now we look at the next section where you will learn how to write a function for an XOR operator.
The three fundamental Boolean operators are AND, OR and NOT.
In Python, these are written in words as and or and not.
These are sometimes also called logical operators.
Just like logic gates, Boolean operators require their inputs to be either true or false.
Python does not have a built-in operator for XOR unlike those for and or and not.
You need to create your own function to evaluate XOR.
This here is a function for the AND operator.
The example code is given in the image here.
What do you think will the AND function return? Pause the video here and have a quick think.
Did you come up with true? You were right.
The AND function will return true.
If you just quickly look at this programme, there's a function, AND function is being defined with two parameters A and B.
Within the function, the if statement checks whether A is true and B is true.
If both of them are true, then the output C is set to true, otherwise it is set to false.
And then the value of C is returned back to the main programme.
Within the main programme there are two variables, one and two.
One is set to the value of the equality between four and four.
So a double equal to operator is the equality operator in Python.
So one is set to the value of the equality between four and four, which obviously is going to be true.
Similarly, two is said to be the equality between two and two, which again is going to be true.
And then on line number 11, the output of the AND function is printed.
The AND function is called with the arguments one and two, the output will be true.
Because you are passing both arguments as true to the AND function.
So well done on working that one out.
This here is a function for an OR operator.
The snippet of code is given in the image below.
What do you think the OR function will return? Pause the video here and have a quick think.
Okay, did you come up with the answer true? Very well done.
Let's again, very quickly look at the code.
So the OR function is being defined with two parameters, A and B, and within the function the check, the if statement, the if statement checks.
If A is true or if B is true, then set C to be true.
Else C is set to false.
And then the function returns the value of C.
Within the main programme, there are two variables that are being declared, one and two.
One is set to the equality between four and four, which is going to be true.
And two is set to the equality between two and two, which is also going to be true.
And then on line number 11, you print the output of calling the OR function with the arguments one and two.
Now, because both one and two are set to true within the OR function, the if statement is going to set the output C to true and return this value back to the main programme.
Therefore the output printed will be true, well done.
Right, let's do a quick check before we move on.
This is a function for an AND operator.
The snippet of code is shown here in the image.
What will the AND function return? You've got two options, either true or false based on what we just covered in the last two slides.
Pause the video here, have a think and try and work this out.
I'm sure you'll be able to.
The answer os going to be false.
If you look at the programme, the AND function is being called with the parameters A and B.
Within the AND function, if A is true and B is true, then C is set to true else C is set to false.
And then the value C is returned back to the main programme.
Line number eight starts the main programme.
Here we are defining, we are declaring two variables, one and two.
One is set to be the equality between four and four, which is going to be true.
Two is said to be the equality between two and four, which is going to be false.
Then on line number 11, the output is being printed and the output is that of the AND function being called with the arguments one and two.
Now one is true, two is false.
So when the AND function is called the if statement it's not going to be true.
So the control passes to the else statement on line number four and the value of C is going to be set to false.
When this is returned back to the main programme, the output is printed as false.
Well done on working that one out.
Let's move on.
This here is a function for an OR operator.
What will the OR function return Pause the video here and try and work this out.
Solution will be a, true.
Let's see how we came up with that.
So within the programme you have the OR function being defined with the parameters A and B.
Within the OR function the if statement checks to see if either A or B is true.
Then the output C is set to true otherwise C is set to false.
This value of C is then returned back to the main programme.
Within the main programme, there are two variables being declared.
One is set to the equality between four and four, and the variable two is set to be the equality between two and four.
On line number 11, the result of the call to the OR function with the arguments one and two is being printed.
Now when the OR function is called with the arguments one, which is true, and two which is false.
Within the OR function, the if statement checks is A true.
Is the first parameter true? Yes, is the second parameter true? No, it's not, but it doesn't matter, if either of the parameters is true.
C will be set to true, which is what happens, and then this value is returned and then printed on line number 11.
Therefore the answer is true.
Well done on working that one out.
Jun here says, "I have created the pseudo code for an XOR function." The pseudo code that Jun has created, the snippet of code is shown here in the image.
So this subroutine shows you what we have seen in the truth table for an XOR function.
If either of the inputs is true, the output is true.
If both inputs are set to true, the output is false.
If both inputs are set to false, the output is again false.
So that's what Jun has created.
So within a subroutine, once he has checked, once he has written the if statements, he ends the if statement, returns the value of the output C and the sub-routine ends.
So that brings us to the end of this section.
Just to make sure that you have understood what we covered in this section, let's attempt a quick task.
So task B is split into four parts.
The first part asks you to use Jun's pseudo code to create the XOR function in Python.
The second part asks you to add the following variables after the function.
one, which is set to the equality between four and four.
And two, which is set to the inequality between two and four.
The third part asks you to add this line to call the function and print the results.
So you need to add print XOR function one comma two to your programme.
Finally, part four asks you to test the output of your programme by amending the inputs to match the truth table below and check the output is as expected.
Pause the video here, have a look at these different parts.
Think about how to work these out and give these questions a go.
I'm sure you'll be able to work the different parts out.
Okay, so let's look at a possible solution.
So the first three parts.
In the first part, you were asked to define a function, create the XOR function that Jun wrote a pseudo code for.
The second part, you were asked to create two variables, define two variables, one and two.
And in the third part, you were asked to include the print statement that would print the output of the function call to the XOR function that you created in the first part.
So let's look at this programme very quickly.
The first part where you had to define the XOR function, the XOR function is being defined.
That takes two parameters, A and B.
Within the XOR function as Jun's pseudo code was showing the if statement checks, if parameter A is set to true and parameter B is set true, then C, the output C should be set to false.
Then comes the elif statement which checks if A is set to false and B is set to false, then the output C is set to false.
Finally, the else statement sets the output C to be true.
In the main programme, starting at line number 10, you define your variables.
The two variables, one and two.
One is set to the equality between two values, between four and four.
And two is set to the inequality between two and four.
Now, variable one will have the value true because the equality between four and four is true.
Variable two will have the value true again because it is set to the inequality between two and four, the two values are not equal.
So variable two will have the value true.
Finally, line number 13, you include the print statement which prints the output of the function call to the XOR function.
The XOR function is being called with the arguments one and two.
Let's now look at the fourth part.
The fourth part asks you to test the output of your programme by amending the inputs to match the truth table and check the output is as expected.
So in the first case where one is set to the equality between four and four, as I said, it should be the value is true and the value of the variable two is also true because it is the inequality between the values two and four.
So the two values are not equal.
So the value of the variable two will also be true.
When this is passed to the XOR function.
If you just look at the programme again, when this is passed to the XOR function, the XOR function is now being called with the arguments A and B, which both have the value true.
So the if statement, the first if statement where it's checking is A true and is B true, will then set the value of C to be false.
So the return value will be false, which means the print statement, the text output should be false.
Next, the values of the variables are being set to.
So value of variable one is being set to the equality between four and two.
And the value of variable two is being set to the inequality between two and four.
Which means the value of one will be false because four and two are not equal.
The value of the variable two will be true because it is the inequality between two and four, which is true, the two values are not equal.
So when this is now passed to the XOR function, if you go back to the function again, when this is passed to the XOR function, the else statement comes into play because the first statement is not true because the XOR function is being called with the parameters A, which is false, and B, which is true.
So the first if statement, obviously the condition is not satisfied.
The elif statement, the condition is not satisfied, which means the control, the programme control goes to the else statement, and then the value of C is set to true.
This is passed to the print statement, which means the text output will be true.
Well done on attempting these parts.
Really well done on finishing the section you've done brilliantly.
To summarise, a logic gate is a fundamental component of a digital circuit.
A truth table shows all possible combinations of inputs and the output that a circuit will produce.
An XOR logic gate is true when either input is true, but not both.
Unlike AND OR and NOT, Python does not have a built-in operator for XOR.