Lesson 5 | Hexadecimal numbers |
Objective | Convert between binary and hexadecimal numbers. |
Convert between binary and hexadecimal numbers(Operation)
One shortcoming in expressing numbers in binary form is that large numbers require a lot of digits. For example, the binary equivalent of the
decimal number 123,456,789 is 111010110111100110100010101
. To address this problem it is quite common to express binary numbers as hexadecimal numbers.
The hexadecimal number system is a base 16 number system. Each digit in a hexadecimal number can be one of the numerals 0, 1, 2, 3, 4,
5, 6, 7, 8, or 9, or one of the letters A, B, C, D, E, or F. Because the base of the hexadecimal number system is equal to 24
(since 16 is 2 to the 4th power), each digit in a hexadecimal number corresponds to four digits in the equivalent binary number. Here are the
first 16 hexadecimal numbers along with their binary and decimal equivalents.
Hexadecimal number system: Base 16 number system.
Hexadecimal |
Binary |
Decimal |
0
1
2
3
4
5
6
7
8
9
A
B
C
D
E
F
|
0000
0001
0010
0011
0100
0101
0110
0111
1000
1001
1010
1011
1100
1101
1110
1111
|
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
To convert from binary to hexadecimal simply replace each group of four binary digits with the corresponding hexadecimal digit. For example,
to convert the binary number 111010110111100110100010101
from the example above to hexadecimal, we write the digits in groups of four (from right to left), and then replace each group with the corresponding hexadecimal digit, like this:
Computer Science Structured Approach
0111 0101 1011 1100 1101 0001 0101
7 9 C B C 1 5
Thus the binary number
111010110111100110100010101
is equivalent to the hexadecimal number
79CBC15
.
If you like, you can convert
79CBC15
directly to decimal like this:
7 * 166 + 9 * 165 + 12 * 164 + 11 * 163 +
12 * 162 + 1 * 161 + 5 * 160
to verify that this is indeed the decimal number 123,456,789.
Converting from hexadecimal to binary is also quite easy. Simply replace each hexadecimal digit with the four corresponding binary digits.
For example, consider the hexadecimal number
3F2A
. Replacing each hexadecimal digit with the corresponding four binary digits we
have:
3 F 2 A
0011 1111 0010 1010
Thus the hexadecimal number 3F2A is equivalent to the 16-bit binary number 001111100101010
.
We have yet to consider how negative integers are stored. We'll explore this topic in the next lesson.
Hexadecimal Numbers - Quiz