views:

18

answers:

3

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!

A: 

probably you want

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

instead of

document.write("a + b = " + newadd(a,b);
mykhal
That would help, but is still not the main problem =(
pwnmonkey
i get "a + b = 1,01,1" what should be the correct result?
mykhal
it should read 0,1,1
pwnmonkey
for some reason it is adding them as if they were strings, even though that slot is suppose to be overwritten, sorry if im being vague.
pwnmonkey
@{Devan Buggay} i still don't know what your code should do.. but obviously you are treating numbers like strings here: `ao+""+co`
mykhal
.. maybe you want `(ao+co)+""` ? still not your desired result
mykhal
I have edited the add function. ill update in a second.
pwnmonkey
the purpose of this is to take two binary arrays (a and b) and add them together using a string of full adders.Im just not sure how to do this recursively
pwnmonkey
you are initializing newadd = [3] (array with 1 item 3) - is this what you really want? also note that you can convert integer to binary representation: `x.toString(2)`
mykhal
yes that is what i needed, im trying to do this solely with binary values, converting to int, adding, then converting back again would be cheating =) thanks for your help though. Knowing me I probably would not have caught that first error.
pwnmonkey
A: 

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));

}

xueyumusic
yes, thank you. those have been fixed the add method still does not work properly.
pwnmonkey
A: 

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)); }

xueyumusic
yes i just solved it, using a variation of what you used. (ai.length+1 in the for statement)
pwnmonkey