Sub topic 2
Binary, octal and hexadecimal by grouping
This chapter assumes you can do long division with remainders, know powers of 2 up to 2^10, and solve linear equations. A number in base n uses n digit symbols, and each position is worth a power of n.
Binary, octal and hexadecimal by grouping
Because 8 = 2^3 and 16 = 2^4, each octal digit matches exactly 3 binary digits, and each hexadecimal digit matches exactly 4. To convert, split the binary digits into groups from the right, then read each group as one digit.
For 101101, the groups of 3 are 101 and 101, giving octal 55. The groups of 4 are 0010 and 1101, giving hexadecimal 2D.
| Base | Bits per digit | 101101 written in this base |
|---|---|---|
| Binary | 1 | 101101 |
| Octal | 3 | 55 |
| Hexadecimal | 4 | 2D |
Adding, subtracting and multiplying in other bases
Use the same column method as in decimal, but carry at n instead of 10. In base 5, 34 + 42: the units give 4 + 2 = 6 = 1·5 + 1, so write 1 and carry 1. The fives give 3 + 4 + 1 = 8 = 1·5 + 3, so write 3 and carry 1. The result is 131 in base 5.
Finding an unknown base
If 23 in base b equals 17 in decimal, then 2b + 3 = 17, so b = 7. A second case: 121 in base b equals 100 in decimal. Since b² + 2b + 1 = (b + 1)², we need b + 1 = 10, so b = 9.
Fractional parts in other bases
Digits after the point have place values 1/n, 1/n², and so on. So 0.11 in base 2 equals 1/2 + 1/4 = 0.75, and 0.4 in base 5 equals 4/5 = 0.8.
Digits as algebra: reversals and digit relations
Write a two-digit number with tens digit a and units digit b as 10a + b. Its reversal is 10b + a. Suppose the digits sum to 9 and the reversal is 27 more than the number. Then 9(b − a) = 27, so b − a = 3. With a + b = 9, we get b = 6 and a = 3. The number is 36, and 63 − 36 = 27.
Check the base conversions and arithmetic with this code:
def base_to_decimal(digits, base):
value = 0
for ch in digits:
value = value * base + int(ch, 16)
return value
print(base_to_decimal("101101", 2)) # 45
print(base_to_decimal("2D", 16)) # 45
print(base_to_decimal("55", 8)) # 45
print(base_to_decimal("131", 5)) # 41Exercise
Convert 11010110 (base 2) to hexadecimal.
Show answer
Group the bits as 1101 and 0110. These give D and 6, so the answer is D6 in hexadecimal. Check: 13 × 16 + 6 = 214, and 128 + 64 + 16 + 4 + 2 = 214.