Main content

## Computers and the Internet

### Course: Computers and the Internet > Unit 2

Lesson 3: Logic gates and circuits# Logic gates

We send information through computers using wires that represent 1s and 0s. Computers need a way to manipulate those 1s and 0s, so that they can eventually do more complicated operations like calculating the 50th digit of pi.

Computers use

**logic gates**to transform the 1s and 0s from input wires. A logic gate accepts inputs and then outputs a result based on their state.### NOT gate

The simplest gate is the

**NOT**gate, also known as an**inverter**. It accepts a single input and outputs the opposite value.If the input is 0, the output is 1:

If the input is 1, the output is 0:

Inverting a value may seem like a trivial operation, but in computers, we can build highly sophisticated logic by combining many small operations.

### AND gates

All other logic gates operate on multiple inputs. The

**AND**gate accepts two wires, and if both of those wires are "on" (representing 1), it outputs 1:If either of those wires are "off" (representing 0), then it outputs 0:

AND is a

**Boolean**operation, an operation that takes values that are either "true" or "false", and then outputs "true" or "false" based on a logical manipulation of those inputs. In logic gates, we consider 1 to be true and 0 to be false.One way to understand Boolean operations is to make a

**truth table**of all the possible inputs and outputs. Here's a truth table for the AND gate:Input A | Input B | Output |
---|---|---|

True | True | True |

True | False | False |

False | True | False |

False | False | False |

Notice there's only

*one*row where the output is true - that happens only when both inputs are true.We can also write the truth table using 1 and 0 to think of it in more computer-y terms:

Input A | Input B | Output |
---|---|---|

1 | 1 | 1 |

1 | 0 | 0 |

0 | 1 | 0 |

0 | 0 | 0 |

Once again, there's only one row where the output is 1.

What if we want the opposite, a gate that outputs 1 almost every time? There's a gate for that!

### OR gates

The

**OR**logic gate accepts two inputs, and as long as*either*of those inputs is a 1, it outputs a 1:Let's look at the truth table for OR gates:

Input A | Input B | Output |
---|---|---|

1 | 1 | 1 |

1 | 0 | 1 |

0 | 1 | 1 |

0 | 0 | 0 |

As you can see, the output is a 1 for every row except one. The only time that an OR gate outputs a 0 is if both inputs are 0.

### Thinking logically

It might help you to think about what these gates do in human speak.

Consider this OR gate. The first input represents "it's raining outside", the second input represents "we're going hiking", and the output represents "we should wear boots".

If it's raining outside OR we're going hiking, then we should wear boots. We can say that more verbosely: If it is true that "it's raining outside" OR it is true that "we're going hiking", then it's true that "we should wear boots".

That means that if it's raining outside (regardless of whether we're going hiking), we should wear boots. If we're going hiking (regardless of whether it's raining), we should wear boots. The only time we shouldn't wear boots (according to this gate) is when it's not raining outside and we're not going hiking.

That was a vast simplification of the complicated logic that humans compute when we decide whether to wear boots, but it shows you that logic is something that relates very much to the "real world" outside the computer.

### Behind the abstraction

The logic gates that we've presented here are abstract representations of real devices. A logic gate describes

*any*device that can take in values of 0 or 1 and output a 0 or 1 according to its truth table.In most modern computers, logic gates are built using transistors combined with other electrical components like resistors and diodes. Those are all wired together to make sure that they transform the inputs in the way we expect.

With a little electronic equipment, you could put together your own logic gates, like this video shows. Here are the homemade circuits for AND and OR gates from that video:

