# Binary, Decimal, Hexadecimal

# Representation of different bases, and converting to decimal

## Decimal

We think of decimal as the base-10 system. The set of all decimal digits is $\{0,1,2,3,4,5,6,7,8,9\}$.

Imagine we have a number 42 (base ten). We could rewrite it as each digit multiplied by a power of ten, like so: $42 = 4 * 10^1 + 2 * 10^0$.

## Binary

The set of all binary digits is $\{0,1\}$.

The same idea is true for binary numbers, also known as the base-2 system. Imagine we have a number 101010 (base two). We could rewrite it as each digit multiplied by a power of two, like so: $101010_2 = 1 * 2^5 + 0 * 2^4 + 1 * 2^3 + 0 * 2^2 + 1 * 2^1 + 0 * 2^0$. If we wanted to get the decimal version of this, we could just simplify: $1 * 2^5 + 1 * 2^3 + 1 * 2^1 = 32 + 8 + 2=42$.

## Hexadecimal

The set of all hexadecimal digits is $\{0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F\}$, where A-F have decimal values of 10-15 respectively.

The same idea is true for hexadecimal numbers, also known as the base-16 system. Imagine we have a number 2A (base sixteen). We could rewrite it as each digit multiplied by a power of sixteen, like so: $2A_{16} = 2 * 16^1 + 10 * 16^0$. If we wanted to get the decimal version of this, we could just simplify: $2 * 16^1 + 10 * 16^0 = 32 + 10 = 42$.

## Bringing it together: binary and hexadecimal to decimal

Above, we have seen that finding the value of a binary or hexadecimal number in decimal simply requires writing out each digit multiplied by its place value and then simplifying.

# Converting decimal to binary or hexadecimal by repeated division

A technique for converting to either binary or hexadecimal is to take the number in decimal and successively divide by either 2 or 16 (depending on the target base), using the remainders to construct the resultant number. Say we want to convert 42 to binary. We would start to divide by 2:

\(\require{enclose} \begin{array}{rll} 21\\\\[-3pt] 2 \enclose{longdiv}{42}\kern-.2ex \\\\[-3pt] \underline{42} \\\\[-3pt] 0 \\\\[-3pt] \end{array}\)

Now, we know that the rightmost digit of the resulting binary number is 0, as that is the remainder of the division. We continue by dividing the previous quotient again:

\[\require{enclose} \begin{array}{rll} 10\\\\[-3pt] 2 \enclose{longdiv}{21}\kern-.2ex \\\\[-3pt] \underline{20} \\\\[-3pt] 1 \\\\[-3pt] \end{array}\]The remainder is 1, so the next digit to the left is a 1. Continue dividing until the quotient is 0 (the intermediate three divisions are left as an exercise for the reader):

\[\require{enclose} \begin{array}{rll} 0\\\\[-3pt] 2 \enclose{longdiv}{1}\kern-.2ex \\\\[-3pt] \underline{0} \\\\[-3pt] 1 \\\\[-3pt] \end{array}\]The final remainder is 1, so the leftmost digit of the resulting binary number is 1. Writing all the results next to each other, we get a result of 101010.

The same technique works for converting to hexadecimal (simply divide by 16 repeatedly instead of 2). This technique generalizes to other bases as well.