Hey guys, today i was inspired by a 16 bit ALU made in Mincraft to try and make my own self-extending adder. I literally started JavaScript today, and have been playing around with logic gates over the past couple days. So I am very new to this.
Here is my code so far.
<html>
<body>
<script type="text/javascript">
function add(ai, bi) {
var newadd = [ai.length+1];
for(i = 0 ; i < ai.length ; i ++) {
if(i>0) {
newadd[i]=fullAdd(ai[i],bi[i],fullAdd(ai[i-1],bi[i-1],0).charAt(1)).charAt(0);;
}
else {
newadd[i]=fullAdd(ai[i],bi[i],0).charAt(0);
}
}
return newadd;
}
function fullAdd(ai, bi, ci) {
var ao = ((ai^bi)^ci);
var co = (((ai^bi)&ci)|(ai&bi));
return ao+""+co;
}
var a = [1,0];
var b = [0,1];
document.write("a + b = " + add(a,b));
</script>
</body>
What am I doing wrong? I'm sure it is something silly or stupid, or that my self-devised system just won't work. Anyways thanks for your help in advance! -Devan
Note: the full adder does work and return the characters it should. The problem is the add function.
EDIT: fixed the 2 obvious mistakes
EDIT2: I have come close to the answer by changing the add function a bit.
EDIT3: Solved, here is the code that works
<html>
<body>
<script type="text/javascript">
function add(ai, bi) {
var newadd = [ai.length+1];
for(i = 0 ; i < ai.length+1 ; i ++) {
if(i>0) {
newadd[i]=fullAdd(ai[i],bi[i],fullAdd(ai[i-1],bi[i-1],0).charAt(1)).charAt(0);;
}
else {
newadd[i]=fullAdd(ai[i],bi[i],0).charAt(0);
}
}
return newadd;
}
function fullAdd(ai, bi, ci) {
var ao = ((ai^bi)^ci);
var co = (((ai^bi)&ci)|(ai&bi));
return ao+""+co;
}
var a = [1,0,1,1];
var b = [0,1,0,1];
document.write("a + b = " + add(a,b));
</script>
</body>
Thank you guys!