Hey Y’all! This is a translation of my blog post originally written in Portuguese. If you want to read that version, click here.

Hello people of this internet, for those who are arriving here now, this is the second article of a series I’m doing to talk about the fundamentals of computing, I recommend reading the first one about logic gates. And for those who have already read the first article, take a quick look there because I did a revision of the article, I highly recommend taking a look 😊

Loading the Elevenlabs Text to Speech AudioNative Player...

Let’s dive a bit deeper and explore some concepts of mathematics and also human history.

Mathematics came from the human race’s need to count and measure – how many animals Maria takes care of, how many days until the next solstice… Over time and human expansion, ancient peoples created various numbering systems for their daily lives. Games like Grand Theft Auto V, Call of Duty: Black Ops III, Dragon Quest XI are examples of using the numeral system that the Romans used, and the I Ching, an ancient Chinese text, has roots in binary representations.

However, not only ancient civilizations used other numerical systems. The hours of a day are divided between 12 hours of the morning period and 12 hours of the afternoon period, or a dozen eggs. In the French language, there are still remnants of a vigesimal-based system (20), for example, the number 80 is said as quatre-vingts (four twenties), and one of the most recent is Braille, which also has roots in binary representations.

Nowadays, we use decimal representation, and the reason is quite simple: the average number of fingers on human hands is an incredible 10 fingers, so it’s easier to count using only hands.

☝️ + ☝️ = ✌️#

This base 10 representation is positional, meaning the position in which the digits are “drawn” changes the quantity being represented. 70 represents ten times more than 07, hence the popular saying “zero to the left”. In school, we learn about decimal places – units, tens, hundreds… They serve to represent what the position of that digit is.

TensUnits
70
07

And on top of this order, there’s a mathematical formula that converts a number from any base to decimal. To make it easier:

  • Digit: Symbol that represents a unique quantity. Ex.: 1, 2, 3, 4, 5, 6, 7, 8, 9, 0
  • Number: Digits organized in order of position. Ex.: 10, 144, 999, 42, 37, 0x8A6C, 0b101010
  • Base: Maximum quantity of digits that can be represented. Ex.: binary (0b) 2, decimal 10, hexadecimal (0x)
  • Index: The magnitude of the digit in the position. _Ex.: 10, the 1 in 10 has Index 1 and the 0 has Index 0 (it’s a bit counterintuitive, but it makes sense). In other words, the index is the position from right to left, starting at 0.

The formula is quite simple: (d)igit × (b)ase(i)ndex. In the example we’re doing, this is the formula to convert 70 to decimal… to 70 in decimal. 7 × 101 + 0 × 100 = 70

Interactive

When we talk about numbers this way, it becomes easier to represent binary, which is nothing more than the quantity that can be represented by the formula d × 2i.

Interactive

And this way of representing quantities can also be manipulated with mathematical operations. The addition 25 + 17 is solved in a way similar to this.

1
2 5
1 7
--- +
4 2

In the example above, we have some elements that need to be highlighted:

  1. A digit can only be added to another digit that is in the same index.
  2. When the sum cannot be represented with just one digit, the smallest index is kept as the result, and the remainder is sent to the next index.
  3. The result of a sum will always be the sum of the digits of the index + the remainder that came from the digit in the previous index.

With these same steps, we can apply addition in any base.

    1 1 1       |  1      |  
1 0 0 0 1 1     |  2 5    |  1 9
0 0 0 1 1 1     |  1 7    |  1 1
------------ +  |  --- +  |  --- +
1 0 1 0 1 0     |  4 2    |  2 A 

Calculation of 25 + 17 in Binary, Decimal and Hexadecimal bases

Interactive

Now that we already know each other, how can we make the computer know us too?

Playing with translation#

Remembering the previous article, let’s isolate the addition of two digits in tables. First, let’s look at just the result. We have this table that very much resembles the OR gate table, except for this small change at the end.

Result
?01
001
110
OR
01
001
111

For this, we can combine the results of the gates we saw earlier into a single circuit called “Exclusive OR” or “XOR”.

Interactive

Now, let’s also look at the remainder table of our addition and you’ll notice that it’s an exact copy of the AND gate.

Remainder
?01
000
101
AND
01
000
101

And just like Captain Planet, “By the union of their powers”, we will be able to perform the operation of adding two digits in binary, and this component is called “Adder”.

Interactive

With this combination of logic gates, a computer can already perform the incredible additions of: 0 + 0, 1 + 0, 0 + 1, 1 + 1, but beyond that, it can also say how much remainder there was from the addition. And when several ADDERs are combined, we can perform addition of more complex numbers like 42, but that we’ll see in the next article.

That’s all for today, folks.

References