Binary and hex conversion practice
Binary places are worth 1, 2, 4, 8 … doubling to the left; each hex digit (0-F) is exactly 4 bits.
8-bit binary to decimal.
Convert the binary number 01110111 to decimal.
How to solve these
Binary to decimal. Add the place values (128, 64, 32, 16, 8, 4, 2, 1) where there is a 1.
Decimal to binary. Take the biggest place value that fits, subtract, and repeat.
Hex. A = 10 … F = 15. Each hex digit maps to 4 bits, so convert hex ↔ binary one digit at a time.
Two's complement. In 8 bits the top bit is worth −128, so a pattern starting with 1 is negative: value = unsigned − 256.
Bitwise. Line the bits up and apply AND, OR or XOR column by column.
Worked example
Which 8-bit binary number equals 84?
Answer: 01010100. Take the biggest place value that fits, subtract, repeat: 64 + 16 + 4 = 84, so 01010100.
Show the working
- Write down what is given: 84, and the 8 place values 128 64 32 16 8 4 2 1.
- Take the biggest place value that fits, subtract it, and repeat with what is left: 84 − 64 = 20, 20 − 16 = 4, 4 − 4 = 0.
- Write 1 under each place value you used (64, 16, 4) and 0 under the others.
- Check: 64 + 16 + 4 = 84. Answer: 01010100.
Questions
Why is hex used?
It is a short way to write binary: two hex digits are exactly one byte (8 bits).
What range does 8-bit two's complement cover?
From −128 (10000000) to +127 (01111111).