Welcome to Project Lovelace! We're still in early development so there are still tons of bugs to find and improvements to make. If you have any suggestions, complaints, or comments please let us know on Discourse, Discord, or GitHub!

NAND gate

You will learn about: logic gates and if statements.

You have hundreds of millions of logic gates in your phone and laptop. They're tiny circuits that work with 0's and 1's and perform basic boolean operations, usually with 2 inputs.

Truth tables for the NOT, AND, and NAND logic gates. (Source: Wikipedia: NAND logic)

A simple logic gate would be the NOT gate, which inverts the input so a 0 becomes a 1 and vice versa. An AND gate produces 1 only if both of its inputs are 1, otherwise it produces 0. The NAND gate is a an AND gate followed by a NOT gate, so it's performing a (NOT AND) operation. Try to write a function NAND(A, B) that returns the output of a NAND gate.

Input: Two integers A and B that have the value 0 or 1.

Output: The output of a NAND gate with inputs A and B.


Input A: 1 Input B: 1 Output NAND(A, B): 0
 Difficulty  Timesink
 Function NAND(A, B)

You must be logged in to view your submissions.


  • All logic gates are made using transistors so transistors are the building blocks of all phones and computers.
  • The NAND gate (and NOR gate) is special in that it's a universal gate so you can build any other gate using a NAND gate. You could even build a CPU completely out of NAND gates, although it would probably be bigger and slower than if you just used the actual gates you needed.
  • NAND to Tetris teaches you how to build a computer that plays tetris from the ground up starting with NAND gates!

Let us know what you think about this problem! Was it too hard? Difficult to understand? Also feel free to discuss the problem, ask questions, and post cool stuff on Discourse. You should be able see a discussion thread below. Would be nice if you don't post solutions in there but if you do then please organize and document your code well so others can learn from it.