Jump to content

Four bit adder: Difference between revisions

m
fixed a dumb mistake
No edit summary
m (fixed a dumb mistake)
Line 1,122:
// 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(8|a).toString(24),
inB = Array(8|a).toString(24),
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,
Line 1,132 ⟶ 1,138:
pass = halfAdder(inA[3], inB[3]);
out[3] = pass.sum + out;
pass = fullAdder(inA[2], inB[2], pass.carry);
out[2] = pass.sum + out;
pass = fullAdder(inA[1], inB[1], pass.carry);
out[1] = pass.sum + out;
pass = fullAdder(inA[0], inB[0], pass.carry);
out[0] = pass.sum + out;
return out.join('');
}
</lang>
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.