# How To Understand Binary

Okay, so you probably kind of know what binary is - zeros and ones. But you’re probably also thinking "yeah, but that is literally all I know about it."

Well, I promise that in this post I will teach you how to understand how binary numbers work, using exactly the same methods that taught you how normal numbers work.

What’s more, you’ll also be able to understand hexadecimal, octal, and pretty much any other numbering system you come across.

## What does binary mean?

Okay, first thing’s first, what does the word “binary” actually mean?

Well, it’s a word that means there’s two of something. So, for example, a binary election would be one that’s contested by only two candidates. You can vote for Candidate A, or Candidate B, but there are no other choices. There’s no Candidate C. Sorry, Kanye.

Another example is a “binary star”, which is actually two stars orbiting each other.

When we’re talking about binary in the context of computers, we’re talking about a number system where there are only two digits. They are - you guessed it - zero and one.

### Why Use Binary?

The reason why binary is so important and helpful in computing is that 1 and 0 can correspond to lots of other concepts that exist in simple electronics. For example:

• "Is the power on or off?"
• "Is there a current in this circuit, or not?"
• "Is there a positive charge or a negative charge?"
• "Is the switch open or closed?"

From there, you can combine things to make (slightly) more complex ideas:

• “Is there a current in this first circuit, AND NOT a current in that second one?”
• “Are the first switch AND the second switch both off?”

And on and on. Combining many of these questions together (like, millions), and then acting on the answers, is basically how computers work. They’re just really, really complicated yes/no machines. Seriously.

In everyday life, we have ten digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9.

But in binary, there’s only two: 0 and 1.

## How to add up normal numbers

Okay, so here’s where we have to go back to school lessons when you were about 8 years old or whatever. We’re going to add 144 to 169, but first let’s look at the numbers themselves.

```H = Hundreds, T = Tens, U = Units

H T U
1 4 4

H T U
1 6 9
```

So, as we can see, the number 144 has 1 hundred in it, 4 tens, and 4 units:

```1 hundred + 4 tens + 4 units = 144

100 + 40 + 4 = 144
```

And the number 169 has 1 hundred, 6 tens, and 9 units:

```1 hundred + 6 tens + 9 units = 169

100 + 60 + 9 = 169
```

With me so far? Great. I know this all seems very obvious, and you’re thinking “I came here to learn binary, I already know how to add up,” but it’ll all make sense in a minute.

Now, to add them together:

```  H T U
1 4 4
+ 1 6 9
= 3 1 3
1 1
```

So first we added the units column up, ignoring the other two columns:

`4 + 9 = 13`

But 13 is more than 10, so what we’ve actually got is a number with 1 ten and 3 units. So we put the 3 in the units column, and put a 1 in the tens column, so we can remember to add it when we’re adding up that column.

Now we do the tens column, ignoring the other two columns:

```4 + 6 + 1 = 11
```

We added the 4 and the 6 from the “tens” column, and also the 1 ten that we carried over from before, when we added up the units. The result is 11, but again, that’s actually 1 ten and 1 unit. So we write the 1 unit into the answer box, and carry the 1 ten that we’ve got over to the hundreds column (because ten tens is a hundred).

Finally, we add the hundreds column:

```1 + 1 + 1 = 3
```

We’ve got 3 hundreds in the hundreds column (two from our starting numbers, and one that we carried over just now). So we put 3 in the answer box.

There’s no “thousands” column, so now we’re done. Our answer is 313. That is:

```300 + 10 + 3 = 313
```

## “Okay, I added some numbers up, can you teach me about binary now please?”

Okay, sure.

So our number 144 that we were just working with looks like this in normal numbers (that is, “decimal numbers”):

```H T U
1 4 4
```

But it looks like this in binary:

```128 64 32 16 8 4 2 1
1  0  0  1 0 0 0 0
```

The number 144 has 1 “one-hundred-twenty-eight” in it, and 1 “sixteen” in it.

“But hang on a minute,” I hear you say. “What’s with all these numbers across the top? Where do they come from?”

Great question, and this is where the magic lies.

In decimal numbers, we have ten digits. Every time we go from one column to the next column on the left, we’re multiplying by ten. We just take the previous number, and multiply it by ten.

```        1 = 1
1 × 10 = 10
10 × 10 = 100
100 × 10 = 1000
1000 × 10 = 10000 ... and so on
```

