### Binary

Binary is another name for a base2 number system, meaning each column represents
a power of 2, and the **radix** (the number of unique digits in the system)
is 2. So each column/place in a binary number can have one of two values:
`0`

or
`1`

. In programming, binary numbers (binary *literals*) are often represented
by putting a
`b`

out front:
`b10011101`

is the equivalent of
`$1001110{1}_{2}$`

.

Let's start by converting a binary number to decimal, using the same technique used earlier:

**ex.** Write the binary number
`$110{1}_{2}$`

in decimal (base10):

*Recall that the right hand side (where we do our math) is in base 10*

```
$110{1}_{2}=(1\ast {2}^{3})+(1\ast {2}^{2})+(0\ast {2}^{1})+(1\ast {2}^{0})$
$110{1}_{2}=8+4+0+1$
$110{1}_{2}=1{3}_{10}$
```

We found that
`b1101`

is equal to
`13`

in decimal. Let's try going the other
way:

**ex.** Write the decimal number
`$11{6}_{10}$`

in binary:

The largest power of 2 that fits in to 116 is 6 (
`${2}^{6}=64$`

), so we'll
start in the 7th column (the 1st column represents
`${2}^{0}=1$`

):

```
$11{6}_{10}=1\_\_\_\_\_{\_}_{2}$
$11{6}_{10}-6{4}_{10}=5{2}_{10}$
```

The next power of 2 that fits in to our remainder of 52 is
`${2}^{5}=32$`

:

```
$11{6}_{10}=11\_\_\_\_{\_}_{2}$
$5{2}_{10}-3{2}_{10}=2{0}_{10}$
```

The next power of 2 that fits in to our remainder of 20 is
`${2}^{4}=16$`

:

```
$11{6}_{10}=111\_\_\_{\_}_{2}$
$2{0}_{10}-1{6}_{10}={4}_{10}$
```

Now, we've reached a point where the next power of 2,
`${2}^{3}=8$`

is too
big to fit into our remainder. We fill it's place with a 0 and move on to the
next power:

```
$11{6}_{10}=1110\_\_{\_}_{2}$
${4}_{10}-{0}_{10}={4}_{10}$
```

The next power fits right in (
`${2}^{2}=4$`

):

```
$11{6}_{10}=11101\_{\_}_{2}$
${4}_{10}-{4}_{10}={0}_{10}$
```

It also leaves us with
`0`

for a remainder. Since the only multiple of 0 is
0, we can be pretty confident that there are no remaining powers of two that
will fit into our answer. Thus, we can fill the rest of the blanks with
`0's`

:

```
$11{6}_{10}=111010{0}_{2}$
```

### Hexadecimal

Hexadecimal, or just 'hex', is the most common number system other than decimal that you will see in programming. Hexadecimal numbers are written in base16; having a radix of 16 allows for larger numbers to be written with less symbols, but also means that you need at least 16 different symbols to write these numbers. Using arabic numerals, we only have 10 digits, so we fill the remaining symbols in with the letters A-F.

That means that each column in a hexadecimal number can take on a value between 0 and F, with the following equivalence:

base16 | base10 |
---|---|

0-9 | 0-9 |

A | 10 |

B | 11 |

C | 12 |

D | 13 |

E | 14 |

F | 15 |

In programming, or computing in general, hex numbers can be written in a number
of ways. Most commonly, they're hexadecimal literals are written as
`0x3A`

,
with the leading
`0x`

signifying that the following is a base16 number. The
leading
`0`

may also be replaced with a slash, or other character, but generally
there is an
`x`

that lets you know the number is in hex. Because hexadecimal is
so common in computing, oftentimes there won't be any sign at all that a number
is written in hex- but you can usually tell from the context or the mix of
numbers and letters A-F.

Let's do a quick conversion, hexadecimal to decimal as an example:

**ex.** Write
`0xF1`

in decimal:

```
(right-hand-side in base10)
$F{1}_{16}=(15\ast 1{6}^{1})+(1\ast 1{6}^{0})$
$F{1}_{16}=240+1$
$F{1}_{16}=24{1}_{10}$
```

Notice that we used 1 character less when writing the number in hexadecimal versus decimal. If we had a lot of numbers to store (millions or billions) as text in something like a document, it would be much more space efficient to store them as hex rather than decimal.

### Binary to Hex and Back

The relationship between base2 (binary) and base16 (hex) makes it extremely simple to convert back and forth between the two. Each column in hexadecimal can be comprised of 4 columns of binary and vice-versa. Converting between the two is as easy knowing the numbers 0-15 in both binary and hex.

decimal | hex | binary |
---|---|---|

0 | 0 | 0000 |

1 | 1 | 0001 |

2 | 2 | 0010 |

3 | 3 | 0011 |

4 | 4 | 0100 |

5 | 5 | 0101 |

6 | 6 | 0110 |

7 | 7 | 0111 |

8 | 8 | 1000 |

9 | 9 | 1001 |

10 | A | 1010 |

11 | B | 1011 |

12 | C | 1100 |

13 | D | 1101 |

14 | E | 1110 |

15 | F | 1111 |

**ex.** Convert
`0x2F9A`

to binary:

We'll just look at each column one at a time, and use the chart to convert:

```
${2}_{16}=001{0}_{2}$
${F}_{16}=111{1}_{2}$
${9}_{16}=100{1}_{2}$
${A}_{16}=101{0}_{2}$
```

After we have the binary number for each hex digit, write them in order to obtain the complete binary equivalent. Note that it's standard to put a separator (in this case, a space) every 4 digits in binary to help with readability:

```
$(2F9A{)}_{16}=(0010\text{}1111\text{}1001\text{}1010{)}_{2}$
```

To convert a binary number to hex, the same strategy applies, but in reverse. The binary number is broken up into groups of 4, and then the equivalent hex value is substituted:

**ex.** Convert
`b1101011`

to hexadecimal:

First note that there are only 7 digits in the binary number. To be able to
separate it into 2 groups of 4 digits, we can simply add a
`0`

out front, then
do the conversion:

```
$011{0}_{2}={6}_{16}$
$101{1}_{2}={B}_{16}$
$(0110\text{}1011{)}_{2}=(6B{)}_{16}$
```

If you've been around computers enough, you may have noticed that a lot of numbers occur in powers of 2: you might have a 256 gigabyte SSD in your computer, or 2048 bytes of SRAM on an Arduino's microcontroller. Binary, and thus powers of 2, is the base number system with which all modern computers operate. In the final block, we'll discuss the very basics of why that is.