Section 5.3 Octal and hexadecimal numbers
We will turn our attention to two useful abbreviations of binary numbers: octal (base-\(8\)) and hexadecimal (base-\(16\)). Octal numbers are used to issue commands to UNIX systems, and hexadecimal numbers are typically used to encode digital colors. The method we use to abbreviate binary numbers into octal and hexadecimal numbers can be applied more generally to other bases.
The octal digits are \(0, 1, 2, \ldots, 7\text{.}\) (In general, as you may have realized, the digits in base \(b\) are \(0\) through \(b-1\text{.}\)) Because \(8 = 2^3\text{,}\) we can compress a binary number into octal by grouping the digits into groups of threes, starting from the right, and writing each group as the corresponding octal digit.
Example 5.3.1.
Write the number \(486\) in octal.
First, convert \(486\) to binary. Using the methods of the previous section, confirm for yourself that \(486\) in binary is \((111100110)_2\text{.}\)
Next, group the digits of the binary number into groups of three:
Convert each to the corresponding octal number. We are treating each as a three-digit binary number, so the first (right-most) digit is the ones place, the second digit is the twos place, and the last (left-most) digit is the fours place. Therefore, \(110\) corresponds to the octal digit \(6\text{,}\) because it is \(1\times 4 + 1 \times 2 + 0 \times 1\text{.}\)
The group \(100\) corresponds to the octal digit \(4\text{,}\) and the group \(111\) corresponds to \(7\text{.}\) Therefore, \(486\) in octal is \((746)_8\text{.}\) Notice that the notation confirms it's not the number seven hundred forty-six.
We run into a small issue with hexadecimal: we do not have sixteen different numerical digits to use. So, we appeal to the alphabet. The hexadecimal digits are \(0, 1, 2, \ldots, 8, 9, A, B, C, D, E, F\text{,}\) where \(A\) represents \(10\text{,}\) \(B\) represents \(11\text{,}\) so on, and \(F\) represents \(15\text{.}\) We group a binary number into groups of four digits because \(16=2^4\text{.}\)
Example 5.3.2.
Write the number \(1513\) in hexadecimal.
First, write \(1513\) in binary. Its representation is \((10111101001)_2\text{.}\) You may notice that these digits do not split evenly into fours. However, adding zeros to the left end of a number does not change its value. So we will simply do this to get a number of digits that divides cleanly into four.
The first three digits correspond to one, two, and four again, and the last digit corresponds to eight. So, \(1001\) is \(1 \times 8 + 0 \times 4 + 0 \times 2 + 1 \times 1 = 9\text{.}\)
The second group is \(1110\text{,}\) which is \(8+4+2=14\text{.}\) The hexadecimal digit that corresponds to \(14\) is \(E\text{.}\)
Finally, \(0101\) is \(5\text{,}\) so \(1513\) in hexadecimal is \((5E9)_{16}\text{.}\)
Example 5.3.3.
Write the number \((80F)_{16}\) in decimal.
This means, write it as one of our usual base-\(10\) numbers. There are two ways to do this. The first is to write it as sums of powers of the given base. In this case, that's base-\(16\text{,}\) so we can write
(since the \(F\) represents \(15\)). If you have a calculator, you can quickly sum that to be \(2063\text{.}\)
However, passing to binary first can simplify the computation, since powers of \(2\) are not as difficult to add on paper or in your head. The hexadecimal digit \(8\) turns into \(1000\) in binary, \(0\) is \(0000\text{,}\) and \(F\) is \(1111\text{.}\) Therefore the binary expansion of this number is
which is
This abbreviation method works becuase \(8\) and \(16\) are powers of \(2\text{.}\) It generalizes to work any time you want to write a number already in base \(b\) in base \(b^k\) for some positive integer \(k\)—group the base-\(b\) digits into groups of \(k\text{.}\)
Example 5.3.4.
In the previous section we wrote \(1143\) in base-\(5\) as \((14033)_5\text{.}\)
To write \(1143\) in base-\(25\text{,}\) we can group the base-\(5\) digits in groups of two since \(25=5^2\text{.}\)
We will need many more digits. Let \(A\) be \(10\text{,}\) \(B\) be \(11\text{,}\) etc.
The first digit is the base-\(25\) number \((33)_{25}\text{.}\) Note it is not thirty- three! The right \(3\) represents three \(1\)s and the left \(3\) represents three \(5\)s. Since \(3\times 5 + 3 \times 1 = 18\text{,}\) this is \(I\) in our set of digits.
The base-\(25\) number \((40)_{25}\) is \(4\times 5 + 0 \times 1 = 20\text{,}\) which is represented by \(K\text{,}\) and \((01)_{25}\) is represented by \(1\text{.}\) So, \((14033)_5\) is \((1KI)_{25}\text{.}\)