If you're seeing this message, it means we're having trouble loading external resources on our website.

If you're behind a web filter, please make sure that the domains *.kastatic.org and *.kasandbox.org are unblocked.

### 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$:
What do you think this combination will output?

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 AInput BOutput
TrueTrueTrue
TrueFalseFalse
FalseTrueFalse
FalseFalseFalse
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 AInput BOutput
$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$:
Based on that description, what value do you think this OR gate outputs?

Let's look at the truth table for OR gates:
Input AInput BOutput
$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

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

Computers also use other logic gates like NAND, NOR, and XOR. Each logic gate operates on inputs in a slightly different way; they output $1$ and $0$ in different situations.
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.

## 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!
• 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! (:
• 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?
• 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!
• Can you expand on this Boolean Operation? What are some (real-life) examples? Are all the gates Boolean... (how)?
• 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)
• does all logic gates used ina computer and what do they operate about
• 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!