Sub topic 5
Binary, octal and hexadecimal by grouping
This chapter assumes you can convert between decimal and base n using the place-value methods from the earlier chapters. It also assumes you know the powers of 2 up to 2^10 and can read hexadecimal digits A to F as 10 to 15. Here you will learn a shortcut that converts between bases 2, 8 and 16 without going through decimal.
Why grouping works
Base 8 is 2^3 and base 16 is 2^4. One octal digit therefore stands for exactly three binary digits, and one hexadecimal digit stands for exactly four.
Take the binary numeral 101101. Its place values from the right are 1, 2, 4, 8, 16, 32. Split it into two groups of three:
- The left group 101 sits in the places worth 8, 16 and 32. Its value is 1·32 + 0·16 + 1·8 = 8 · (1·4 + 0·2 + 1·1) = 8 · 5.
- The right group 101 sits in the places worth 1, 2 and 4. Its value is 5.
So the number is 5·8 + 5·1, which is 55 in base 8. Each group of three bits is a base-8 digit, because the group's place values are the same 1, 2, 4 pattern multiplied by a power of 8.
The same argument works for any pair of bases b and b^k. A group of k digits in base b is one digit in base b^k. This gives a general rule:
- Base 2 to base 4 uses groups of 2 bits.
- Base 2 to base 8 uses groups of 3 bits.
- Base 2 to base 16 uses groups of 4 bits.
- Base 3 to base 9 uses groups of 2 ternary digits.
- Base 5 to base 25 uses groups of 2 digits.
The shortcut does not work between bases that are not powers of a common base. For example, base 2 to base 5 has no grouping, and neither does base 8 to base 10. Use the decimal route for those.
The lookup table
You only need to know the sixteen four-bit patterns. The octal patterns are the first eight rows with the leading zero dropped.
| Decimal | 4-bit binary | Hex | 3-bit binary (octal) |
|---|---|---|---|
| 0 | 0000 | 0 | 000 |
| 1 | 0001 | 1 | 001 |
| 2 | 0010 | 2 | 010 |
| 3 | 0011 | 3 | 011 |
| 4 | 0100 | 4 | 100 |
| 5 | 0101 | 5 | 101 |
| 6 | 0110 | 6 | 110 |
| 7 | 0111 | 7 | 111 |
| 8 | 1000 | 8 | not used |
| 9 | 1001 | 9 | not used |
| 10 | 1010 | A | not used |
| 11 | 1011 | B | not used |
| 12 | 1100 | C | not used |
| 13 | 1101 | D | not used |
| 14 | 1110 | E | not used |
| 15 | 1111 | F | not used |
You do not need to memorize this table. Each pattern is the place values 8, 4, 2, 1 added up. For B (11), take 8 + 2 + 1, which is 1011.
Binary to octal or hexadecimal
- Start at the right end of the binary numeral, not the left.
- Cut it into groups of 3 (for octal) or 4 (for hex), moving leftward.
- If the leftmost group is short, pad it with zeros on its left.
- Replace each group by its digit.
Example: convert 1111010111 to hexadecimal.
- The length is 10, so the groups from the right are 0111, 1101 and 11.
- Pad the short group: 0011.
- Read each group: 0011 = 3, 1101 = D, 0111 = 7.
- The answer is 3D7.
For octal, the same bits give groups of three: 001 111 010 111. That reads as 1, 7, 2, 7, so the octal numeral is 1727.
SYMBOLS = "0123456789ABCDEF"
def group_convert(bits, k):
"""Convert a binary string to base 2**k by grouping k bits from the right."""
pad = (-len(bits)) % k
bits = "0" * pad + bits
out = []
for i in range(0, len(bits), k):
out.append(SYMBOLS[int(bits[i:i + k], 2)])
return "".join(out)
print(group_convert("101101", 3)) # 55
print(group_convert("101101", 4)) # 2D
print(group_convert("1111010111", 4)) # 3D7
print(group_convert("1111010111", 3)) # 1727
print(int("1111010111", 2), int("3D7", 16), int("1727", 8)) # 983 983 983The last line is a check. All three numerals have the value 983, so the grouping preserved the number.
Octal or hexadecimal to binary
Reverse the process. Replace each digit with its full block of bits. Every digit must use all k bits, including leading zeros inside the block.
Example: convert 7A3 from hexadecimal to binary.
- 7 becomes 0111, A becomes 1010, and 3 becomes 0011.
- Joined together: 011110100011.
- Drop the zeros at the very front of the whole numeral: 11110100011.
Only the zeros at the front of the whole numeral can be dropped. The zeros inside the blocks, such as the 00 in 0011, must stay. Dropping them shifts every later digit and changes the value.
def expand(numeral, k):
"""Convert a base 2**k numeral to binary by expanding each digit to k bits."""
bits = "".join(format(int(ch, 16), "0{}b".format(k)) for ch in numeral)
return bits.lstrip("0") or "0"
print(expand("2D", 4)) # 101101
print(expand("7A3", 4)) # 11110100011
print(expand("725", 3)) # 111010101Octal to hexadecimal, and back
Octal and hexadecimal do not group with each other directly, since 8 is 2^3 and 16 is 2^4. Use binary as the bridge.
Example: convert 725 (octal) to hexadecimal.
- Expand each octal digit to 3 bits: 7 = 111, 2 = 010, 5 = 101. This gives 111010101.
- Regroup from the right in fours: 1 1101 0101. Pad the left group to 0001.
- Read the groups: 0001 = 1, 1101 = D, 0101 = 5.
- The answer is 1D5.
Check in decimal: 725 in octal is 7·64 + 2·8 + 5 = 469. And 1D5 in hex is 256 + 13·16 + 5 = 469.
General grouping between any bases b and b^k
The rule is the same for bases other than 2.
Example: convert 2101 (base 3) to base 9.
- Group the ternary digits in pairs from the right: 21 and 01.
- 21 in base 3 is 2·3 + 1 = 7. And 01 in base 3 is 1.
- The base 9 numeral is 71.
Check: 2101 in base 3 is 2·27 + 1·9 + 0·3 + 1 = 64. And 71 in base 9 is 7·9 + 1 = 64.
Fractional parts
For digits after the radix point, start at the point and move rightward. Pad the last group with zeros on its right, not its left.
- 101.11 in binary: the integer part 101 gives 5. The fractional part 11 is padded to 110, which is 6. So the octal numeral is 5.6. Check: 5 + 6/8 = 5.75, and 101.11 in binary is 4 + 1 + 1/2 + 1/4 = 5.75.
- 0.1011 in binary is exactly one group of four, so it is 0.B in hex. Check: 11/16 = 0.6875 = 1/2 + 1/8 + 1/16.
Common errors
- Grouping from the left. For 1101, grouping from the left gives 110 and 1, which reads as 6 and 1. The correct grouping from the right is 001 and 101, giving 15 in octal. Check: 1101 in binary is 13, and 15 in octal is 13.
- Padding on the wrong side. Pad the integer part on its left. Pad the fractional part on its right. Padding the integer part on its right multiplies the number by a power of the base.
- Dropping interior zeros when expanding, as described above.
- Using the wrong block size. Octal needs 3 bits and hex needs 4. A mix-up produces digits that look plausible but are wrong. Check that no octal digit is 8 or 9.
- Misreading letters. In hex, A to F are 10 to 15, so D is 13, not 4.
When to group and when to use decimal
Grouping is fastest when both numerals are in bases 2, 4, 8 or 16, or when the question asks for the number of digits or the leading digit in such a base. If the question gives a binary numeral and asks for the hex numeral, grouping takes seconds. If it asks for the decimal value of a hex numeral, expand by place value instead.
The digit counts also help in estimation. A 12-bit number needs 12/4 = 3 hex digits and 12/3 = 4 octal digits. The largest 12-bit number, 111111111111, is FFF in hex, and the largest 3-digit hex numeral is FFF = 4095 = 2^12 − 1.
Exercise
Convert the binary numeral 11010111101 to hexadecimal and check your answer in decimal.
Show answer
Group from the right in fours: 0110 1011 1101 (the left group 110 is padded to 0110). The digits are 6, B and D, so the answer is 6BD. Check: the binary numeral equals 1024 + 512 + 128 + 32 + 16 + 8 + 4 + 1 = 1725, and 6BD in hex is 6·256 + 11·16 + 13 = 1536 + 176 + 13 = 1725.
Exercise
Convert 5471 from base 8 to base 16 without using decimal.
Show answer
Expand each octal digit to 3 bits: 5 = 101, 4 = 100, 7 = 111, 1 = 001. The binary numeral is 101100111001. Group in fours: 1011 0011 1001, which reads as B, 3 and 9. The answer is B39. Check: 5471 in base 8 is 2560 + 256 + 56 + 1 = 2873, and B39 in hex is 2816 + 48 + 9 = 2873.
Exercise
Convert 0.1101 from binary to octal. Then say whether grouping could convert a base 5 numeral to base 10.
Show answer
Group the fractional bits from the point in threes and pad on the right: 110 and 100, which are 6 and 4. The answer is 0.64 in octal. Check: 0.1101 in binary is 1/2 + 1/4 + 1/16 = 13/16 = 0.8125, and 0.64 in octal is 6/8 + 4/64 = 0.75 + 0.0625 = 0.8125. Grouping cannot convert base 5 to base 10, because 10 is not a power of 5.