Photo by Crissy Jarvis on Unsplash
You know how to add numbers progmatically, right?
1 + 1
will basically give you 2.
Numbers are added in binary form down in machine level.
But how do numbers get added underneath the hood?
I will show how to add “positive” integers (no floating) using boolean operations.
💭 Assumption
I will assume the knowledge of binary numbers and Boolean operations.
And you can follow along on CodeSandbox.
📊 AND & XOR Truth Tables
Below is the truth table of all possible XOR & AND operations I will refer back to.
1️⃣ Adding one bit
When you add two onebit numbers, you get either 0 or 1 for the sum and 0 or 1 for the carry.
Did you notice that, carry
output looks the same as output of AND truth table, and sum equal to that of XOR?
The operation can be represented using logical XOR & AND gates as shown here.
A circuit formed that way is called halfadder.
Armed with the knowledge and we can now implement the addition using XOR & AND.

xor
returns true (or 1) when both input are different. 
and
was used using builtin JavaScript&&
operator. 
xorBit
&andBit
return 1 or 0 depending on whether result is true or false. Think of
andBit
as an AND gate andxorBit
as XOR gate in the Halfadder figure above.
 Think of
 “s” refers to “sum”, while “c” means “carry”.
When we run the half adder on combination of one bit addition, the result looks like below.
OK, that wasn’t interesting enough as we can’t do anything by adding just one bit.
Let’s spice it up by adding two bits.
2️⃣ Adding two bits
We got the carry from the halfadder but to calculate the next bit we need to pass the carry to the next adder.
But the problem is that, halfadder accepts only two inputs and doesn’t accept a carry.
We can solve the problem by combining two halfadders, making it a fulladder.
Logic looks like following.
 You calculate the first (least significant) bit using the halfadder and feed the carry from it to the full adder.
 The full adder will calculate the 2nd bit and then sum again in the half adder with the carry as the input
 Lastly, output carry of the full adder is the OR of carries from twohalf adders in the fulladder.
Simply put, you perform two operations. One for the current bit, and another with the carry.
Let’s take a look at an example of adding 11 and 01 to get 100.
I apologize for the 💩 illustration 😅.
And thank you @MarkN_LP for catching the error.
The diagram shows the result of first carry being fed into the 2nd halfadder, which is used to calculate sum.
Let’s implement the fulladder & add two bit numbers.
Fulladder is implemented in line#4~8 using newly created orBit
method to calculate the carry.
It uses two halfadders and uses the carry from the “first” operation in the second halfadder.
And the carry is the result of two carries in the two halfadders as shown in the diagram.
11 + 01
correctly returns { c1: 1, b1: 0, b0: 0 }
.
Still useless right? Let’s add more bits.
3️⃣ Adding Nbits
When you add one bit, you need just a halfadder. For two bits, 1 halfadder & 1 fulladder.
For 3 bits, you would need 1 halfadder & 2 fulladders.
So for Nbit addition, you need 1 halfadder & N1 fulladders.
I could’ve shown 3bit operation but decided to share a method that works on any Nbits instead (unlike how microprocessors are physically constrained).
This code assumes that length of two digits have the same length.
I initially was going to change the length dynamically but it made the demo code too convoluted so left it out.
Line #2 & #3 converts strings into array of numbers
and #7 uses reduceRight to start working on the least significant (rightmost) bit.
On first iteration, we calculate the sum using halfadder on line #14, and then we use the fulladder for the rest.
Carry passed to the fulladder is retrieved from the first item in the array because we are prepending new digit ([{c, s}, ...acc]
) on each iteration.
Lastly, we are returning a text representation of the sum for demo purpose only.
Sorry about abusing &&
there 😜.
I got excited after reading “Mastering JavaScript’s && and  logical operators” by Nicolas Marcora today. 🙂
Let’s check out the demo result.
Values within parenthesis shows operations in base 10.
👋 Parting Words
We’ve looked at how positive numbers are added under the hood.
I am also just learning about this thus the explanation might lack much.
The source I am learning from is “The Manga Guide to Microprocessors“.
I still haven’t finished the book but it has been delightful.
🏔 Resources
If you want to dig deeper, check out following resources.
 The Manga Guide to Microprocessors – No Starch Press
 AdderWikipedia article
 Diagram & Truth tables for
 Demo program is available on CodeSandbox
 Fulladder diagram on Google Slides.
 Halfadder on Wikipedia.
The post Adding numbers using Boolean operations in JavaScript appeared first on Sung's Technical Blog.
Discussion (0)