The Basics of Bases
sugarfi (266)

Intro

You might have heard of bases or binary before. But what does that actually mean? In this tutorial, I will try to cover the fundamentals of bases and digits.

Digits and Naming Bases

The fundamental element of a base is a digit. A digit is just any symbol, like 0 or 9 or a. Numbers are just strings of digits. A base if just a list of which digits you can use. They are named things like base 10 or base 2 based on how many digits they use. The main base that we use is called base 10, because it uses 10 digits: 0 through 9. Binary is called base 2 because it uses 2 digits: 0 and 1. Can you guess why hexadecimal is called base 16? It uses 16 digits: 0 through 9, and a through f.

Converting from Other Bases

But how do you convert to and from other bases? Converting from bases is simple. Let's say you have a number, 5F, and a base 16. How do we convert this to another base, like 10? It's simple, but it involves a bit of math. We start with the first digit, which we will call digit 0. We take the base, 16, and raise that to the power of the digit, 0. This gives us 1. This digit is F. That is the 15th digit, so we multiply our index, 16**0 or 1, and multiply that by the number of this digit. 1 * 15 is 15, so we add 15 to our output number. Then we move to the next digit. This one is digit 1, so we compute 16**1, giving us 16. We know 5 is the 5th digit, so we multiply 16 by 5. This is 80. We add this to our 15 to get an output of 95. Thus, 5F in base 10 is 95.

Converting to Other Bases

Converting to other bases is pretty much the same process. Let's say we wanted to convert 5 to base 2. We start out with an index for digit 0 of 2**0 or 1. We know with base 2 that every digit is either 0 or 1, so we can multiply our 1 by either 0 or 1. To convert to other bases, we try to subtract the highest possible multiple, so we subtract 1*1, or 1. Thus, we add 1 to our output, and subtract 1 from the number, leaving us with 4. Then we increase our digit index, so now we have 2**1, or 2. You might think we would subtract 2 now, but we actually do not. If we subtracted 2, we would be left with 2, but the base index would increase to 2**2 or 4, leaving us unable to subtract! So we add a 0 to our output, leaving us with 01. Now our index is 2**2 or 4, and we have 4 left. So we subtract 4, leaving us with 0, and add 1 to out output. Thus, 5 in base 2 is 101.

Automation

So as you can see, converting to and from other bases can be difficult, and you probably don't want to do it manually every time. Luckily, most languages contain functions to automate this. In Python, to convert a number from a base to base 10, you can use the int function. For example, to solve our above problem, you can use int('5F', base=16). Sadly, there are not builtin functions to convert to any base, only bases 2, 16, and 8. The functions for that are bin, hex, and oct, respectively. As well, in most languages, for denoting those three bases, you can use 0x, 0b, or 0o before the number, instead of an int function. You might use 0x5F rather than int('5F', base=16), for example.

End

Thanks for reading my tutorial! I hope you enjoyed it and learned a little about bases in doing so. I plan to release another tutorial soon, so be on the lookout for that. Thanks for reading!

You are viewing a single comment. View All
Highwayman (1092)

I made a translator to binary, but I used mod, is that a thing? I think it works :s
https://repl.it/@Highwayman/Binary-to-decimal-because-I-m-lazy
Edit: oops no I didn’t

sugarfi (266)

@Highwayman mod works. I explained it kinda stupidly in the article.

Highwayman (1092)

@sugarfi ok. Cool thnx! Yeah, ngl I was pretty confused by your explanation.

Highwayman (1092)

@sugarfi XD that about sums up my every answer on ask lol