Logic gate is a basic element of [logic circuits](logic_circuit.md), a simple device that implements a [Boolean function](bool.md), i.e. it takes a number of [binary](binary.md) (1 or 0) input values and transforms them into an output binary value. Logic gates are kind of "small boxes" that eat 1s and 0s and spit out other 1s and 0s. Strictly speaking a logic gate must implement a mathematical function, so e.g. [flip-flops](flip_flop.md) don't fall under logic gates because they have an internal state/[memory](memory.md).
NOTE: Logic gate is a term used in [hardware](hardware.md), but the principles of logic gates are present everywhere in [programming](programming.md) and [computer science](compsci.md), the term *logic gate* is practically equivalent to what we otherwise call **Boolean functions, logical operators, binary operations etc.** So this article will talk about all of these at once; the knowledge is crucial not just for hardware engineers but for anyone interested in [hacking](hacking.md) computers in any way.
Logic gates are to logic circuits kind of what [resistors](resistor.md), [transistors](transistor.md) etc. are for electronic circuits. They implement basic functions that in the realm of boolean logic are equivalents of addition, multiplication etc.
Behavior of logic gates is, just as with logic circuits, commonly expressed with so called [truth tables](truth_table.md), i.e. a tables that show the gate's output for any possible combination of inputs. But it can also be written as some kind of equation etc.
There are 2 possible logic gates with one input and one output:
- **[identity](identity.md) (buffer)**: Output equals the input. This doesn't have any function from the logic perspective but can e.g. be used as a placeholder or to introduce intentional delay in the physical circuit etc.
- **[NOT](not.md)**: Negates the input (0 to 1, 1 to 0).
There are 16 possible logic gates with two inputs and one output (logic table of 4 rows can have 2^4 possible output values), however only some of them are commonly used and have their own names. These are:
- **[OR](or.md)**: Gives 1 if at least one input is 1, otherwise 0.
- **[AND](and.md)**: Gives 1 if both inputs are 1, otherwise 0.
Functions NAND and NOR are [functionally complete](functional_completeness.md) which means we can implement any other gate with only one of these gates. For example NOT(x) = NAND(x,x), AND(x,y) = NAND(NAND(x,y),NAND(x,y)), OR(x,y) = NAND(NAND(x,x),NAND(y,y)) etc. Similarly NOT(x) = NOR(x,x), OR(x,y) = NOR(NOR(x,y),NOR(x,y)) etc. NAND and NOR are the only two functions that are functionally complete by themselves, but there are more functionally complete sets of multiple functions, for example OR together with NOT can also implement any other function (which can be seen from the fact that we can implement NOR as NOT(OR(x,y)), and we know NOR is already functionally complete). More detail on this will follow below.
Now please see to the following gigatable of all 16 existing boolean functions of two variables (also called logical operators, conjunction etc.). It's clear there are exactly 16 such functions because two input binary variables result in 4 rows of a truth tables, each of which can be either 0 or 1, which gives 2^4 = 16 possible truth tables. These functions are related in various ways, for example to every function there is always another function which is basically the same, just with negated truth table. The table should highlight some of these patterns.
- Top shows all 16 boolean functions of two variables along with symbols we will use for them, alternative symbols that are often used, commonly used names, their truth tables etc.
- The *arity* row says how many arguments the function's output depends on.
- The *comm* and *assoc.* rows say if the function is [commutative](commutativity.md) (result is the same with swapped arguments) and [associative](associativity.md) (result doesn't depend on bracketing, i.e. order of evaluation) respectively.
- The *dual*, *neg.* and *adj.* say the dual function (equivalent via De Morgan's laws, i.e. its truth table is flipped vertically and negated), negated function (function that gives the same results, just negated) and adjoint function (function giving the same results if we flip the arguments, i.e. its truth table has the middle two rows flipped vertically) respectively.
- The bigger part of the table shows how the function is implemented when we're limited to any of the 26 existing functionally complete sets of functions (of course there exist many ways, here we show hopefully one of the most minimal expressions). This shows that we can implements ALL of the 16 functions using as few as one single function (NAND or NOR).
- Formally all functions here take exactly two input variables, but effectively some of them (namely !X, !Y, X, Y, 0 and 1) behave as [nullary](nullary.md) or [unary](unary.md) functions (have arity 0 or 1) because they ignore one or both inputs. Out of convenience we treat them accordingly in our notation, i.e. for example we'll write just `0` instead of `X0Y` or `!X` instead of `X!Y`, and consequently in our expressions we see `!` as a single unary function to which we feed just one argument, but just keep in mind that eventually `!` is actually secretly two functions: `!X` and `!Y`.
- Notice patterns in the table, i.e. that adjoint functions such as `<` and `>` use the same formula just with *X* and *Y* switched, that the negated function of a function is always the one that's mirrored by the table center etc.