Four bit adder: Difference between revisions

Wrote a JavaScript version
m ({{omit from|GUISS}},)
(Wrote a JavaScript version)
Line 1,044:
 
See also: "A Formal Description of System/360” by Adin Falkoff
 
 
=={{header|JavaScript}}==
 
===Error Handling===
In order to keep the binary-ness obvious, all operations will occur on
0s and 1s. To enforce this, we'll first create a couple of helper functions.
 
<lang JavaScript>
function acceptedBinFormat(bin) {
if (bin == 1 || bin === 0 || bin === '0')
return true;
else
return bin;
}
 
function arePseudoBin() {
var args = [].slice.call(arguments), len = args.length;
while(len--)
if (acceptedBinFormat(args[len]) !== true)
throw new Error('argument must be 0, \'0\', 1, or \'1\', argument ' + len + ' was ' + args[len]);
return true;
}
</lang>
 
===Implementation===
Now we build up the gates, starting with 'not' and 'and' as building blocks.
Those allow us to construct 'nand', 'or', and 'xor' then a half and full adders
and, finally, the four bit adder.
 
<lang JavaScript>
// basic building blocks allowed by the rules are ~, &, and |, we'll fake these
// in a way that makes what they do (at least when you use them) more obvious
// than the other available options do.
 
function not(a) {
if (arePseudoBin(a))
return a === 1 ? 0 : 1;
}
 
function and(a, b) {
if (arePseudoBin(a, b))
return a + b < 2 ? 0 : 1;
}
 
function nand(a, b) {
if (arePseudoBin(a, b))
return not(and(a, b));
}
 
function or(a, b) {
if (arePseudoBin(a, b))
return nand(nand(a,a), nand(b,b));
}
 
function xor(a, b) {
if (arePseudoBin(a, b))
return nand(nand(nand(a,b), a), nand(nand(a,b), b));
}
 
function halfAdder(a, b) {
if (arePseudoBin(a, b))
return { carry: and(a, b), sum: xor(a, b) };
}
 
function fullAdder(a, b, c) {
if (arePseudoBin(a, b, c)) {
var h0 = halfAdder(a, b),
h1 = halfAdder(h0.sum, c);
return {carry: or(h0.carry, h1.carry), sum: h1.sum };
}
}
 
function fourBitAdder(a, b) {
if (typeof a.length == 'undefined' || typeof b.length == 'undefined')
throw new Error('bad values');
// not sure if the rules allow this, but we need to pad the values
// if they're too short and trim them if they're too long
var inA = (8|a).toString(2),
inB = (8|a).toString(2),
out = '',
pass;
 
// now we can start adding... I'd prefer to do this in a loop,
// but that wouldn't be "connecting the other 'constructive blocks',
// in turn made of 'simpler' and 'smaller' ones"
pass = halfAdder(inA[3], inB[3]);
out += pass.sum;
pass = fullAdder(inA[2], inB[2], pass.carry);
out += pass.sum;
pass = fullAdder(inA[1], inB[1], pass.carry);
out += pass.sum;
pass = fullAdder(inA[0], inB[0], pass.carry);
out += pass.sum;
return out;
}
</lang>
===Example Use===
<lang JavaScript>fourBitAdder('1010', '0101'); // 1111 (15)</lang>
 
all results:
 
<lang JavaScript>
// run this in your browsers console
var outer = inner = 16, a, b;
 
while(outer--) {
a = (8|outer).toString(2);
while(inner--) {
b = (8|inner).toString(2);
console.log(a + ' + ' + b + ' = ' + fourBitAdder(a, b));
}
inner = outer;
}
</lang>
 
 
=={{header|LabVIEW}}==
Anonymous user