## Want to keep learning?

This content is taken from the Raspberry Pi Foundation & National Centre for Computing Education's online course, Understanding Maths and Logic in Computer Science. Join the course to learn more.
1.3

## Raspberry Pi Foundation

Skip to 0 minutes and 3 seconds You’ll recognise this symbol from the on/off switches of electrical items in computers. It looks like a one and a zero. This symbol also represents how computers work by asking “True or False”. The one means “True”. It symbolises a current flowing through a switch when it’s on. The zero means “False”. Zero represents a lack of current when the switch is off. By combining millions of these on/off switches together, a computer can process complex calculations. You can explore the basic theory of these calculations by using logic gates to represent the flow of electricity through a transistor. A logic gate is an electrical circuit with inputs and outputs.

Skip to 0 minutes and 46 seconds It receives incoming electric currents, determines whether they are “True” or “False”, on or off, and sends a new outgoing electric current depending on what it finds. So first of all, you need to go to circuitverse.org in your browser, and you’ll end up with a page that looks a little bit like this. And then if you click on the “Launch Simulator” button right here in the middle, you will get to the page. I’m just going to zoom in a little bit so that you can see what’s going on quite well here. So first of all, we’re going to grab two inputs. So you can position them wherever you like on the grid. Just drag them out.

Skip to 1 minute and 23 seconds I’m going to grab another output here, and I’ll position this over on the side, leaving a little bit of space for my logic gate, and I’m going to use an AND gate here. Let’s put my AND gate in. Then we just need to join them up, so you can use these little green dots to join up your gates. There we go. I’m going to join up my inputs as well. You can use more than one line if you like if you want to make it a bit neater. And then you can see that because both of my inputs are zero, my output is also zero over here. If I turn one of the inputs on, my output still remains zero.

Skip to 2 minutes and 4 seconds And we’ll just show you with the other one on as well just to demonstrate. It doesn’t matter. Either of them being on still results in the output being zero, so you need to have both of your inputs on to get an output of one. But what happens if you replace your AND gate with an OR gate? I’ll let you investigate and find out.

# True and False

In this step we are going to introduce the concepts of True and False in logic and explain how these are denoted as signals of 1 and 0. We will also have a go at building simple versions of the two most basic logic gates, AND and OR.

### True and False

You will already know that computers are very good at doing millions of calculations very quickly, but did you know that they are always dealing with one basic question: True or False (which essentially means on or off)? You’ve seen the symbol on the switch of most electrical items and computers that looks like this:

This basic switch symbol, which looks like a 1 and a 0, has come to mean on/off and could be interpreted as representing the entire way in which computers work. The 1 means True (or on) and the 0 means False (or off). If we take this right back to the physical way in which computers work, the 1 represents a high voltage and the 0 represents a low voltage. These voltages control transistors - electrical components which act as an electrically controlled switch.

Before we start looking at the way in which we express this, it may be useful to consider this table, which lists four of the ways that computer scientists and educators express this same concept.

True False
On Off
Current flowing Current not flowing
1 0

In programming we refer to variables which can only have two possible states, True or False, as having the Boolean data type.

By combining millions of these on/off switches, the computer can handle very complex conditions to tackle very complex calculations. But let’s start at the beginning, by considering how we handle one condition.

### Logic Gates

We can represent this process in diagrammatic form by using logic gates to represent the flow of electricity through a transistor. A logic gate is an electric circuit, usually with two inputs and an output. It receives two incoming electric currents, determines whether they are True or False (on or off), and sends on a new, outgoing electric current depending on what it finds. The process of determining the output based on the inputs like this is known as Boolean logic.

#### The AND gate

This is an AND logic gate, which represents a small circuit configured in a particular way to give a particular result. You can see the inputs on the left represented by lines going into the AND logic gate symbol. What do you think this means?

The AND gate has two inputs and one output. Both have to be True (or on) to produce an output of True. In the example above both inputs are set to True, which results in a True output.

What if only one was True? Like this:

What do you think the output (denoted by X here) would be this time?

In the case of an AND gate, if only one of the inputs was switched on then the output is 0. And if neither input is switched on, you would be right in assuming that there is again an output of 0. Both the first input and the second input have to be 1 to achieve a True (or 1, or on) output.

#### The OR gate

So, an AND gate only switches on when both inputs are on. What about if we want the output to be on when at least one of the inputs is on? This is called an OR gate. In order for the gate to produce an output of True, there must be a current from at least one input. Can you predict what the output would be if the inputs were 0 and 1 in an OR gate?

Why not use Circuitverse to test if your predictions are correct? Circuitverse is a very useful and free online tool which simulates logic circuits. Go to Circuitverse and begin by clicking ‘Launch Simulator’. Choose two inputs, one output, and the OR gate, and then try toggling the different options to switch the two inputs on and off. By clicking on the dots on the side of the symbols and dragging your mouse you can join the inputs to the logic gate symbol and the logic gate symbol to the output to simulate the flow of current in the circuit. You will see that the output changes depending on your choice of input.

Your OR logic gate, with inputs of 1 and 0, should look like this:

In the next step we will look at truth tables, which are tables that provide complete summaries of the inputs and outputs of logic gates and circuits.