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.

Main content

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 π.
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:
A diagram showing a single wire going into a NOT gate. The wire is in the on state, signifying 1. The output wire from the gate is in the off state, signifying 0.
If the input is 1, the output is 0:
A diagram showing a single wire going into a NOT gate. The wire is in the off state, signifying 0. The output wire from the gate is in the on state, signifying 1.
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:
A diagram that shows two wires in the on state (1) going through a gate that says "AND", and outputting an "on" wire (1).
If either of those wires are "off" (representing 0), then it outputs 0:
A diagram that shows two wires in the off state (0) going through a gate that says "AND", and outputting an off wire (0).
Check your understanding
What do you think this combination will output?
A diagram that shows two wires, one in the on state (1) and one in the off state (0). Both are going through a gate that says "AND", and output a wire with an unknown state.
Choose 1 answer:

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
111
100
010
000
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:
A diagram that shows two wires, one in the on state (1) and the other in the off state (0). Both wires are going through a gate that says "OR", and output an on wire (1).
Check your understanding
Based on that description, what value do you think this OR gate outputs?
A diagram that shows two wires in the on state (1) going through a gate that says "OR", and outputting a wire with an unknown state.
Choose 1 answer:

Let's look at the truth table for OR gates:
Input AInput BOutput
111
101
011
000
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".
A diagram that shows one wire next to rain drops and another wire next to a mountain. Both wires go through a gate that says "OR", and output a wire next to a pair of 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:
Screen capture from video. There's a breadboard at the top with wires, transistors, resistors, push buttons, and LED lights. The breadboard is on top of a piece of paper with truth tables for AND gates and OR gates, plus a diagram of the breadboard circuit.
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.

🙋🏽🙋🏻‍♀️🙋🏿‍♂️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?

  • duskpin ultimate style avatar for user Jonathan
    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)
    Default Khan Academy avatar avatar for user
    • orange juice squid orange style avatar for user Evan
      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! (:
      (18 votes)
  • spunky sam blue style avatar for user Jiselle Jackson
    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?
    (9 votes)
    Default Khan Academy avatar avatar for user
  • orange juice squid orange style avatar for user Evan
    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)
    Default Khan Academy avatar avatar for user
    • hopper jumping style avatar for user pamela ❤
      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!
      (13 votes)
  • hopper cool style avatar for user Moksha
    Can you expand on this Boolean Operation? What are some (real-life) examples? Are all the gates Boolean... (how)?
    (4 votes)
    Default Khan Academy avatar avatar for user
    • mr pink red style avatar for user siphesihlezwane583
      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)
  • blobby green style avatar for user jerome mwita
    does all logic gates used ina computer and what do they operate about
    (2 votes)
    Default Khan Academy avatar avatar for user
    • leaf green style avatar for user Shane McGookey
      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!
      (5 votes)
  • aqualine seed style avatar for user Ethel Isong
    I would appreciate a further explanation of how to apply the three basic gates in real life computing
    (2 votes)
    Default Khan Academy avatar avatar for user
  • aqualine ultimate style avatar for user Eishah Armaghan
    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)
    Default Khan Academy avatar avatar for user
  • duskpin ultimate style avatar for user Zainab Siddiqui
    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)
    Default Khan Academy avatar avatar for user
  • leaf red style avatar for user Azeem Khan
    Can you explain to me Building blocks that use NAND and NOR gates?
    (1 vote)
    Default Khan Academy avatar avatar for user
  • blobby green style avatar for user Lavish Bhojani
    A piece of equipment has three fault detectors and these are connected into an alarm
    control unit which is a logic circuit.
    A single fault is regarded as unimportant as it probably arises from a faulty detector. Faults
    are therefore indicated only if two or more inputs show a fault condition.
    (a) Construct a truth table to show when the alarm control unit (output Y) is turned on for
    each possible combination of inputs A, B and C.
    (b) Determine the sum-of-products equation for output Y.
    (c) By applying minimisation techniques to your answer for part (b), derive the minimum
    logic expression for output Y. Show your methodology clearly in your answer.
    (d) Using IEC symbols and your answer to part c, draw the minimised logic circuit
    diagram
    (1 vote)
    Default Khan Academy avatar avatar for user