Course 1.1Lesson 2 of 3
Hexadecimal is the base 16 number system. One hexadecimal digit stands for four bits, so long binary numbers become short and easy to read. You convert between hexadecimal, binary and denary, and learn where computer scientists use hexadecimal.
Try the code and the simulations beside the lesson.
Look at the binary number 1011010011110010. It is hard to read, and very easy to copy wrongly. Hexadecimal (hex for short) writes the same number as B4F2. It is much shorter.
Hexadecimal is the base 16 system. It needs sixteen digits. It uses 0 to 9, and then the letters A to F for the values ten to fifteen.
Each place in hexadecimal is worth 16 times the place to its right. A two-digit hex number has the place values 16 and 1. A four-digit one has 4096, 256, 16 and 1.
Four bits can hold 0000 to 1111, which is 0 to 15. That is exactly one hexadecimal digit. A group of four bits is called a nibble. So one byte is two nibbles, and two hex digits.
The picture shows all sixteen nibbles with their denary and hex values. You do not need to learn it by heart. Work any nibble out with the place values 8, 4, 2 and 1.
Split the binary number into nibbles, starting from the right. Then write each nibble as one hex digit.
Convert 1011010011110010 to hexadecimal.
If the number of bits is not a multiple of four, add 0s at the left. For example 110101 becomes 0011 0101, which is 35 in hex.
Do the same thing backwards. Write each hex digit as four bits. Convert 7C: 7 is 0111 and C (12) is 1100, so 7C is 01111100.
Hex to denary. Multiply each digit by its place value and add. For 7C: .
Denary to hex. Divide by 16. The whole-number part is the first digit and the remainder is the second digit. For 200: remainder . So 200 is C8 in hex. Check: .
You can also go through binary. Convert 200 to binary (11001000), split it into nibbles (1100 1000) and write each as hex (C8).
For a four-digit hex number use the place values 4096, 256, 16 and 1. The largest 16-bit number, 65535, is FFFF.
The computer itself still works in binary. Hexadecimal is for people. It is a shorter way to write binary, so it is easier to read, to remember and to type without mistakes. It is also quick to change back to binary, one digit at a time. A denary number does not split into bits so neatly.
Here are some places where you meet hexadecimal.
The program converts a binary number to hex one nibble at a time, using a table of the sixteen digits. Then it checks the result with Python's own int(text, 16).
Try each one on your own first. If you are stuck, open a hint. Then check with Show the answer.
Every lesson is open to read. Enrolling also lets you:
Log in to join the discussion.
Loading…
This lesson is free to read. Enroll free to keep your place and take the tests.