Sub topic 5
Binary, Octal and Hexadecimal by Grouping
Assume that you know place value, powers of 2, and how to convert between decimal and another base by repeated division. This chapter uses a faster method for three common bases: binary, octal, and hexadecimal.
Why grouping works
Binary has base 2. Each digit is a bit and can be only 0 or 1.
Octal has base 8, and:
\[ 8=2^3 \]
So one octal digit represents exactly three binary digits.
Hexadecimal has base 16, and:
\[ 16=2^4 \]
So one hexadecimal digit represents exactly four binary digits.
Because these bases are powers of 2, you can convert by grouping binary digits instead of doing decimal arithmetic.
Binary to octal
Starting from the right, split the binary number into groups of three. If the leftmost group is incomplete, add zeros on its left.
For example:
\[ 1101011_2 \]
Group the digits:
\[ 1\ |\ 101\ |\ 011 \]
Pad the first group:
\[ 001\ |\ 101\ |\ 011 \]
Now convert each group:
| Binary group | Value | Octal digit |
|---|---|---|
| 001 | 1 | 1 |
| 101 | 5 | 5 |
| 011 | 3 | 3 |
Therefore:
\[ 11010112=1538 \]
To check, expand the octal result:
\[ 153_8=1(8^2)+5(8^1)+3(8^0)=64+40+3=107 \]
The original binary number also equals 107.
Binary to hexadecimal
Starting from the right, split into groups of four and pad the leftmost group when needed.
\[ 1101011_2=0110\ |\ 1011 \]
The group \(0110\) equals 6. The group \(1011\) equals 11, which is represented by hexadecimal digit B.
Thus:
\[ 11010112=6B{16} \]
Hexadecimal uses letters for values 10 through 15.
| Decimal value | Hexadecimal digit |
|---|---|
| 10 | A |
| 11 | B |
| 12 | C |
| 13 | D |
| 14 | E |
| 15 | F |
Converting between octal and hexadecimal
There is no equal-size grouping between octal and hexadecimal, because 3 and 4 are different group sizes. The reliable shortcut is:
- Convert each octal digit to three binary bits.
- Regroup the full binary string into groups of four.
- Convert each group to a hexadecimal digit.
For example:
\[ 572_8 \]
Convert each octal digit:
\[ 5\to101,\qquad 7\to111,\qquad 2\to010 \]
So:
\[ 5728=1011110102 \]
Regroup into fours:
\[ 0001\ |\ 0111\ |\ 1010 \]
Therefore:
\[ 5728=17A{16} \]
The reverse process changes every hexadecimal digit into four bits, then groups those bits into threes for octal.
A quick programming check
def binary_to_hex(binary):
value = int(binary, 2)
return format(value, "X")
def binary_to_octal(binary):
value = int(binary, 2)
return format(value, "o")
bits = "1101011"
print(binary_to_octal(bits)) # 153
print(binary_to_hex(bits)) # 6BThe grouping method is usually faster by hand, while decimal conversion is a useful independent check.
Exercise
Convert 101111001011₂ to octal and hexadecimal.
Show answer
Group by threes: 101|111|001|011, giving 5713₈. Group by fours: 1011|1100|1011, giving BCB₁₆.