Logic behind base to number conversion

755 Views Asked by At

I am trying to understand and visualize the logic behind the base conversion method.

By "base" I mean how many numbers in a number system:

  1. The decimal number system we use every day has 10 digits {0, 1, 2, 3, 4, 5, 6, 7, 8, 9} and so it is Base 10
  2. A binary digit can only be 0 or 1, so is Base 2
  3. A hexadecimal digit can be {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F}, so is Base 16
  4. And we can use letters {A, B, C, ..., X, Y, Z}, and we get Base 26

Like in base 26 for AAA its 1* 26*26 + 1*26 + 1

Why formula is:- digit * ( base )^position index

2

There are 2 best solutions below

1
On BEST ANSWER

Because it works.

We need a way to express every possible whole numbers.

By having a base $b$ (assuming $b$ is a whole number larger than one) we can simply use the $b$ different digits to list the first $b$ whole numbers from $0$ to $(b-1)$.

(You might ask why $0$ to $b-1$ rather than $1$ to $b$. Technically it is arbitrary. But the indexing works out better if we start at "nothing")

That's the first group of $b$ numbers. We can do a second batch of $b$ numbers by putting a $1$ in front and relisting the $b$ digits after the $1$ for $1\color{blue}0$ to $1\color{blue}{(b-1)}$. This next group of number will go from $1\color{blue}0= b$ to $1\color{blue}{(b-1)} = b + (b-1) =2*b - 1$.

So those are the first $2b$ numbers from $0$ to $2b -1$.

We can do a third, fourth and fifth group of $b$ numbers (assuming our $b$ is that large by placing a $2,3,4$ befor and repeating the $b$ digits. We can do this for $b$ groups of $b$ from:

$\underbrace{\underbrace{0, 1,2 ,...,(b-1)}_{b} + \underbrace{10, 11,12 ,...,1(b-1)}_{b}+ \underbrace{20, 21,22 ,...,2(b-1)}_{b}+...... \underbrace{(b-1)0, (b-1)1,(b-1)2 ,...,(b-1)(b-1)}_{b}}_{b\text{ groups of }b\text{ numbers each}}$

This $b$ groups of $b$ numbers so this is $b^2$ numbers from $0$ to $b^2 -1$. The the number $ka$ would the int $k+1$th group which has the numbers from $k*b$ to $k*b + (b-1)$ and this is the $a+1$ number in the group and is the number $k*b + a$. (Okay, I admit it. The indexing from $0$ is confusing sometimes... but trust me... on the whole it is easier.

Now we have run out of two digit combos after $b^2$ numbers. (Which makes sense. We can have $b$ options for the first digit and $b$ for the second of $b^2$ total.)

But we can do another set of $b^2$ numbers but putting a $1$ before the two digits.

And if we put the $b$ digits before the two digits we can get $b$ groups of $b^2$ number making a total of $b^3$ numbers ranging from $0$ to $b^3-1$ but having $0...(b-1)$ being the numbers $0$ to $b-1$. $10$ to $(b-1)(b-1)$ being so that the number $an = a*b + n$ being the numbers from $b$ to $(b-1)*b + (b-1) = b^2-1$. And then numbers $100$ to $(b-1)(b-1)(b-1)$ where $amn$ would be the $a+1$th group of $b^2$ numbers which go from $ab^2$ to $a*b^2 + (b^2-1)$ and within that group it is the $m+1$th group of $b$ numbers that go from $a*b^2 + m*b$ to $a*b^2 + m*b + (b-1)$ and it is the $n+th$ number in the group so is $n$ more than $a*b^2 + m*b$ or is $a*b^2 + m*b + n$.

And we bootstrap up. We can get $(b-1)$ more groups of $b^3$ numbers but putting $1$ to $(b-1)$ before them to represent the first $b^4$ numbers with $4$ digits and so on.

It works.

And that's really all there is that can be said.

We do it because it will work.

3
On

Say your number system has 10 digits. This means that you have numbers 0 through 9 to represent quantities 0 through 9. Once you run out of digits, but still want to represent a quantity numberically, it is conventional (at least in the West) to introduce a new digit to your number to the left of it.

What digit should that be? Well, if we have gone through digits 0-9 once, we can say that we have exhausted 1 of these quantities, so lets place a 1 and start the count from zero again, i.e. 10.

We can keep on going until we say that we have exhausted this quantity of 0-9 + 1 (one unit of ten), 9 + 1 times. Well, we've run out of digits again, so lets add a new digit to the left: 100. What does it mean, well, that we've gone through a unit of 10, 10 times. 10*10 = 100.

We can apply this reasoning for any base. 10 in hexadecimal means you've gone through 0-F one time. 100: you've gone through 10 (i.e. 16), 16 times. So, to figure out the value of 1000? You've gone through 16 * 16, 16 times, i.e. 16 * 16 * 16 = $16^3$

So, for any number base, to find out the value of a position index, you can say that you've gone up to the base at that position index a base number of times. Effectively, we can see that the forumla becomes $base^{position}$$ ^{index}$.

And for your forumla: $digit * base^{position}$$ ^{index}$. Well, say that you want to know what 20 represents in hexadecimal. Well, it simply means that you've gone from 0 to F not once, but twice. So that'll be 2 * 16 (as there are sixteen possible values from 0 to F).

And, we can of course apply this to other position indices. 200 in hex? Well, you've used up 16, 16 times, which is $16^2$, and you've done that twice, so $16^2 * 2$.

An extremely similar logic can be used when thinking about what values to apply after a floating point (we can't say decimal point as the decimal specifically refers to base 10). I am happy to elaborate on how we can find this too if need be.