Convert Hexadecimal and Binary in your head!
Tags: tutorial, computerscience
Published: 2020-06-29 03:09:41

# Why?
Being able to convert a number between hexadecimal and binary in your head is a really valuable skill in the world of computer science. Even if you don't regularly go down to that low of a level, it can be extremely helpful when learning about lower level systems that you are building on top of, and can be invaluable when you do need to work in binary/hex.

# How?
## Binary and hexadecimal?
Before we talk about *how* to convert between binary and hex, let's talk about *what* binary and hex are.

### Number Bases
A number base is the amount of values that can be stored in one digit. For example, in base 10 (aka what we usually think of numbers in) one digit can store 10 different values (0-9).

### Binary
Binary is a number system in base 2. This means each digit can only store 2 values (usually notated as a 1 or a 0). Because the base is so low, binary numbers can be extremely long and intimidating to read (the number 51 in binary is 00110011). In computer science, we will usually group these numbers into groups of 8 (aka a Byte).

### Hexadecimal
Similarly, hex is also just a number system with a different base: 16. How do we store more than ten values in a given digit? We use letters! In other words, the number 10 in hex becomes A, 11 becomes B, 12 becomes C, 13 becomes D, 14 becomes E, and 15 becomes F. This also means that in hex, the number 10 is actually the number 16 in base 10 (usually we would say 16 in decimal).

## The key relationship between hex and binary
The key trick to converting between hex and binary is to realize that one hex digit is perfectly equal to four binary digits. 

What do I mean by this? Take a look at the table below.

| Decimal | 0    | 1    | 2    | 3    | 4    | 5    | 6    | 7    | 8    | 9    | 10   | 11   | 12   | 13   | 14   | 15   |
|---------|------|------|------|------|------|------|------|------|------|------|------|------|------|------|------|------|
| Binary  | 0000 | 0001 | 0010 | 0011 | 0100 | 0101 | 0110 | 0111 | 1000 | 1001 | 1010 | 1011 | 1100 | 1101 | 1110 | 1111 |
| Hex     | 0    | 1    | 2    | 3    | 4    | 5    | 6    | 7    | 8    | 9    | A    | B    | C    | D    | E    | F    |

You may notice that every combination of four binary digits is neatly contained in one hex digit. This means that we can convert any arbitrarily large number by simply breaking it down into chunks of four binary digits.

To give you an example, if we know that the number 0101 in binary is equivalent to 5 in hex, and 1111 in binary is equivalent to F in hex, then we can easily determine that 01011111 in binary is equivalent to 5F in hex, and 11110101 binary would be F5 hex. It might be tough to know what F5 hex is in decimal (245), but we don't *need* to know that to do these conversions!

## Converting a single hex digit back and forth
Okay, so we know that we can convert an arbitrarily large number by breaking it into single hex digits, but how do we convert a single hex digit to its equivalent four binary digits and vice versa? Well, let's start with the easy ones.

### Powers of 2
Each of the four binary digits in isolation are a power of two. 

| Decimal | 0    | 1    | 2    | 4    | 8    |
|---------|------|------|------|------|------|
| Binary  | 0000 | 0001 | 0010 | 0100 | 1000 |
| Hex     | 0    | 1    | 2    | 4    | 8    |

If you see a series of four binary digits with only one 1, then you simply run right to left counting by powers of 2. This can be confusing at first, but I promise that it only takes a few times before you get really good at memorizing 0100 = 4 and 0010 = 2 and so forth.

### Filling in the other numbers
To fill in the gaps, we break up the number into a series of binary digits with only one 1 and add those values. What do I mean by that? Well, let's take a look at an example: 0101.

0101 can be broken into the numbers 0100 (4) and 0001 (1). We then add these two numbers to get our final value: 5.

Let's try again with the number 1011. This can be broken into 1000 (8), 0010 (2), and 0001 (1). These three numbers add to 11, which in hex would be B.

### Going the other way
To convert from hex back to binary, you go digit by digit, seeing if the number is larger than your current binary digit, and if so, subtracting it from the number.

This is sort of confusing and awkward to write, so let's just look at an example: D.

The first binary digit (1000) is 8. D is larger than 8, so we know that the first digit in binary is a 1.

We will subtract 8 from D, leaving us with 5, and move on to the next binary digit: 4 (0100).

5 is larger than 4, so we subtract four and mark our second digit as a 1.

The next binary digit is 2 (0010), which is larger than our current remainder, so we know that our third binary digit is a 0.

Lastly, we have the number 1 and our binary digit is 1, so we know that our last digit is a 1.

If you managed to follow this long-winded example, you should have ended up with the number 1101 for D!

A quick summary is as follows

| Remainder | Current Binary Digit | Binary Digits Figured Out |
|-----------|----------------------|---------------------------|
| D         | 1000                 | 1XXX                      |
| 5         | 0100                 | 11XX                      |
| 1         | 0010                 | 110X                      |
| 1         | 0001                 | 1101                      |

# Speeding it up
I find that manually running through each digit works really well in the beginning to get used to converting digits, but that it tends to be really slow. So, I've come up with a couple tricks to help me speed up the conversion process.

## Look for numbers that are +/- 1 from a power of 2
Numbers like 0101 (5) and 0111 (7) are really easy to get to from their nearest power of two.

## Think backwards from the top
1111 (F) is a very easy number to convert, and it can be helpful to simply subtract a given digit from it (i.e. 1101 (F - 2) is equal to D, 1011 (F - 4) is equal to B)

# Practice
I was hoping not to get too long-winded with this post, because I don't want to make it seem like converting between hex and binary is at all difficult. I promise it's not! With a little practice, you may find that you can pick it up in minutes.

To help you with that, here are some practice conversions. If you really need to, use a [binary to hex converter](https://www.rapidtables.com/convert/number/binary-to-hex.html), but please try and solve these on your own!

## Binary to hex
1. 01001100 11000011
2. 11100101 00101101
3. 10110011 10110100
4. 00110110 10010001

## Hex to binary
1. 3F C5
2. DD 13
3. 4A 72
4. 11 01

# Thank you
Thank you for taking the time to read through this, and please feel free to let me know if I messed something up in this post, or if you have a better way that you use to mentally convert between binary and hex!