In the previous entry, we talked about number systems, representation of numbers, and positional systems.
Thus, the concept of base emerged, which is the set of symbols that we will use to represent numbers (more specifically, we will not usually refer to the number of available symbols).
We saw a number system invented that had this base 🔵,🔺, 🟩,⭐. Now it’s time for a bit of math to “settle” the concepts.
During this entry, I will use decimal base as the “normal” system. Basically because if we talk in little stars and squares, it would be hard to understand. But remember that everything would be equally valid with any other system.
Mathematical content ahead! If you don’t feel like reading math at this moment, go to another entry and come back in a while.
How much is my number worth in baseN
Returning to our example of base 🔵,🔺, 🟩,⭐ (1, 2, 3, 4), what equivalence does a number have with the decimal base?
In this case, we have a baseN = 4
- The digit on the right can take values from 0 to 3
- If there is a digit in the next position, it means that the one on the right has passed 4
- If there is another digit in the next position, it means it has passed 4 times by 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 that the one on the right has already passed 4 positions
That means 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 that the one on the right has already passed 2 times by 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? This is what we did above, but generalized to any base.
Where,
Dᵢ
is 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 succession
Conversion from decimal to BaseN
Now let’s see the reverse process converting a decimal number to a BaseN. In this case, the simplest thing is that we will divide and calculate 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 this seem strange to you? If you found it hard to visualize, think for a moment about the decimal system.
Let’s imagine the number 1.537. What we have 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 you have internalized the decimal system very well.
We also said that instead of thinking of it as a sum of powers, you can think of it as a succession. 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 succession in reverse 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
These are the same operations you did for the base change, only that instead of ‘10’ you put BaseN.