If you break into your own computer (don't!), you won't see anything like that. Our powerful computers now require billions of gates, so manufacturers have figured out how to make electronic parts very small. My own mac has 5.6 billion transistors that are just 14 nanometers wide.

We can understand and use logic gates without needing to know exactly how they're implemented. That's the power of an abstraction, enabling us to ignore the details and focus on the higher-level functionality.

### Further learning

In fact, the NAND and NOR gates are known as universal logic gates, which means that we can build any of the other gates with only NAND gates or only NOR gates. Computer hardware manufacturers prefer to use NAND gates due to their universality and ease of fabrication, so your computer likely has millions of NAND gates inside its circuitry.

🙋🏽🙋🏻♀️🙋🏿♂️Do you have any questions about this topic? We'd love to answer— just ask in the questions area below!

## Want to join the conversation?

- I have taken logic before and not coincidentally, see that logic gates share many of the same properties as logic like NOR gate (negation), AND gate (conjunction) and the OR gate (disjunction). However, I would like to know if there will be a conditional gate or a biconditional gate. Thanks!(6 votes)
- If I understand biconditional correctly, then yes, there is a corresponding logic gate. It's called
*XNOR*. Here is the truth table for it.

A | B | OUT

0 | 0 | 1

0 | 1 | 0

1 | 0 | 0

1 | 1 | 1

Basically, if the two inputs are the same, then the output will be 1 (true). Otherwise, the output will be 0 (false).

There is also a conditional gate. According to wikipedia, it is called*IMPLY*. Although I have not heard of many uses of this gate.

Hope this helps! (:(16 votes)

- Could you classify a computer as a logic gate itself? Because all computers really do is take in 0s and 1s, and then outputs some 0s and 1s. Isn't that the same definition of a logic gate?

Thanks for responding! (:(1 vote)- From the author:Interesting idea! It's true that a computer takes in binary data and outputs binary data. However, it does more than a logic gate. A logic gate is a device performing a
**Boolean logic**operation on one or more binary inputs and then outputs a**single**binary output. Computers perform more than simple Boolean logic operations on input data, and they typically output more than a single binary digit.

Computers definitely use logic gates for all their calculations, however, so they are incredibly dependent on logic gates!(11 votes)

- Can you expand on this Boolean Operation? What are some (real-life) examples? Are all the gates Boolean... (how)?(4 votes)
- From my understanding, boolean gates take two values/bits(1s and 0s) and output an appropriate result.

For example, imagine you had a machine that needed to be precise with its results(if the result is off by a 0.00001, then the machine wouldn't work). For your output to be precise, you would need all of your inputs to be as accurate.

This is where you would then use a logic gate(specifically an AND Gate) to make sure that the inputs are correct.

An AND Gate because this gate only works(outputs a 1) when all of the inputs gates are correct(When all gates are 1s).

What you want your machine to do will influence which gate you choose and how you will use it.

I hope this helps.(1 vote)

- How do companies decide on the number of logic gates for a computer/ how do logic gates define the quality of the computer?

If logic gates are how computer makes decisions for outputs, and there billions of possible inputs, how does a computer scientist decide there are enough in a system?

How do they decide which inpiuts correspond to a certain gate? How are the gates implemented for a keyboard, for example? Is there one logic gate for each key?(4 votes) - does all logic gates used ina computer and what do they operate about(2 votes)
- You have a multitude of different logic gates that operate within a computer. These gates are used in combinational and sequential circuit design. The logic gates include: AND, OR, NOT, NAND, NOR, XOR and XNOR.

The**AND**gate takes two inputs and evaluates to true (i.e. outputs a '1') when both of its inputs are true, or false otherwise.

The**OR**gate takes two inputs and evaluates to true when either one of its inputs are true (or if both inputs are true - this is conventionally named "inclusive or").

The**NOT**gate takes in one input and inverts that input (i.e. it will flip a '1' to a '0' and a '0' to a '1').

The**NAND**gate is essentially an**AND**gate whose output is then fed into a**NOT**gate. Therefore, it is true in all cases*except*for when both inputs are '1'.

The**NOR**gate is essentially an**OR**gate whose output is then fed into a**NOT**gate. Therefore, it is true only in the case where both inputs are zeroes (the only case that would have made an**OR**gate output a '0').

The**XOR**gate is true when the inputs are opposite of each other, but false when they are equal. For example, the two inputs '1' and '0' would produce a true ('1') output, but the two inputs '1' and '1' or '0' and '0' would produce a false ('0') output (this is conventionally named "exclusive or").

The**XNOR**gate follows the same conventions as above, and acts like an**XOR**gate whose output is then fed into a**NOT**gate. Therefore, an**XNOR**gate is true only when the two inputs are*the same*.

Logic gates are the fundamental building blocks that you need to understand in order to understand computer system design, do take the time to familiarize yourself with the gates and continue to ask questions if you are curious or need any help!(4 votes)

- I would appreciate a further explanation of how to apply the three basic gates in real life computing(2 votes)
- From the author:Computer programmers often use AND/OR/NOT inside computer programs, to decide whether a program should execute one set of instructions or the other. For example, a weather website might have logic like "if the humidity is low and the wind speed is high, send a warning to customers in wildfire zones." You can learn more about programming with logic here: https://www.khanacademy.org/computing/ap-computer-science-principles/programming-101/boolean-logic/a/compound-booleans-with-logical-operators(4 votes)

- Fortnite battle pass(2 votes)
- As in XOR (Puts out a 1 if there is only 1 true input). Will it answer 1 if both the inputs are true or 1?(0 votes)
- You can think of XOR as an inequality detector. If you have a 0 with a 1, the two inputs are not equal and hence you output 1. Alternatively, if you have 0 with 0 or 1 with 1 (as you mention), you output 0.(3 votes)

- If Im designing a digital circuit, and there's an input A, is there any gate I can use to check at the beginning of my circuit if A is true or false? Because I would only feed A through the rest of my gates if its true, but I don't know how to "check" if its false.(1 vote)
- Can you explain to me Building blocks that use NAND and NOR gates?(1 vote)