We multiply by 10 each time because there are 10 digits in the decimal system (0 through 9).

But in binary, we don’t have ten digits. We only have two. So instead of multiplying by ten when we go across one column, we multiply by two:

```    1 = 1
1 × 2 = 2
2 × 2 = 4
4 × 2 = 8
8 × 2 = 16 ... and so on
```

So binary numbers, instead of being made up of ones, tens, hundreds, thousands, and so on, are made up of ones, twos, fours, eights, sixteens, and so on.

## Let’s do some adding up, but pretend to be robots

Okay, bleep bloop, I’m a robot. Bloop bleep, you are also a robot. Let’s add up some numbers, because that’s what robots do.

Here's our first number:

```128 64 32 16 8 4 2 1
1  0  0  1 0 0 0 0
```

(Yes, it’s 144 in human, but shhh, we’re pretending to be robots.)

Here’s our second number:

```128 64 32 16 8 4 2 1
1  0  1  0 1 0 0 1
```

(Yes, it’s 169, because 1 + 8 + 32 + 128 = 169, but shhhhhh.)

Now let’s add them up:

```  128 64 32 16 8 4 2 1
1  0  0  1 0 0 0 0
+   1  0  1  0 1 0 0 1
=   ?  0  1  1 1 0 0 1
```

Uh oh, we’ve got two ones in the highest value (leftmost) column! What are we gonna do? We know 1 + 1 is 2, but we don’t have a 2 in binary!

Well, imagine if we were adding up good old human decimal numbers, back when we were humans. The similar situation there would be if we were adding 5 + 5 so we ended up with 10.

In that case, we can only write one digit for the units column, so we carry the 1 over to the next column (which is like the current column, only multiplied by ten), and then write the remaining 0 in our current column:

```  10 1
0 5
+  0 5
=  1 0
1
```

But we don’t have another column yet! That’s okay. We can make one. We know that we can just multiply our current column by two (because there’s two digits in binary, not ten digits) to get the next column:

```      1 = 1
1 × 2 = 2
2 × 2 = 4
4 × 2 = 8
8 × 2 = 16
16 × 2 = 32
32 × 2 = 64
64 × 2 = 128
128 × 2 = 256
```

Great! So now we have a 256 column to use!

```  256 128 64 32 16 8 4 2 1
0   1  0  0  1 0 0 0 0
+   0   1  0  1  0 1 0 0 1
=   1   0  0  1  1 1 0 0 1
1
```

We carry over the 1, and put it underneath, in the 256 column where it belongs. That leaves us with 0, so we put that in the 128 column we’re working on. In our new 256 column, we just have the 1 we carried over - neither of our starting numbers had a 256 in them - so that’s that. So now we have our finished sum!

```256 128 64 32 16 8 4 2 1
1   0  0  1  1 1 0 0 1
```

Which is the same as:

```256 + 32 + 16 + 8 + 1 = 313
```

## Is that all there is to binary?

There’s a few more things that are helpful to know, but in terms of understanding how it works, that’s pretty much it, yeah. You’ve done the hard bit.

There’s a couple of words it’s good to understand the meaning of though. In binary, each 0 or 1 is called a “bit”. A “byte” is made up of 8 “bits”. So the largest number you can represent with 1 “byte” is 255:

```128 64 32 16 8 4 2 1
1  1  1  1 1 1 1 1
```

Which is the same as:

```128 + 64 + 32 + 16 + 8 + 4 + 2 + 1 = 255
```

Knowing these two pieces of information means you’re now ready to understand how text is stored within computer memory.

## How computers deal with text

When you’re a kid (a human kid, not a robot kid), and you wanted to send secret messages to your friends, you might have a code something like this:

```A = 1
B = 2
C = 3
D = 4
E = 5
F = 6
G = 7
H = 8
I = 9
J = 10
K = 11
L = 12
M = 13
N = 14
O = 15
P = 16
Q = 17
R = 18
S = 19
T = 20
U = 21
V = 22
W = 23
X = 24
Y = 25
Z = 26
```

Instead of writing the letters of words, you write its corresponding number in your system. Your friend knows the system, so they can turn your numbers back into letters, and read your message.

```Secret message: 2 12 5 5 16   2 12 15 15 16
```

