Hexadecimal numbers
Introduction to the hexadecimal base-16 system.
Hexadecimal Numbers ๐ข๐ช
As we saw with Unicode, computers need to handle very large numbers to distinguish between the many possible characters and instructions. While computers work effortlessly with large binary numbers, we donโt! Converting 32-bit or 64-bit values from binary to decimal is tedious, which makes large binary numbers difficult for humans to read and write quickly.
Weโre comfortable with large decimal numbers, but the problem is that 10 is not a power of 2. This means it doesnโt align neatly with the binary storage model computers rely on. For example, with just 4 bits we can represent values up to 16, but to store something slightly larger, like 17โ20, we already need 5 bits.
The result is that decimal numbers cannot be directly stored in machines built from transistors that only distinguish between โonโ (1) and โoffโ (0). So whatโs the solution? We turn to hexadecimalโa system that maps perfectly onto binary while staying compact and much easier for humans to read and write.
Notice that 16 is a power of 2: \(2^4 = 16\). This means every 4 bits (a nibble) can store one hexadecimal digit (a โhexitโ), and with 8 bits we can store two โhexitsโ.
Hexits ๐น
The hexadecimal system is base 16 ๐ข. This means we count from 0 to 15 before moving to the next place value. In decimal, we only have the digits 0โ9 ๐. To show the extra numbers (10โ15), we use the letters A, B, C, D, E, and F ๐ .
| Hexadecimal | Binary | Decimal |
|---|---|---|
| 0 | 0000 | 0 |
| 1 | 0001 | 1 |
| 2 | 0010 | 2 |
| 3 | 0011 | 3 |
| 4 | 0100 | 4 |
| 5 | 0101 | 5 |
| 6 | 0110 | 6 |
| 7 | 0111 | 7 |
| 8 | 1000 | 8 |
| 9 | 1001 | 9 |
| A | 1010 | 10 |
| B | 1011 | 11 |
| C | 1100 | 12 |
| D | 1101 | 13 |
| E | 1110 | 14 |
| F | 1111 | 15 |
Building Hexadecimal Numbers ๐๏ธ
Binary to Hexadecimal 0๏ธโฃ1๏ธโฃ
Going from binary to hexadecimal and vice versa is quite straight forward.
๐ Steps:
Group the binary number in nibbles (packs of 4 bits) ๐ Example : 0101011011 โ 0001 0101 1011
Convert each nibble into its hexadecimal digit:
- (0001)2 โ (1)16
- (0101)2 โ (5)16
- (1011)2 โ (B)16
- Write the hex digits in the same order. โจ 0001 0101 1011 โ (15B)16
Hexadecimal numbers are often written with the prefix 0x to make their base clear:
0001 0101 1011 = 0x15B
โ Final Answer: 0x15B
Apply the same approach to convert a hexadecimal number to binary, each hex character will correspond to a nibble (4 bits).
๐ข Decimal to Hexadecimal
This is a little less straight forward but itโs still achievable.
๐ Steps: ๐ Example: Letโs convert 4,284 in hexadecimal:
- List a few powers of 16 and their decimal values:
| Power of 16 | \(16^4\) | \(16^3\) | \(16^2\) | \(16^1\) | \(16^0\) |
|---|---|---|---|---|---|
| Decimal | 1,048,576 | 4,096 | 256 | 16 | 1 |
Find the largest power of 16 that fits into 4,284. ๐ \(16^3 = 4,096\) (fits, since \(16^4\) is too big).
Divide by this power of 16. ๐ \(4284 รท 4096 = 1\) remainder 188. ๐น Next digit: 1
Use the next power of 16 (\(16^2 = 256\)). ๐ \(188 รท 256 = 0\) remainder 188. ๐น Next digit: 0
Move to the next power \(16^1 = 16\). ๐ \(188 รท 16 = 11\) remainder 12. ๐ Decimal 11 = Hex B. ๐น Next digit: B
Finally, \(16^0 = 1\). ๐ \(12 รท 1 = 12\) remainder 0. ๐ Decimal 12 = Hex C. ๐น Next digit: C
Write the hexadecimal digits in the order of largest to lowest powers:
โ Final Answer: 0x10BC
Riddle
Avatar Aang went to get his first driving license. The clerk asked Aang for his ID to make sure he was old enough to do the driving test. The clerk read his ID, and told him that he is not old enough for the test. They him that he needs to be at least 18.
Aang answered, โIโm sorry but you are wrong. I am exactly 18, in decimal. My ID in fact shows my age in Hexadecimal.โ
What age is Aang in Hexadecimal(if he is indeed 18 in decimal?)
