Main content
Course: Computers and the Internet > Unit 1
Lesson 3: Hexadecimal numbersHexadecimal numbers
Binary numbers are a great way for computers to represent numbers. They're not as great for humans though—they're so very long, and it takes a while to count up all those s and s. When computer scientists deal with numbers, they often use either the decimal system or the hexadecimal system. Yes, another number system!
Fortunately, number systems are more alike than they are different, and now that you've mastered decimal and binary, hexadecimal will hopefully make sense.
In the decimal system, each digit represents a power of —the ones' place, the tens' place, etc. We call the base of the decimal system.
In the hexadecimal system, each digit represents a power of .
Here's how to count to 10 in hexadecimal: , , , , , , , , , .
That looks very familiar until the final "number" . You see, in the hexadecimal system, each digit needs to represent the values - , but the decimal numbers - don't fit into a single digit. To work around that, the hexadecimal system uses the letters - to represent the numbers - .
Here, let's count from 10 to 15: , , , , , . Strange but true!
There's actually been many different proposals over the years for how to represent the values 10-15, but - is the solution that won out.
Let's count higher now. Here's the decimal number , represented in hexadecimal as :
This number requires two digits, where the right digit represent the ones' place ( ) and the left digit represents the sixteens' place ( ). The same way that we added decimal and binary numbers, we can add up and see that hexadecimal equals the decimal value .
Let's try a number with a letter in it. Here's the decimal number , represented in hexadecimal as :
To understand this one, we need to first remember that represents the value . I do that by counting the letters on my fingers, but you can count however you'd like, as long as you remember to start with representing .
Then we can add it up like before and see that does equal the decimal value .
Why base 16?
At this point, you might be wondering what it is that computer scientists like so much about the hexadecimal system. Why use a system where we have to use letters to represent numbers? A little detour into history will show us why...
Early computers used 4-bit architectures, which meant that they always processed bits in groups of 4. That's why we still write bits in groups of 4, like when we write to represent decimal even though we could just write .
How many values can 4 bits represent? The lowest value is (all s, ) and the highest value is (all s, ), so 4 bits can represent 16 unique values. Aha, there's that number 16!
Each group of 4 bits in binary is a single digit in the hexadecimal system. That makes it really easy to convert binary numbers to hexadecimal numbers, and it makes it a natural fit for computers to use as well.
Converting binary to hexadecimal
Let's prove how well binary and hexadecimal get along by converting from binary to hexadecimal.
We'll start with a short binary number:
That's 4 bits long, which means it maps to a single hexadecimal digit. There's a in every place except the twos' place, so it equals the decimal number . The decimal and hexadecimal systems both represent the numbers - the same way, so is simply in hexadecimal.
Let's try a longer binary number:
One approach would be to figure out what decimal number is represented by that long string of s and s, and then convert that to hexadecimal. That approach would work, but gosh, it seems like a lot of work for this long of a number.
The easier approach is to convert each group of 4 bits, one at a time.
Starting with the left-most group of , that equals , the decimal number . That number is less than , so is simply in hexadecimal.
The next group is , or , the decimal value . That number requires a letter representation in hexadecimal, .
The next group is , or , the decimal value . That number is the same in hexadecimal, .
The final group is , or , the decimal value . That number requires a letter representation in hexadecimal, .
Our final hexadecimal result is . We did that conversion without ever understanding what decimal number is represented. I'll reveal now that both and equal the decimal number . That's an awfully big number, I'm glad we converted it one digit at a time.
Patterns in hexadecimal numbers
Let's build some intuition around the hexadecimal system.
First: for a given number of digits, what does the biggest number look like for those digits? In the decimal system, that's all s, like . In the binary system, that's all s, like .
In the hexadecimal system, that's when every digit has an in it, . When we see a number like that, we know that it represents the highest possible value for that number of digits. To represent any higher number, we'd need more digits.
Okay, now how much does a big number like that represent? We can use the same rule that we used for binary: the largest number that can be represented by a number of digits is the same as . Now that we're dealing with base 16 instead of base 2, we might need to bring out a calculator to figure that out though.
Here's a table of the first four largest values:
Digits | Highest hexadecimal number | Decimal equivalent |
---|---|---|
1 | ||
2 | ||
3 | ||
4 |
Uses for hexadecimal
In this unit on how computers work, we're mostly going to be dealing with binary numbers. It's important to understand the hexadecimal system as well though, because they'll pop up throughout later units and just generally, in the life of a programmer.
As one example from my life, web developers use hexadecimal numbers to represent colors. We describe colors as a combination of three components: red, green, and blue. Each of those components can vary from to . A color like blue can be written as unique colors—more than 16 million colors!
rgb(0, 0, 255)
or the more concise hexadecimal version, #0000FF
. Using that notation, we can describe Now that you know about hexadecimal numbers, keep your eyes out for them. You'll often see them written with an
0x
in front, like 0x4F
, or you might just recognize their distinctive mix of 0-9
with A-F
. If you spot any interesting uses for them in the wild, share your discovery below in the Tips & Thanks.🙋🏽🙋🏻♀️🙋🏿♂️Do you have any questions about this topic? We'd love to answer— just ask in the questions area below!
Want to join the conversation?
- I quote the article: "Now that you know about hexadecimal numbers, keep your eyes out for them. You'll often see them written with an 0x in front, like 0x4F"
Why are they often written with 0x in front?(39 votes)- thats a general label that means the following numbers and letters are hexadecimal, in the development of big softwares its important to label things properly as to not get confused between one programmer and the next(65 votes)
- I've seen base 64 used quite a bit as well, Why 64?
I suspect it has something to do with 64 being a power of 2, like 16 is a power of 2. Any thoughts?(17 votes)- Mainly due to 64 being a power of 2. I would also guess that because it just so happens that the total number of uppercase and lowercase letters and numbers is 62, we use base64 for text encoding.(16 votes)
- "#0000FF. Using that notation, we can describe 16^6 unique colors..."
Why is #000000 16^6 instead of 16^5? We start from 16^0, not 16^1.(5 votes)- I think it's easier to understand if we go back to binary a little:
With one bit you can represent two values (0 and 1). What happens if we add a second bit? For each value of this second bit, the previous bit can still be 0 or 1, so we essentially multiply the number of unique values by 2.
1 bit => 0 or 1
2 bits with the new bit being 0 => 00 or 01
2 bits with the new bit being 1 => 10 or 11
So in this case it's 2 * 2 = 4.
All of this is true for other number systems. In my example above we multiply by 2 because we are using the binary system.
Now in the hexadecimal system for the same multiplication we use 16. As one hexadecimal digit can represent 16 unique values, two digits can represent 16 * 16 = 256.
The general formula looks like this:
unique values = b^n,
where b is the base of the number system and n is the number of digits. What matters is the number of digits, not the the exponent of the base on the leftmost place.
Hope this helps!(19 votes)
- why is the total number of unique values that can be represented by 2 digits in hexadecimal FF = 256 instead of 272 (256 + 16)?(5 votes)
- Because there is no character to represent 16. Hexadecimal characters go from 0 to F (15). Since the F represents 15 the number FF is 255 because (15*16) + (15) = 255.(17 votes)
- how do i work out 9C36 to normal decimal numbers(3 votes)
- Hexadecimal numbers are base-16, whereas the decimal numbers that we are accustomed to are base-10. (9C36)_16 (using _16 to represent base-16) is equivalent to (39990)_10 (using _10 to represent base-10).
You can determine this result through the following process:
Pick apart each of the digits of the hexadecimal number, so that you have (starting at the last digit and going towards the first) 6 3 C 9. Recall that the character 'C' represents the number 12 in hexadecimal, so you have 6 3 12 9.
Now, multiply by powers of 16, starting from 0:
6 * 16^0 = 6 * 1 = 6
3 * 16^1 = 3 * 16 = 48
12 * 16^2 = 12 * 256 = 3072
9 * 16^3 = 9 * 4096 = 36864
Adding the results together... 6 + 48 + 3072 + 36864 = 39990.
This conversion process would hold regardless of how long the hexadecimal number is, just continue multiplying by incremental powers of 16 and then sum the results.(13 votes)
- What is said of the color in the computer?
Can you explain?(1 vote)- Colors can be encoded in hexadecimal, it's called the RGB encoding.
Imagine you have 3 lights: one red (R), one green(G) and one blue(B).
No each of those lights have switch with 256 (0 to 255) settings, where 0 means the light is off and 255 or FF in hexadecimal is the light is running on full power.
So we start by turning the lights all off
00 00 00 that means we get black
No we turn the green light all the on, so to FF in hexadecimal(255)
00 FF 00 that means we get a bright green
Now, because we paid attention in art class we turn on the red and the green light each to 150 (or 96 in hexadecimal) to get purple
96 00 96 that's some kind of purple
And the end we turn all three lights all the way on
FF FF FF and we get white(13 votes)
- The table shows the highest values for each number of digits. What is the total number of unique values that can be represented by 8 digits?(3 votes)
- 16^8 - 1, or 4,294,967,295.
The highest value that can be expressed increases exponentialy for each digit you add.
For any number of digits, just take the base you are in B, to the power of the number of digits D, and then subtract 1.
B^D - 1
Example: Base 12, 7 digits
12^7 - 1
35831808 - 1
35831807(4 votes)
- Are the English alphabet letters in hexadecimal used universally?(5 votes)
- stop yapping(0 votes)
- i want to do coding not binary(1 vote)
- man, this is vital without this you can't do a single thing. If you do not know what is going on the computer then how will you do coding or programming(6 votes)
- How would you get 34 in hexadecimal in decimal form(2 votes)
- Hexadecimal numbers are base-16, whereas the decimal numbers that we are accustomed to are base-10. (34)_16 (using _16 to represent base-16) is equivalent to (52)_10 (using _10 to represent base-10).
You can determine this result through the following process:
Pick apart each of the digits of the hexadecimal number, so that you have (starting at the last digit and going towards the first) 4 3.
Now, multiply by powers of 16, starting from 0:
4 * 16^0 = 4 * 1 = 4
3 * 16^1 = 3 * 16 = 48
Adding the results together... 4 + 48 = 52.
This conversion process would hold regardless of how long the hexadecimal number is, just continue multiplying by incremental powers of 16 and then sum the results.(4 votes)