Well, computers are basically just like kids sending secret messages. They have a list of numbers and letters, so they know which letter each number corresponds to, and they store those numbers. The near-ubiquitous table of numbers and characters that pretty much all computers use as standard is known as ASCII (pronounced “ASS-ky”). It stands for “American Standard Code for Information Interchange”, which I’m sure you’ll agree is not a particularly catchy name, which is why everyone just calls it ASCII.

ASCII includes all the letters from A through Z, in both uppercase and lowercase, and also the digits 0 through 9 (yeah, it’s a bit weird that we use numbers to represent… other numbers… but hey, the world is weird), a bunch of other special characters and punctuation, like “(“ and “)”, “!”, “@”, “#” and “?”, the space character (as in, “ ”), plus some other weird stuff you don’t really need to worry about.

In ASCII, the letter “A” unfortunately isn’t 1, it’s actually 65, and Z is at 90. The lowercase letter “a” is at 97, and the lowercase letter “z” is at 122.

The weird stuff I mentioned is mostly down at the start of the list, in the first 30 or so slots.

Each slot is called a “code point”. So you’d say that the lowercase letter “z”, as I mentioned above, is at “code point 122” in the ASCII table.

The whole ASCII table looks something like this:

ASCII Code Point Character
0 ... 31 Weird stuff, don’t worry about it
32 (space)
33 !
34 "
35 #
36 \$
37 %
38 &
39 '
40 (
41 )
42 *
43 +
44 ,
45 -
46 .
47 /
48 0
49 1
50 2
51 3
52 4
53 5
54 6
55 7
56 8
57 9
58 :
59 ;
60 <
61 =
62 >
63 ?
64 @
65 A
66 B
67 C
68 D
69 E
70 F
71 G
72 H
73 I
74 J
75 K
76 L
77 M
78 N
79 O
80 P
81 Q
82 R
83 S
84 T
85 U
86 V
87 W
88 X
89 Y
90 Z
91 [
92 \
93 ]
94 ^
95 _
96 `
97 a
98 b
99 c
100 d
101 e
102 f
103 g
104 h
105 i
106 j
107 k
108 l
109 m
110 n
111 o
112 p
113 q
114 r
115 s
116 t
117 u
118 v
119 w
120 x
121 y
122 z
123 {
124 |
125 }
126 ~
127 More weird stuff, don’t worry about it

So, the string “Hello, world!” in ASCII would basically be this:

 Character ASCII Code Point H e l l o , w o r l d ! 72 101 108 108 111 44 32 119 111 114 108 100 33

I’ve listed these using decimal numbers, but using our new binary skills, we know how to write the ASCII code points as binary numbers:

Character ASCII Code Point
Decimal Binary
H 72 01001000
e 101 01100101
l 108 01101100
l 108 01101100
o 111 01101111
, 44 00101100
32 00100000
w 119 01110111
o 111 01101111
r 114 01110010
l 108 01101100
d 100 01100100
! 33 00100001

So “Hello, world!”, represented as binary, looks like this:

`01001000 01100101 01101100 01101100 01101111 00101100 00100000 01110111 01101111 01110010 01101100 01100100 00100001`

And that is pretty much how computers store text, in binary format. We can confirm this on the command line:

```# Write the text into a file called bleep_bloop:
\$ `echo -n “Hello, world!” > bleep_bloop`

