How Computers Do Math
To understand the latter half of this article, you will need to know Boolean Algebra. This is not a topic I discuss in this article. Here is a link to where you can learn about it.

Introduction
I’ve been reading Code by Charles Petzold over the past couple of weeks. That book is an absolute goldmine of information.
I’ve been curious to understand how math operations work inside of a computer on a fundamental level. I’ve always been aware that computers carry out mathematical operations differently from human beings because computers operate in binary.
This book answered a lot of questions I’ve had on this topic, and I want to share that information with you.
I’ll be going over how we can do addition and subtraction using logical operators in code. You can replace these logical operators with their logic gate equivalents in the physical world.
This article is an exercise in curiosity, and nothing more. Have fun with it!
Why Binary
First, let’s start with a simple question. Why do we count the way we do? Why do we count 1,2,3,4,5,6,7,8,9,10? Any numbers that come after that are the same with an additional or different digit placed to the left?
Why don’t we count 1,2,3,4,5,6,7,10 for example? It seems a completely arbitrary choice initially.
One possible reason is the number of fingers on our hands.

We have 10 fingers on our hands, so our number system developed the way it did.
Unfortunately, we don’t have the same luxury with electric circuits. Circuits can represent one of two different states at any given time: either the presence or absence of a current running through the wires. So, electrically, we count the following way: 1, 10.
Now, one of the obvious questions here is: Why 1, 10? Why not 1, 2? We need to take a quick diversion to answer that question.
The Importance Of Zero
To understand the importance of zero, let’s compare two different systems of counting. The modern Hindu-Arabic method of counting and the ancient Roman numerals:
Hindu-Arabic: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
Roman: I, II, III, IV, V, VI, VII, VIII, IX, X
The important difference between the two is the last number. The Roman numeral system has a special signifier for the number 10.
To understand why that’s such a big deal, let’s take a simple example:
Here’s how we write the number 205 in the Roman numeral system:
CCVFor the Hindu-Arabic numerical system, however, we can think of it the following way:
205 = 2 * 100 + 0 * 10 + 5 * 1There is no obvious correlation between the number and the Roman numeral system, whereas there is an obvious correlation between the number 205 and the position of its different digits.
This is the importance of zero. It allows the numerical system to become positional
Still not convinced? Look at the following differences in representation between the Roman and Hindu-Arabic numerical system.
Hindu-Arabic Roman
100 C
1000 MIt becomes progressively harder to count larger and larger numbers.
You can read more about this here and here
Counting In Binary
Here’s how we count in binary: 00, 01, 10, 11. Each of these digits is called a bit. These are called two-bit binary numbers.
Don’t confuse two-bit with binary. Binary just means either a 0 or 1 in each position. The number of bits dictates the number of digits in each of the numbers.
Now, we need to learn to perform Boolean Algebraic operations on binary numbers. Boolean Algebra is slightly different in its rules, so just follow along.
If you want to understand Boolean Algebra first, here is a nice link that explains everything you will need to proceed.
Below is an image of the result of three different Boolean Algebra operations. These are also called logic gates.


It is possible to build all of the above logic gates using electrical circuits and components. I won’t go into how it’s possible to do that because it will exceed the scope of this blog post. You can, of course, look this up or better yet, buy a copy of CODE and read it!
What we need to know now is how to represent each of these logic gates in code. Here is how we do that in JavaScript.
AND &
OR |
XOR ^These are called bitwise operators, and I will refer to them as such going forward. If you read bitwise operators, just think logic gates. You can read more about bitwise operators in JS here.
Building A Binary Adding Machine
Okay, we have the basic rules for Boolean Algebra. Now, how do we add two binary numbers? Here’s a table for that.
+ 0 10 00 011 01 10We’ll represent the result in 2 bits. The right bit is called the sum bit and the left bit is called the carry bit.
Let’s split the table into two separate ones: one representing the sum bit and one representing the carry bit.

Okay, great! We now know everything we need to add in binary. We need to figure out a way to reflect the two tables above in terms of code.
Combining Circuits And Code
Let’s reiterate what we know now.
- We know three different logic gates and their corresponding operators in JS.
- We know how to add two binary numbers by hand using the sum and carry tables we just discussed.
We need to figure out a way to express 2 in terms of 1. That is how do we add two binary numbers using the different bitwise operators at our disposal.
Let’s replicate the sum and carry tables separately. First, consider the sum table.

Go back and consider the three logic gate tables we have: AND, OR and XOR. If you compare the values of each cell in the sum table and XOR table, you’ll find that they are an exact match.
So, if we have two binary numbers: 0 and 1, doing the following would give us the sum bit. (You can try this out in the dev tools window in your browser).
let a = 0
let b = 1
let result = a ^ b
// result = 1Next, consider the carry bit. Do the same thing and go back and consider the three logic tables we have and compare them with the carrying table. You’ll find that the carrying table and AND tables are an exact match.
We can express it this way in code
let a = 0
let b = 1
let result = a & b
// result = 0Okay, great! So we can add two 1-bit binary numbers and obtain the sum bit and the carry bit using a combination of an AND gate and XOR gate. Let’s call this a Half Adder.

Okay, great! Let’s express this Half Adder in code now.
Here are the numbers we’re going to add: 1010, 0101. In base 10, there will be 10 and 5 respectively.
1 0 1 0
0 1 0 1
-------
1 1 1 1So, we know how to do that by hand because every column is essentially the same operation: the sum of 1 and 0. We know that it is equal to 1.
Let’s do this in code.













