Full-adders, 32-bit adders in JavaScript

Share on Facebook1Share on Reddit0Tweet about this on TwitterShare on Google+0
Full-Adder circuit diagram

A Full-Adder, courtesy of the Wikimedia Commons

Fascinated by a lecture as part of my CS degree pursuits (there is light at the end of the tunnel!), I  decided to build some digital circuitry beginning with logic gates and ending with a full 32-bit adder. LIVE DEMO

So here’s my question: I’ve got a 32-bit adder… What now?

Beginning with basic AND, and OR functions, (&& and || operators are native to JavaScript), I built a simple XOR function — which is the first building block not built in to JavaScript:

I wanted each gate/function to work with simple Boolean values, as in JavaScript 1==True, 0==False can get a bit messy. For formatting reasons, I return each value + 0, so they results of higher order circuits are displayed numerically in   binary, rather than a series of True, False’s

After implementing, these foundational gates, I coded up a full adder:

 Within the returned object, “c” represents the carry, and “s” represents the sum. Information about how adders function can be found at this great Wikipedia article.

While there are more complex versions, a simple 4-bit adder is just 4 full-adders daisy chained together which each carry leading into the next adder. The final carry is returned from the 4-bit adder:

16, and 32 bit adders function in the same way: several smaller units daisy chained together, returning the final carry along with the sum:

The functions can be used like so:

So here’s where you come in: What do I do with a 32-bit adder, other than build a 64-bit adder?

Share on Facebook1Share on Reddit0Tweet about this on TwitterShare on Google+0
  • Rob Ballshurt

    Hey there this was a great help to me.
    I am showing my son how to do binary calculations and in doing so decided that we would build a html version to make it easier for him.
    I would imagine not many people would use this but for me it was just what I needed.
    So thanks for the example it was just what we needed.

    • mdjasper

      Thanks Rob for that kind comment. I knew when posting that this post was a little out there, so I’m very glad that it was able to help someone. I’d love to see your page when you’re finished. That sounds interesting.