-
Notifications
You must be signed in to change notification settings - Fork 11
/
Nand.js
59 lines (49 loc) · 1.24 KB
/
Nand.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/**
* The MIT License (MIT)
* Copyright (c) 2017-present Dmitry Soshnikov <dmitry.soshnikov@gmail.com>
*/
'use strict';
const BuiltInGate = require('../BuiltInGate');
/**
* Canonical truth table for the `Nand` gate.
*/
const TRUTH_TABLE = [
{a: 0, b: 0, out: 1},
{a: 0, b: 1, out: 1},
{a: 1, b: 0, out: 1},
{a: 1, b: 1, out: 0},
];
/**
* A bitwise 1-bit Nand (negative-And) gate.
*/
class Nand extends BuiltInGate {
/**
* Nand is the very basic chip on top of which any other chip can
* be implemented: https://en.wikipedia.org/wiki/NAND_gate.
*
* It shares this property with the Nand gate.
*
* In the internal implementation we build it on top of the `&` operation.
*/
eval() {
const a = this.getInputPins()[0].getValue();
const b = this.getInputPins()[1].getValue();
this.getOutputPins()[0].setValue(0x1 - (a & b));
}
}
/**
* Specification of the `Nand` gate.
*/
Nand.Spec = {
name: 'Nand',
description: [
'Implements bitwise 1-bit Nand (negative-And) gate.',
'',
'The "Nand" gate similarly to the "Nor" gate is a basic',
'building block for all other gates.',
].join('\n'),
inputPins: ['a', 'b'],
outputPins: ['out'],
truthTable: TRUTH_TABLE,
};
module.exports = Nand;