Hexadecimal
Hexadecimal is a way to represent a data based on 16 symbols:
0 - 9, A - F
(16 in total)
The reason is:
- Easier to read hexadecimal comparing to a binary system
- Easier to remember hexadecimal
- Some times can combind to make meaning full word (i.e
0xc0010ff
in MacOS when the computer is too hot —cool off
)
Convert from Binary to Hexadecimal
To convert from binary to hexadecimal, we use the system of 8 4 2 1
for each 4 group of binary number. To calculate the hexadecimal representation, we simply add the corresponding value of 8 4 2 1
for bit that's 1
For example
8 | 4 | 2 | 1 | Hexadecimal Representation | |
---|---|---|---|---|---|
0 | 0 | 0 | 0 | 0 | |
0 | 0 | 0 | 1 | 1 | |
0 | 0 | 1 | 0 | 2 | |
0 | 0 | 1 | 1 | 3 | |
0 | 1 | 0 | 0 | 4 | |
0 | 1 | 0 | 1 | 5 | |
0 | 1 | 1 | 0 | 6 | |
0 | 1 | 1 | 1 | 7 | |
1 | 0 | 0 | 0 | 8 | |
1 | 0 | 0 | 1 | 9 | |
1 | 0 | 1 | 0 | A | |
1 | 0 | 1 | 1 | B | |
1 | 1 | 0 | 0 | C | |
1 | 1 | 0 | 1 | D | |
1 | 1 | 1 | 0 | E | |
1 | 1 | 1 | 1 | F |
Note that to convert a binary, we can just sum the number of the toggled on bit (1
) to see what's the hexadecimal representation.
For example, given 0110
we can see that bit 4
and 2
is 1
. Therefore hexadecimal value is 0x6
.
When convert a long binary value, for example 1100111010011010
into hexadecimal:
- we divide into group of 4:
1100 1110 1001 1010
- Convert them into letter based on the table above:
C E 9 A
- Therefore the hexadecimal presentation is
0xCE9A
As a result, we can represent a long binary value to a much shorter value.
[!note]
In hexadecimal, we use the prefix0x
to denote that it's base 16 (is hexadecimal)
[!important]
Hexadecimals are often use to represent memory addresses in RAM or DISK because of this characteristic
General representation
Assume there are 5 heximal
0 | 1 | 2 | 3 | 4 | |
---|---|---|---|---|---|
Hexadecimal | A | B | C | 1 | 2 |
Base | $16^4$ | $16^3$ | $16^2$ | $16^1$ | $16^0$ |
Therefore in decimal it would be: $10 \times 16^{4}+ 11 \times 16^{3}+ 12 \times 16^{2} + 1 \times 16^{1}+ 2 \times 16^0$ = 703506 (decimal — base 10 — normal number)