开发者

Help with JavaScript self-extending bit adder

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 ++) {
   开发者_如何学C             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!


Also, I don't know whether you should use for(i=0;i < ai.length; i++) or for(i=ai.length-1; i>=0; i--)

Assume the original one, then maybe you should use:

if (i>0) { newadd[i]=fullAdd(ai[i],bi[i],newadd[i].charAt(0)); newadd[i+1]=fullAdd(ai[i],bi[i], newadd[i].charAt(1)); }


probably you want

document.write("a + b = " + add(a,b));

instead of

document.write("a + b = " + newadd(a,b);


At a first glance, maybe you have written wrong, maybe it should be : document.write("a + b = " + add(a,b));

The original lacks ")" at last and use "newadd"

Also, I don't know whether you should use for(i=0;i=0;i--)

Assume the original one, then maybe you should use if (i>0) { newadd[i]=fullAdd(ai[i],bi[i],newadd[i].charAt(0));

newadd[i+1]=fullAdd(ai[i],bi[i], newadd[i].charAt(1));

}

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