-
Notifications
You must be signed in to change notification settings - Fork 11
/
And16.js
66 lines (54 loc) · 1.62 KB
/
And16.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
60
61
62
63
64
65
66
/**
* The MIT License (MIT)
* Copyright (c) 2017-present Dmitry Soshnikov <dmitry.soshnikov@gmail.com>
*/
'use strict';
const BuiltInGate = require('../BuiltInGate');
const {int16Table} = require('../../../util/numbers');
/**
* Canonical truth table for the `And16` gate.
*/
const TRUTH_TABLE = int16Table([
{a: 0b0000000000000000, b: 0b0000000000000000, out: 0b0000000000000000},
{a: 0b0000000000000000, b: 0b1111111111111111, out: 0b0000000000000000},
{a: 0b1111111111111111, b: 0b1111111111111111, out: 0b1111111111111111},
{a: 0b1010101010101010, b: 0b0101010101010101, out: 0b0000000000000000},
{a: 0b0011110011000011, b: 0b0000111111110000, out: 0b0000110011000000},
{a: 0b0001001000110100, b: 0b1001100001110110, out: 0b0001000000110100},
]);
/**
* A bitwise 16-bit And gate.
*/
class And16 extends BuiltInGate {
/**
* IN a[16], b[16];
* OUT out[16];
*
* for i = 0..15: out[i] = (a[i] & b[i])
*
* Abstract:
*
* And(a=a[0], b=b[0], out=out[0]);
* And(a=a[1], b=b[1], out=out[1]);
* ...
*
* Technically use JS bitwise operations at needed index.
*/
eval() {
const a = this.getInputPins()[0].getValue();
const b = this.getInputPins()[1].getValue();
// In JS implementation doesn't differ from the simple `And` gate.
this.getOutputPins()[0].setValue(a & b);
}
}
/**
* Specification of the `And16` gate.
*/
And16.Spec = {
name: 'And16',
description: 'Implements bitwise 16-bit And & operation.',
inputPins: [{name: 'a', size: 16}, {name: 'b', size: 16}],
outputPins: [{name: 'out', size: 16}],
truthTable: TRUTH_TABLE,
};
module.exports = And16;