How the computer calculates “a+b”?

Yuhei Horibe
6 min readMay 25, 2020

--

Simulation of multiplier

Summary

Have you ever wondered, how “a + b” is calculated in a computer hardware?

I’ll answer this question in this article. I’ll try to make it as easy as possible, so I hope, non-engineering people can also read it.

How the numbers are handled in computer

Human beings handle numbers using decimal. So there are 10 different symbols; 0, 1, 2, 3, 4, 5, 6, 7, 8, 9. Then, next number will go to the next digit like 10. We use just 10 different symbols, and multiple digits to express all the integer numbers.

But in the computer, what can easily be handled is just “0” and “1”, which are corresponding to “off” and “on”. But don’t worry. It is totally possible to express all the integer numbers using just “0” and “1”. How we can count the number with just “0” and “1”? It’s the same idea. 0, 1, then next number will go to the next digit like “10”. This numbering system is called “binary”. Also, you might have heard the word “bit”, which is shortened version of “Binary digit”. So basically, 1 bit is 1 digit of binary. What is Byte? 1 byte is equivalent to 8 bits, so 1 byte is 8 binary digits. Table below shows how to count the number in binary (0 — 15).

Decimal Numbers and Binary Numbers

Logical operations

The topic is, “how a + b is calculated”, but we need one more step. Because, “a + b” is calculated by combinations of logical operations. So what is the logical operations then? Basically, there are 3 different types of operations; AND, OR and NOT.

To make it easier to understand, here’s an example. Imagine I’m asking somebody, “Can you hand me something red AND round?”. So the person will look for the object. The person will pick up something, and ask; “Is this red?” and “Is this round shaped?”. If both conditions are true, the object matches with what I asked. This is logical AND.

Next, imagine, I’m asking somebody “Can you hand me something round OR triangular?”. Then, the person, again, will look for the object. The person will pick up some object and ask, “Is this round shaped?” and “Is this triangular”? If, at least one of these conditions is true, the object matches with what I asked. Also, both conditions can be true in this case. This is logical OR.

At last, I would ask somebody, “Can you give me something NOT sweet?”. This one is easy. The person will pick up something, and ask “Is this sweet?”. If this condition is not true, the object matches with what I asked. So basically, “NOT” operation negates “true” and “false”.

Logical operations in electrical circuit

If you remember electrical circuit, it might be easier to imagine how those operations work. Here are some examples.

Logical AND in electrical circuit

The lamp is on if both Switch A and Switch B are on.

Logical OR in electrical circuit

The lamp is on if one of, or both Switch A and Switch B are on.

Logical NOT in electrical circuit

This one is a bit tricky. The switch A is b contact, which means, if “Switch A is on, the circuit will be broken”. So, the lamp is on if Switch A is not on.

Binary adder

When you calculate “a + b”, the result is always the same. Let’s say, “1 + 2” is always “3”, and never changes (unless you are joking). If the calculation is “4 + 3”, it’s always “7”. If this is binary, it’s possible to do it with logical operations.

Imagine the case where, two 1-bit numbers are added. The calculation is like this;

  • 0 + 0 = 0
  • 0 + 1 = 1
  • 1 + 0 = 1
  • 1 + 1 = 10

It’s like the table below.

1-bit adder

Look at the second digit of output Y. This is pretty straight forward. If both A and B are “1”, Y1 will be “1”, this is “AND” operation. This digit is also called carry. So in 1-bit adder, carry is calculated like below;

carry = A and B

What about first digit? Output Y’s first digit will be 1 if “one of A or B is 1, excluding the case where both A and B are on". This operation is called “exclusive OR”. This can be presented like “EXOR”, or “XOR”. This operation can be described like below;

Y = A xor B
=> (not A and B) or (A and not B)

It looks, a bit complicated, but XOR can be represented with basic logical operation. This 1-bit binary adder is called “half-adder”. Half adder has two inputs A and B, and two outputs Y and Cout (carry output).

Half adder

Okay, next will be 2-bit adder. But, first digit will be calculated in the way explained above. So let’s think about, how we can calculate just 2nd digit. To calculate 2nd digit, we will add 2nd digit of A and B, and “carry” from 1st digit. So the second digit will be calculated like 3-input, 1-bit adder.

Y = A + B + carry

The calculation will be like below;

  • 0 + 0 + 0 = 0
  • 0 + 0 + 1 = 1
  • 0 + 1 + 0 = 1
  • 0 + 1 + 1 = 10
  • 1 + 0 + 0 = 1
  • 1 + 0 + 1 = 10
  • 1 + 1 + 0 = 10
  • 1 + 1 + 1 = 11

The point is, the result will also be 2 digits. So we can use the same idea as 1-bit adder. The output will be separated into “carry” and “sum”.

3-input 1-bit adder

Let’s look at carry output. Carry output will be “1”, if two or more inputs are “1”. Let’s make it easier. I’ll describe this like below.

Cout = (A and B) or (Cin and (A or B))

The sum can be described like this. If number of 1’s are odd number, the output Y will be “1”. This is described like below;

Y = A xor B xor Cin
=> (A and not B and not C) or (not A and B and not C) or (not A and not B and C) or (A and B and C)

This 3-input binary adder is called “full-adder”.

Full adder

Then, the rest can be calculated in the same way. Here’s an example of 4-bit binary adder.

4-bit binary adder

This is how “a + b” is calculated in the computer hardware.

--

--

No responses yet