I have noticed that many beginners have trouble understanding binary, hex, octal, etc. I will attempt to show you how to use and understand any number system mainly using your pre-existing knowledge of how numbers work.
Author Assigned Level: Newbie
Community Assigned Level:
- Be able to count and do basic arithmetic
Basic Base 10 Counting
First I'm going to talk about how you learned to count. I know this is obvious, but stick with me. You probably learned to start counting at one, but that's not the correct way to count as we programmers know . To count, you just go to the next digit. 0, 1, 2, 3, 4, ..., 9. Once you get to nine, there is no next digit, you have to start using two digits to represent your number. So, add a new digit to the left of your number (it actually has always been there, it was just zero), set it to one, and reset all digits to the right back to zero. Now continue incrementing the right-most digit.
Every number system has a base aka radix. Decimal, the number system we have used since childhood, is base 10. This means it has 10 possible single digits (or 10 different characters if you want to think of it that way). The largest single digit in a number system will always be one less than the radix since zero is one of those possible single digits. Binary is base 2, meaning it only has two possible single digits: 0 & 1.
Counting in other number systems
Counting in other number systems works the same exact way as base 10.
Let's count in binary (base 2):
- Start at zero
- Increment by one
- Increment by one again. But wait, base two has no single digit for 2. That means we need to add a new digit and set the right digit back to zero
- Increment by one again
- Increment by one. Same issue as last time, just happens twice
- 11 + 1 = 100
- If this doesn't make sense to you: all the digits are at their maximum value, so we need a new digit and we reset all the digits before it back to zero.
Hopefully that made sense to you and you were able to count even higher on your own.
Let's try out octal (base 8) now:
- Start at zero
- Increment by one
- This is going to take a while, let's fast forward to seven
- Increment by one. Since this is base 8, seven is the largest single digit we have, so we need a new digit
- Let's fast forward again
- Here is a case we haven't seen yet. You might be tempted to go to 100, but that's wrong. We just increment the second digit and reset the digit to the right
- Let's fast forward really far now
- What's the next number here? I'll let you think about it and I'll put the answer in a spoiler
- 1300. You increment the third number and reset the two numbers to its right
Now let's go to hexadecimal aka hex (base 16):
This is the first number system we have looked at that has higher single digits than base 10. What symbol/character do we use after 9? Other characters do exist. For example, dozenal (base 12) uses X for ten and an upside-down three for eleven. However, in the computer realm we use the alphabet for simplicity's sake. Hex's digit set looks like: 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F.
I'm going to skip the explanations for this since you have already seen it for the past number systems:
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F, 10, 11, 12, 13, ...,
19, 1A, 1B, 1C, 1D, 1E, 1F, 20, 21, 22, ...., 29, 2A, 2B, 2C, .....,
9D, 9E, 9F, A0, A1, A2, A3, A4, ...., A9, AA, AB, AC, AD, AE, AF, B0
Binary, Octal, Decimal, Hex, etc all work the same. Once you understand the similarities, you can understand any number system.
Read Fust3rCluck's paper if you want to learn more about binary and hex. He explains it in a more technical way and shows how to convert between number systems.
- What comes after 11010111 in binary?
- What comes after 359 in hex?
- What comes after 347 in octal?
- What comes after B2F in hex?
- What comes after 2B in base 12?
- What comes after 5388 in base 9?
- Is b64 (aka base64) encoding a number system?
- Yes, it has 64 possible single digits
- Figure out this joke: There are 10 types of people: those who understand binary and those who don't.
- 10 is a binary number in this joke. b10 is 2 in decimal.
- Another joke: Only you, me, and deaf people understand hex.
- Deaf is a hex number (It would usually be written 0xDEAF). The joke also works with dead (0xDEAD)
- How would base 1 work?
Usually zero is the first digit in a number system, but that would mean you can't count higher than 0 in base 1. So, we will use 1 (or anything really) as our sole digit. Base 1 then is just the tally system. 0 is " " (nothing), 1 is "1", 2 is "11", 3 "111".
The roman numeral system could be viewed as base 1 with a fancy way of writing the number.