-
Notifications
You must be signed in to change notification settings - Fork 11
/
HalfAdder.js
60 lines (50 loc) · 1.23 KB
/
HalfAdder.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
/**
* 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 `HalfAdder` gate.
*/
const TRUTH_TABLE = [
{a: 0, b: 0, sum: 0, carry: 0},
{a: 0, b: 1, sum: 1, carry: 0},
{a: 1, b: 0, sum: 1, carry: 0},
{a: 1, b: 1, sum: 0, carry: 1},
];
/**
* A HalfAdder.
* `sum` returns the LSB of the sum of the two bits a and b.
* `carry` returns the carry bit.
*/
class HalfAdder extends BuiltInGate {
/**
* sum = a ^ b
* carry = a & b
*/
eval() {
const a = this.getInputPins()[0].getValue();
const b = this.getInputPins()[1].getValue();
this.getOutputPins()[0].setValue(a ^ b);
this.getOutputPins()[1].setValue(a & b);
}
}
/**
* Specification of the `HalfAdder` gate.
*/
HalfAdder.Spec = {
name: 'HalfAdder',
description: [
'Implements 2-bits adder (half-adder) gate.',
'',
'The `sum` returns LSB (the least significant bit) of the sum',
'of the two bits `a`, and `b`.',
'',
'The `carry` returns the carry bit.',
].join('\n'),
inputPins: ['a', 'b'],
outputPins: ['sum', 'carry'],
truthTable: TRUTH_TABLE,
};
module.exports = HalfAdder;