# Display the file using xxd (xxd is a hex viewer,
# but we specify -b for binary output, and -c 4 so
# we get the output arranged in 4 columns):
\$ `xdd -b -c 4 bleep_bloop`
00000000: 01001000 01100101 01101100 01101100  Hell
00000004: 01101111 00101100 00100000 01110111  o, w
00000008: 01101111 01110010 01101100 01100100  orld
0000000c: 00100001                             !
\$
```

At the very left, we have the character position for the first character in each row (in hexadecimal - more on that below), followed by four letters per line represented as binary, followed by those same four letters represented as… well, letters.

## Okay, whoa, what the hell is hexadecimal?

Don’t panic. It’s just a base-16 numbering system (“hexa-”, from the Greek word for 6, and “dec(a)” from the Greek word for 10). “Base-16” is just a way of referring to a number system that has 16 digits. You can use any number after “base-” to refer to any numbering system.

The hexadecimal system has 16 digits, and, just like in a base-2 (binary) or a base-10 (decimal) numbering system, it follows the same simple rule. Each time you go up a column, you multiply by the number of digits in the system - and in hexadecimal there’s 16 digits.

```        1 = 1
1 × 16 = 16
16 × 16 = 256
256 × 16 = 4096
4096 × 16 = 65536 ... and so on
```

“But hold on,” you quite rightly say. “There’s 16 digits? But there aren’t 16 numbers!”

That’s right. So, we need some extra digits from somewhere, so that we can write the numbers 10, 11, 12, 13, 14 and 15 in hexadecimal. We can’t use “10” for the number 10, because we already used “1” and “0” for… well, 1 and 0. That would be like using “10” to represent “9” in decimal. It would be super confusing, and we’d be in big trouble when we actually needed to write the number “10”.

So, we need new digits. But, instead of having to invent weird new digits for hexadecimal, we can just use the letters A through F in place of the numbers 10 through 15, like this:

```    Decimal: 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15
Hexadecimal: 0  1  2  3  4  5  6  7  8  9  A  B  C  D  E  F
```

You’ll often see hexadecimal written with the lowercase letters “a” through “f” instead - in fact it’s more common that way - but I’m going to stick with the uppercase for now, as it’s easier to read them in sentences like that.

You may notice that there’s some overlap between the column values in the hexadecimal system and the columns values in the binary system. They both have a 1 column, a 16 column, and a 256 column. That’s not a coincidence. It’s because 16 is a power of 2 (24, to be precise).

Hexadecimal is often used because you can write numbers in a much smaller space using hexadecimal than with binary (one hexadecimal digit can do the same work as four binary digits - because 16 is 2 to the power of 4), and because hexadecimal matches up nicely with bytes (two hexadecimal digits corresponds to eight binary digits, or 1 byte of information).

```            Binary     Hexadecimal   Decimal
ASCII “j” = 01101010 = 6A          = 106
```

You might well have come across hexadecimal before in the form of web colours, like `#ffffff` and `#ffcc00`. Each pair of digits represents a number between 1 and 255, and each pair respectively defines colour values for red, blue and green (RGB).

So, let’s just apply the same rules we did with decimal and with binary, and add up some numbers in hexadecimal. Don’t worry, it’s gonna be fine.

Here’s the first one:

```4096 256 16 1
0   0  9 0
```

Yep, it’s 144 again.

```(9 × 16) + (0 × 1) = 144
144 + 0 = 144
```

And here’s the next one:

```4096 256 16 1
0   0  A 9
```

Uh-huh, you guessed it - it’s 169 again.

```(10* × 16) + (9 × 1) = 169
160 + 9 = 169
```
* We turned the hex digit “A” into the corresponding decimal number 10 here

So now we add them together:

```  4096 256 16 1
0   0  9 0
+    0   0  A 9
=    0   1  3 9
1
```

We carried over the 1 because A + 9 (which is 10 + 9 in decimal) is 19 in decimal. That’s more than 16, so what we’ve really got here is one 16, and a 3 left over. So the one 16 gets carried over, and we write the 3 in the current column. In the final column, we just have the carried 1, so that’s that.

We can check our answer is right by adding the digits up in decimal:

```(1 × 256) + (3 × 16) + (9 × 1) = 313
256 + 48 + 9 = 313
```

We can finish up by having a look at our “Hello, world!” text file again with `xxd`, but this time represented as hexadecimal:

```# No -b option this time
\$ `xdd -c 4 bleep_bloop`
00000000: 4865 6c6c  Hell
00000004: 6f2c 2077  o, w
00000008: 6f72 6c64  orld
0000000c: 21         !
```

See that last “!” on the last line? It’s hex number is 21. That’s 2 sixteens, and 1 one.

```(2 × 16) + (1 × 1) = 33
32 + 1 = 33
```

If you have a look back at the ASCII character table earlier in the post, you’ll see that the ASCII code point for the character “!” is (you guessed it) 33, just as we would have expected.

And now (I hope!) you really do pretty much understand how binary works.

## Hold on! Right at the beginning you mentioned octal... how does that work?

You are very eagle-eyed and surprisingly assertive, dear reader. Okay, I promise to talk about octal in my next article, where I’ll explain how bit fields work.

Until then though, give yourself a pat on the back. You’ve definitely earned yourself a cup of tea or coffee and a biscuit or something.