Introduction to Binary

Karl Matthes
6 min readAug 31, 2020

Unless you’re doing something awfully specialized, I’d bet most of your code doesn’t explicitly use binary in it. Most of what you’re writing is probably closer to human languages, and if there’s any binary involved, it’s beneath layers of abstraction. But I think it’s still a useful topic to cover as it lets you learn a bit more about numerical systems, and a bit about boolean logic.

Binary is the name we use for the base-2 numeral system, and it has only two symbols: 0 and 1. Compare this to the more common decimal, or base-10, system with ten symbols: 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9.

Like decimal, binary also uses a positional system. In decimal, if you add 1 to 8, that position will be incremented to 9. If you then add 1 again, you need to move over one position to the left, increment that position by 1, and let the first position get set back to 0, to make 10. Each time a position reaches 9 and needs to be incremented, that position will wrap back around to zero, and the excess will be placed in the positions to the left of it. Binary does this as well, but because there are only two symbols to work with, it wraps around frequently, and as a result, and compared to decimal numbers representing the same value, binary numbers end up being quite long.

The left side is decimal numbers up to 10, and on the right are the binary equivalents. As you can see, you’re need to carry the one a lot. By the time decimal needs to add a new position, binary has already been using three extra positions. But there are a few patterns we can grab out of this. First, look at the instances and values where a new position is added in binary.

At 2, 4, and 8, we need to add a whole new position. This chart is getting rather big already, but if this continued, new positions would be added at 16, 32, 64, 128, … Or that is, for every power of 2, a new position would be added. With that information, let’s try adding 2 and 4 in binary, or that is 10+100.

Pretty straight forward addition, mostly dropping the 1’s down into place, and the result is 110. But if we compare this answer to the chart above, we can see that 6 is 110 in binary, so the value is remaining consistent between the two systems. We could then add a 1, in binary and decimal, and we’d have 7 and 111. We could then interpret 111 to equivalent to 100+10+1, or 4+2+1. If this pattern continues, with each place representing a new power of 2, it could be laid out like this:

A simple table, with a power of two for each column, and the 1’s and 0’s acting as true or false as whether or not that column should be counted. In this case, there are 1's in the columns for 2³ and 2¹, or 8 and 2. Added together those equal 10, and if we check the table from earlier, we can see that 10 is equal to 1010 in binary. There is an extra zero in the example above, making it 01010, but because it doesn’t change the value any, it is equivalent to 1010. Let’s try a longer table and try this with a larger number.

Our number for the example will be 350, and we want to figure out its equivalent in binary. What we’re really trying to do is figure out what combination of powers of 2 will add up to be 350, and there’s a pretty easy way of doing this: subtraction. For each column, we’ll try subtracting the power of 2 from the number we’re working on. If result is less than zero, that column will get a 0 in it, and we’ll skip to the next column. If not, we’ll place a 1 in that column, and then the resulting difference will be the new number to work with for the next column.

For the first column, 2⁸ is 256, and 350–256 is greater than zero. So, a 1 will go in the first column, and 350–256=94, so our new working number is 94.

Next, 2⁷ is 128, and 94–128 is less than zero. For this column, we’ll leave it zero, and move to the next column.

2⁶ is 64, and 94–64 is greater than zero, so we’ll mark the column with a 1, and our new working number will be 94–64, or 30.

2⁵ is 32, and 30–32 is less than zero, so this column gets a 0, and we continue to the next column.

2⁴ is 16, 30–16 is greater than zero, so mark it 1, and the new number is 14.

2³ is 8, 14–8 is greater than zero, add a 1, and 6 is the new number.

2² is 4, 6–4 is greater than zero, add a 1, and we’re left with 2.

2¹ is 2, and 2–2 is zero. At this point, we’ll mark this column with a 1, and then we can fill the rest of the columns in with 0’s, because we would be trying to subtract the remaining columns from zero, and that will always be less than zero.

To double check our work, we can check which columns have 1’s and add up those values to see if they equal 350. In this case, we have 2⁸+2⁶+2⁴+2³+2²+2¹, or 256+64+16+8+4+2, which does equal 350. With that, we’ve verified that 350 in binary is 101011110.

--

--