In the previous entry we talked about numerical systems, number representation, and positional systems.
Thus, the concept of base appeared, which is the set of symbols we are going to use to represent numbers (more specifically, we usually don’t refer to the number of available symbols).
So we saw an invented numerical system that had this base 🔵,🔺, 🟩,⭐. Now it’s time for a little math to “solidify” the concepts.
During this entry I will use the decimal base as the “normal” system. Basically because if we talk in stars and squares, it would be hard to understand. But remember that everything would be equally valid with any other system.
Math content ahead! If you don’t feel like reading math right now, go to another entry and come back later.
What is my number worth in baseN
Going back to our example of base 🔵,🔺, 🟩,⭐ (1, 2, 3, 4) What equivalence with the decimal base does a number have?
In this case, we have a baseN = 4
- The rightmost digit can take values from 0 to 3
- If there is a digit in the next position, it means the rightmost one has passed 4
- If there is another digit in the following position, it means it has passed 4 times 4
- And so on…
Examples
That is, if we have the number 🔺🟩 (1, 2)
- On the right I have a
2 - On the left I have a
1, which means the right one has already gone through 4 positions
That is, the number I have is
4 + 2 = 6
If the number were 🟩🟩 (2, 2), I would have
- On the right I have a
2 - On the left I have a
2, which means the right one has already gone through 2 times 4 positions
So the number I have is
(2 * 4) + 2 = 10
And if I keep clicking until I have 3 digits 🔺🟩🟩 (1, 2, 2)
- On the right I have a
2 - In the middle I have a
2, which is worth 4 x 2 - On the left I have a
1, which is worth 4 x 4
(1 * 4²) + (2 * 4) + 2 = 10
Conversion from BaseN to decimal
So How do we convert from BaseN to decimal? It’s what we did above, but generalized to any base.
Dᵢthe digit in position iBase, the number of symbols in your base
For example, to convert the number 2,310 in base 4 to decimal.
base₁₀ = 2 * 4³ + 3 * 4² + 1 * 4¹ + 0 * 4⁰
= 128 + 48 + 4 + 0
= 180
This equation is also equivalent to this sequence
Conversion from decimal to BaseN
Now let’s see the inverse process converting a decimal number to a BaseN. In this case, the easiest thing is to go dividing and calculating remainders.
Let’s see with an example, converting 180 to base 4.
base₄ => 180 / 4 = 45, remainder 0
45 / 4 = 11, remainder 1
11 / 4 = 2, remainder 3
2 / 4 = 0 remainder 2
And your decimal number 180 converted to base 4 is the ordered remainders, 2310.
Comparison with decimal
Does all that seem strange to you? If it was hard to visualize, think for a moment about the decimal system.
Imagine the number 1,537. What we said is that we can think of 1,527 as a sum of powers of 10.
1.537 = 1.000 + 500 + 30 + 7
// which is the same as saying
1.537 = (1 * 10³) + (5 * 10²) + (3 * 10) + (7)
You do these operations continuously! It’s just that the decimal system is very ingrained in you.
We also said that, instead of thinking of it as a sum of powers, you can think of it as a sequence. At each step you multiply the previous one by 10, and add a number.
1.537 = 153 * 10 + 7
= (15 * 10 + 3) * 10 + 7
= ((1 * 10 + 5) * 10 + 3) * 10 + 7
Finally, you can also do the sequence backwards, and keep the remainders
base10 => 1.537 / 10 = 153, remainder 7
153 / 10 = 15, remainder 3
15 / 10 = 1, remainder 5
1 / 10 = 0 remainder 1
They are the same operations you did for the base change, only instead of ‘10’ you put BaseN.
