Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
14
Task/Four-bit-adder/JavaScript/four-bit-adder-1.js
Normal file
14
Task/Four-bit-adder/JavaScript/four-bit-adder-1.js
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
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;
|
||||
}
|
||||
72
Task/Four-bit-adder/JavaScript/four-bit-adder-2.js
Normal file
72
Task/Four-bit-adder/JavaScript/four-bit-adder-2.js
Normal file
|
|
@ -0,0 +1,72 @@
|
|||
// 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 = Array(4),
|
||||
inB = Array(4),
|
||||
out = Array(4),
|
||||
i = 4,
|
||||
pass;
|
||||
|
||||
while (i--) {
|
||||
inA[i] = a[i] != 1 ? 0 : 1;
|
||||
inB[i] = b[i] != 1 ? 0 : 1;
|
||||
}
|
||||
|
||||
// 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[3] = pass.sum;
|
||||
pass = fullAdder(inA[2], inB[2], pass.carry);
|
||||
out[2] = pass.sum;
|
||||
pass = fullAdder(inA[1], inB[1], pass.carry);
|
||||
out[1] = pass.sum;
|
||||
pass = fullAdder(inA[0], inB[0], pass.carry);
|
||||
out[0] = pass.sum;
|
||||
return out.join('');
|
||||
}
|
||||
1
Task/Four-bit-adder/JavaScript/four-bit-adder-3.js
Normal file
1
Task/Four-bit-adder/JavaScript/four-bit-adder-3.js
Normal file
|
|
@ -0,0 +1 @@
|
|||
fourBitAdder('1010', '0101'); // 1111 (15)
|
||||
11
Task/Four-bit-adder/JavaScript/four-bit-adder-4.js
Normal file
11
Task/Four-bit-adder/JavaScript/four-bit-adder-4.js
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
// 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;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue