4 ms·
I love the boolean identity and proof that was hinted at by item #1 - namely that any truth table can be built from NAND gates. This is a case where several bo
by smoyer 11y ago
I love the boolean identity and proof that was hinted at by item #1 - namely that any truth table can be built from NAND gates. This is a case where several boolean identities build to form the equivalence. And it's these rules that let you assert #2 from the OP.
A NAND gate is defined as:
a x b = 0
So if both a and b are 1, the output will be zero. You need an inverter to form any other logic functions, but you also know that:
a x b = 0 when a = 1 and b = a
In other words, wire both inputs to one signal to get an inverter. Next we need an or gate, so we can use the identity:
!(a x b) = !a + !b
So to build the other necessary gates from a NAND gate, we simply combine these rules:
AND - invert the output of a NAND.
NOT - wire both inputs to the same signal.
OR - invert both inputs of the NAND gate
NOR - invert both the inputs and output of the NAND gate.
One tip for the aspiring logic designer (and those who would rather calculate a result than create giant if-then-else structures in their code - learn Gray's Code and Karnaugh Maps. You can easily minimize almost any truth table into a sum-of-products form.
- deleted 11y ago[deleted]
- shawndumas 11y agoconst logicGates = { nand(a, b) { return !(a && b); }, not(a) { return this.nand(a, a); }, and(a, b) { return this.not(this.nand(a, b)); }, or(a, b) { return this.nand(this.not(a), this.not(b)); }, nor(a, b) { return this.not(this.or(a, b)); }, xor(a, b) { return this.and(this.nand(a, b), this.or(a, b)); }, xnor(a, b) { return this.not(this.xor(a, b)); } }; Object.keys(logicGates) .map(gate => { return [ { gate, a: false, b: false }, { gate, a: false, b: true }, { gate, a: true, b: false }, { gate, a: true, b: true }, ]; }) .forEach(x => { x.map(y => { const desc = JSON.stringify(y).replace(/(gate)|:|"|\{|\}|,/g, ' ') + '=>'; console.log(desc, (({ gate, a, b }) => logicGates[gate](a, b))(y)); }); }); http://blog.shawndumas.com/post/119694815258/logic-gates-es6-version http://blog.shawndumas.com/post/119694815258/logic-gates-es6...
- abecedarius 11y agoThe NAND To Tetris book requests people not post spoilers for their problem sets. (I violated this myself in putting my code on github, but with a big spoiler warning at the top, at least. I put it up there because my Python HDL might be useful to some others who didn't want to use their language.) It's nice to start seeing ES6 code in the wild, though.