Mathematics RU

Practice · Chapter 1

Converting between number bases

Binary, octal, hexadecimal and bases from 3 to 9: convert to decimal and back, and between 2, 8 and 16 directly, in groups of bits.

How to solve it

In decimal a digit is worth what its place allows: in $352$ the three means three hundreds. In base $b$ it works the same way, only the places are powers of $b$: ones, $b$, $b^2$, $b^3$ and so on.

Step by step

  1. To decimal: multiply each digit by the value of its place ($b^0, b^1, b^2, \dots$ from right to left) and add.
  2. From decimal: divide the number by $b$ with a remainder, then the quotient by $b$ again, until the quotient is zero. The remainders read from the bottom up are the digits.
  3. Digits above nine in hexadecimal are letters: $A = 10$, $B = 11$, …, $F = 15$.
  4. Between $2$, $8$ and $16$ decimal is not needed: one octal digit is $3$ bits, one hexadecimal digit is $4$ bits. Count the groups from the right.
The digits, each from $0$ to $b - 1$. The base. The place number counted from the right starting at zero: it is the exponent. Example: $312_4 = 3 \cdot 16 + 1 \cdot 4 + 2 = 54$. Back: $54 : 4 = 13$ (rem. $2$), $13 : 4 = 3$ (rem. $1$), $3 : 4 = 0$ (rem. $3$) — bottom up, $312$.

Common mistakes

  • Reading the remainders top down gives the number backwards. The last remainder is the leading digit.
  • Starting the place values at $b^1$ instead of $b^0$: the rightmost digit is worth $1$.
  • Grouping bits from the left instead of the right: $101110000_2$ in fours from the right is $1\,0111\,0000$, that is $170_{16}$.
  • Writing a digit equal to the base: binary has no digit $2$, octal has no $8$.

Example