Four bit adder: Difference between revisions
Content added Content deleted
No edit summary |
m (fixed a dumb mistake) |
||
Line 1,122: | Line 1,122: | ||
// not sure if the rules allow this, but we need to pad the 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 |
// if they're too short and trim them if they're too long |
||
var inA = ( |
var inA = Array(4), |
||
inB = ( |
inB = Array(4), |
||
out = |
out = Array(4), |
||
i = 4, |
|||
pass; |
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, |
// now we can start adding... I'd prefer to do this in a loop, |
||
Line 1,132: | Line 1,138: | ||
pass = halfAdder(inA[3], inB[3]); |
pass = halfAdder(inA[3], inB[3]); |
||
out = pass.sum |
out[3] = pass.sum; |
||
pass = fullAdder(inA[2], inB[2], pass.carry); |
pass = fullAdder(inA[2], inB[2], pass.carry); |
||
out = pass.sum |
out[2] = pass.sum; |
||
pass = fullAdder(inA[1], inB[1], pass.carry); |
pass = fullAdder(inA[1], inB[1], pass.carry); |
||
out = pass.sum |
out[1] = pass.sum; |
||
pass = fullAdder(inA[0], inB[0], pass.carry); |
pass = fullAdder(inA[0], inB[0], pass.carry); |
||
out = pass.sum |
out[0] = pass.sum; |
||
return out; |
return out.join(''); |
||
} |
} |
||
</lang> |
</lang> |