Making Logic Gates in JavaScript
Everyone knows that computer programs are made of bits - the ones and zeros that hold all the information that keeps our modern life running.
But how do you get from numbers to programs - how simple ones and zeros do anything?
The answer is logic gates (as well as a whole bunch of stuff about how electronics are built - you need to actually make these logic gates on out of circuits in order for our logic structures to be enacted; but I'm a programmer, not an electrical engineer, so let's not go there). A logic gate is what's known as a Boolean function, a mathematical operation that takes in bits and spits out more bits.
Each logic gate takes in two bits of information (as mentioned before, generally denoted as a 1 (also sometimes represented as true) or a zero (also sometimes represented as false)), so there are four possible inputs to a logic gate: {0, 0}, {0, 1}, {1, 0}, and {1, 1}. For each of these inputs, a logic gate can spit out either a 1 or a 0, so there are 2^4 possible output combinations. However, since {0, 1} and {1, 0} are regarded as equivalent, this gives us 2^3 possible output combinations, of which we ignore the always-true combination and the always-false combination, giving us six possible logic gates:
Our regular logic gates are AND, OR, and XOR, and each of these has a corresponding negated form - NAND stands for "NOT-AND".
AND and NAND
And is easy as it conforms to our regular usage of the word "and". If we have two statements and we link them together in a sentence with an "and", the sentence will be true if and only if both of the statements are true. "Pizza is delicious and hamburgers are delicious" is a true sentence, but "Pizza is delicious and hamburgers are not delicious" is a false sentence. Hence, the AND logic gate gives a true output when both of the inputs are true and false either of them are false. The NAND gate is the opposite and gives a false output when both inputs are true and true when both are false.
Here's a simple implementation of AND and NAND in JavaScript. Note that we can take advantage of the fact that 0 is false in JavaScript and use 1s and 0s as inputs. Also note that for NAND, we can either simply negate the return from AND, or we can simply state the input conditions required.
OR
The word "or" is tricky. Sometimes, when we say "or", we mean one or the other, as in "You can go left or you can go right." Sometimes, we mean "either one or both", as in "You can have cheese or bacon on your burger." The logic gate OR corresponds to the "either or both" meaning. So, the OR gate gives a true output when one or both of the input values are true, and false when neither of the inputs are true. Correspondingly, the NOR gate only gives a true output when both inputs are false, and gives true the rest of the time.
XOR
XOR covers the other meaning of "or", where only one of the given options can be true. The XOR gate outputs true if and only if one of the inputs is true, and outputs false both for the case of two true inputs and two false inputs. XNOR reverses this.
I've covered the XOR bitwise operation before, and you might notice that it works the same way as the XOR gate. Convenient!
What's fun about all these functions, since they both input and output booleans, you can call them upon themselves:
Furthermore, the NOR and NAND gates are what's known as universal gates - you can emulate the other gates using just NOR or just NAND. So we can use our JavaScript NOR gate to re-make our other gates:
You can even make a NOR gate out of your NOR gates:
A similar thing can be done in NAND:
And NAND can reproduce itself as well:
And, finally, this whole thing can eat its tail: since NAND can make NOR, and NOR can make anything else, we can make NAND from NOR from NAND:
Ta-da!
Comments
Post a Comment