Binary and Hexadecimal

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 10011101210011101_{2} .

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

ex. Write the binary number 110121101_{2} in decimal (base10):

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

Download file Copy to clipboard
11012=(123)+(122)+(021)+(120)1101_{2} = (1 * 2^3) + (1 * 2^2) + (0 * 2^1) + (1 * 2^0)
11012=8+4+0+11101_{2} = 8 + 4 + 0 + 1
11012=13101101_{2} = 13_{10}

We found that b1101 is equal to 13 in decimal. Let's try going the other way:

ex. Write the decimal number 11610116_{10} in binary:

The largest power of 2 that fits in to 116 is 6 ( 26=642^6 = 64 ), so we'll start in the 7th column (the 1st column represents 20=12^0 = 1 ):

Download file Copy to clipboard
11610=1______2116_{10} = 1\_\_\_\_\_\__{2}
116106410=5210116_{10} - 64_{10} = 52_{10}

The next power of 2 that fits in to our remainder of 52 is 25=322^5 = 32 :

Download file Copy to clipboard
11610=11_____2116_{10} = 11\_\_\_\_\__{2}
52103210=201052_{10} - 32_{10} = 20_{10}

The next power of 2 that fits in to our remainder of 20 is 24=162^4 = 16 :

Download file Copy to clipboard
11610=111____2116_{10} = 111\_\_\_\__{2}
20101610=41020_{10} - 16_{10} = 4_{10}

Now, we've reached a point where the next power of 2, 23=82^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:

Download file Copy to clipboard
11610=1110___2116_{10} = 1110\_\_\__{2}
410010=4104_{10} - 0_{10} = 4_{10}

The next power fits right in ( 22=42^2 = 4 ):

Download file Copy to clipboard
11610=11101__2116_{10} = 11101\_\__{2}
410410=0104_{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 :

Download file Copy to clipboard
11610=11101002116_{10} = 1110100_{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:

Download file Copy to clipboard
(right-hand-side in base10)
F116=(15161)+(1160)F1_{16} = (15 * 16^1) + (1 * 16^0)
F116=240+1F1_{16} = 240 + 1
F116=24110F1_{16} = 241_{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:

Download file Copy to clipboard
216=001022_{16} = 0010_{2}
F16=11112F_{16} = 1111_{2}
916=100129_{16} = 1001_{2}
A16=10102A_{16} = 1010_{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:

Download file Copy to clipboard
(2F9A)16=(0010 1111 1001 1010)2(2F9A)_{16} = (0010\ 1111\ 1001\ 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:

Download file Copy to clipboard
01102=6160110_{2} = 6_{16}
10112=B161011_{2} = B_{16}
(0110 1011)2=(6B)16(0110\ 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.