Skip to content

Instantly share code, notes, and snippets.

@rwaldron
Created October 25, 2016 19:05
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save rwaldron/feed80026adcb1eca0b70e35b94618c9 to your computer and use it in GitHub Desktop.
Save rwaldron/feed80026adcb1eca0b70e35b94618c9 to your computer and use it in GitHub Desktop.
Full Adder
let fullAdder = (a, b, c) => [(a ^ b) ^ c, ~(~(a & b) & ~((a ^ b) & c))];
[
{ args: [0, 0, 0], expect: {sum: 0, carry: 0}},
{ args: [0, 0, 1], expect: {sum: 1, carry: 0}},
{ args: [0, 1, 0], expect: {sum: 1, carry: 0}},
{ args: [0, 1, 1], expect: {sum: 0, carry: 1}},
{ args: [1, 0, 0], expect: {sum: 1, carry: 0}},
{ args: [1, 0, 1], expect: {sum: 0, carry: 1}},
{ args: [1, 1, 0], expect: {sum: 0, carry: 1}},
{ args: [1, 1, 1], expect: {sum: 1, carry: 1}},
].forEach(test => {
let result = fullAdder(...test.args);
console.log(result[0] === test.expect.sum);
console.log(result[1] === test.expect.carry);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment