Photo by Vidar Nordli-Mathisen on Unsplash
Oh God. HEX! It’s just so hard to get your head around. A lot of people think that getting into programming means they’re going to have to learn how to speak “computer” by learning binary and write programs with it. When you get into it, there are very few times that you’ll actually need to use binary for every day tasks. Much more common however is Hexadecimal or Base16.
I’m going to try to give you a rundown on Hexadecimal and while I’m at it, I’ll swing back around to binary with what we’ve learned with Hex.
What we know now.
Our common numbering system that we use day to day is actually called base10 or Decimal. Each digit has 10 positions, zero to nine. if I post up a number like 38 the number in the right most is 8 which means there are, well, 8 in the “ones”
location. “Ones” is not a good thing to call this, because, as we’ll learn, that can change. It’s more accurate to call this the least significant digit or LSD for short. That means it’s the position that affects the overall number the least.
Numbers in this position have the least overall value. Consider the digits of our example. 38
the 8
digit may be larger than the 3
but it’s value is also affected by it’s position in the digit.
So 8
means 8
but the 3
means there are 30
. In English, we call this Thirty Eight but in a lot of other languages it’s called Thirty and Eight. This is the mindset you want to get into. Don’t
think of numbers as individual values, rather think of them as a combinations of digit values. In base 10, each digit above the LSD is a value of 10
of the digit immediately preceding it. 10
is worth 10
1
s,
30
is 3 times 10
1
s, and 100
is worth 10
, 10
s. The 10
is what’s important in base10. It informs when the number rolls over into the next digit.
Hexadecimal
Hexadecimal is also known as base16. In Hex, 1
equals 1
in base10, our well known number system. In fact 0-9 is the same in Hex and Decimal. but those are the only numbers that are the same. For instance. 10
in Hexadecimal is actually 16
in base10. But how does this happen? Well in Hexadecimal, there are single digit numbers that are higher than 9
. How though? You can’t just invent numbers like that. Well actually you
can. Base10 is just a way to record numbers. There’s no reason we couldn’t record numbers another way. Math gets different in different numbering systems but we’re not too worried about that, we just want to be able to record the same information
in less space. Hexadecimal’s LSD is recorded 0-15 like so.
Hexadecimal | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | A | B | C | D | E | F |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Decimal | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 |
Letters! Ok so, once you’ve gotten your head around that you can understand how F + 1 = 10
is the same as 15 + 1 = 16
then you have to start thinking about how 20
in hex is actually 32
in
base10.
Now we get to the last part you have to understand about base16. Remember how we said that 100
is 10
10
s? Well in base 16, 100
is 16
16
s. So in this example, FF + 1 = 100
and 100
in hex is 256
. This is the basics of every numbering system. the base
part of base16 means how many positions the LSD can be in before overflowing into the next digit. Binary is no different.
Binary is also called base2. 0
and 1
.
Binary
Knowing what we know in Hexadecimal, Binary should be more approachable. We don’t need to make up new digits because 0
and 1
already exist.
take a look at this.
Hexadecimal | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | A | B | C | D | E | F |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Decimal | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 |
Binary | 0 | 1 | 10 | 11 | 100 | 101 | 110 | 111 | 1000 | 1001 | 1010 | 1011 | 1100 | 1101 | 1110 | 1111 |
For a large margin all you’ll need to know about Hexadecimal will be 2 digits. FF
is usually the max number you’ll encounter because it just so happens to be a full byte of data. a byte is 8 bits. A bit is a single digit of a
binary number.
FF == 255 == 11111111
Lessons
This is a quick rundown of these numbering systems. To be completely honest, I’m still trying to get my head wrapped around it. The point is here, don’t be intimidated. It’s just a different way to store values in a more compact way, If I
have 1000 entries of 255, I’d much rather store two FF
s than three digits 255
2000 digits vs 3000 digits. More memory, more space.
If you have anything to add, or any questions, Drop me a line.