Computers and electronics are ubiquitous in modern culture (you wouldn’t be reading this post if they weren’t) and as a newbie iOS App developer, you are going to eventually need some knowledge of the background Computer Science concepts that underpin not only iOS App Development but software development and computers in general.
Today is the first of a number of posts that aim to give you that knowledge and in this post we take a look at one of the most basic concepts in computer science, that of binary numbers.
Table of Contents
Introduction
As you I’m sure you know, computers run on electricity.
Whether it be mains power, battery power or solar power, all of these devices are based on the idea of using electricity to individually configure billions of tiny electronic components into one of two states:
- On (which we interpret as a 1)
- Off (which we interpret as a 0)
In essence we use each of these individual components is used to store a single ‘bit‘ of information.
Now, being able to store two values, 0 and 1, is good but it won’t get us very far. What about if we wanted to store larger values?
Decimal Notation
Do you remember when you first learnt to count and write numbers at school? You may not, but in order to explain how these tiny memory cells are used to store larger numbers we need to take a quick trip down memory lane (apologies for the rubbish jokes!). Lets look at an example.
Can you remember the different number columns you were taught about in school? They may have faded into the dim and distant past but you were probably taught that numbers such as the number 1234 could be written by expressing them in terms of the number of times we needed the 1’s, 10’s, 100’s and 1000’s columns etc.
So If we wrote 1234 out in a longer form we could write:
1234 = (1 x 1000) + (2 x 100) + (3 x 10) + (4 x 1)
See how the different number columns are parts of the expression?
Now, what you might not know is that we can split that same expression down even further:
1234 = (1 * 103) + (2 * 102) + (3 * 101) + (4 * 100)
Note: The superscript numbers is called the exponent. If you have never encountered exponents before check out https://en.wikipedia.org/wiki/Exponentiation for more details.
Can you see how the bold characters are still our original number but can you also see how I have broken down the number columns to be expressed as powers of 10? It’s still exactly the same expression just in a longer form.
When we write numbers such as 1234 above, we are writing them in something called decimal notation (also called decimal for short).
Decimal, a term deriving from the latin decem meaning 10, uses 10 characters (the symbols 0 through to 9) to express numbers and it’s the number notation most of us are used to.
As you can probably see from the example above, the number 10 is extremely important in decimal notation and is called the radix or number base (http://en.wikipedia.org/wiki/Number_base). All numbers in decimal notation are expressed in terms of this radix.
Now, you may be wondering where I am going with this and here it is….
What if instead of using 10 characters to express numbers, we used just two, the characters 0 and 1 we discussed right at the start of this post? Could we still express all the numbers we wanted to using just those two characters?
Well as I’m sure you’ve guessed by now, the answer is yes. Say ‘Hello’ to Binary Notation.
Binary Notation
Binary notation (often shortened to binary), derives from the word bi meaning two.
In binary instead of using 10 characters or symbols to express numbers just two are used, the symbols 0 and 1.
As with decimal notation, all numbers in binary notation are expressed in terms of a radix or number base. In the case of binary this radix is 2.
When we write binary numbers we sometimes write them with the number base as a subscript. This is done to distinguish them from numbers in other bases (most commonly to distinguish them fromnumbers written in decimal). Here’s an example of a binary number that shows this: 1012.
Now, say we wanted to represent the decimal number 27 in binary. How would we do it?
Well, we know that in decimal we have different number columns (the 1’s, 10’s, 100’s 1000’s, etc) and each of those columns represents the value of raising the radix by a different power. Let’s work out what those columns would look like for binary:
Power |
27 |
26 |
25 |
24 |
23 |
22 |
21 |
20 |
Decimal |
128 |
64 |
32 |
16 |
8 |
4 |
2 |
1 |
Now, in similar fashion to decimal we can express numbers in binary by adding different combinations of the number columns together. Where binary differs from decimal however, is that instead of being able to use a particular column up to 9 times, you can only use each column once.
Given this, hear is a teaser for you. How could we combined the different number columns above so that they added up to 27?
Well we could write:
2710 = (1 * 16) + (1 * 8) + (0 * 4) + (1 * 2) +(1 * 1)
Or alternatively:
2710 = (1 * 24) + (1 * 23) + (0 * 22) + (1 * 21) + (1 * 20)
So as with decimal if we remove the number column part of each multiplication above we have:
2710 = 110112
And there you have it, 27 (which can also be written as 2710) expressed in binary!
What about another quick example? See if you can tackle this next one on on your own.
Can you work out how to represent the decimal number 6310 in binary?
Converting from Decimal to Binary
There is also a second, and some would say quicker way of converting from decimal to binary called the Division Method. Let’s take our example 2710 again.
The first step in the Division Method is to take our number and perform integer division using the number 2, keeping an eye on the remainder.
Note: You may or may not know but integer division is were you look at the maximum number of times one number will fit into another along with any remainder.
In our case, if we performed integer division we would have:
27 / 2 = 13 r 1.
In the Division Method we are interested in the remainders and will eventually use each remainder as a digit in our binary number. In this case, the digit will be 1.
Next we take our new number 13 and again divide that by 2, keeping track of the new result and the remainder. That gives us:
13 / 2 = 6 r 1.
We continue to repeat this process, taking the new number, dividing it by two and making a note of the remainder until we have either a reminder of 0 or 1. Once we reach this point we also make a note of that last digit.
If we now read off all the remainders we have noted in reverse order we have our binary number i.e. 110112. I’ve written out the full table of workings below:
Calculation |
Result |
Remainder |
27 / 2 |
13 |
1 |
13 / 2 |
6 |
1 |
6 / 2 |
3 |
0 |
3 / 2 |
1 |
1 |
1 / 2 |
0 |
1 |
Let’s try another example.
I’ll skip the description this time but see if you can work through the table below on your own.
In this example we’re going to attempt to convert the number decimal number 126 (12610) into binary.
Calculation |
Result |
Remainder |
126 / 2 |
63 |
0 |
63 / 2 |
31 |
1 |
31 / 2 |
15 |
1 |
15 / 2 |
7 |
1 |
7 / 2 |
3 |
1 |
3 / 2 |
1 |
1 |
1 / 2 |
0 |
1 |
If we read off the remainders in reverse order we get 11111102.
So we now know how to convert from decimal to binary but what about the other direction?
In the next section we’re going to look at the counterpart to the Division Method that allows us to quickly convert from binary to decimal.
Converting from Binary to Decimal
To convert from binary to decimal there is also a simple method but this time, instead of dividing by 2 we’re going to multiply by 2 hence it’s title: the Multiply Method.
To make sure we’ve got the hang of things, we’ll use our result from the example in the previous section and see if we can get back to our starting number.
Let’s start off with our binary number 11111102. Also start with a running total of 0.
The first step in the Multiply Method is to multiply our running total by 2 and then add the first digit from the binary number to get a new running total.
We then repeat this process for all the remaining digits in the binary number, multiplying the running total by two and then adding the next digit.
Once we’ve added the digit from the last column we’re done and should have our decimal equivalent. Let’s see how it goes:
Multiplication |
Binary Digit to Add |
Total |
0 * 2 = 0 |
1 |
1 |
1 * 2 = 2 |
1 |
3 |
3 * 2 = 6 |
1 |
7 |
7 * 2 = 14 |
1 |
15 |
15 * 2 = 30 |
1 |
31 |
31 * 2 = 62 |
1 |
63 |
63 * 2 = 126 |
0 |
126 |
And there we have it. Back to our original result of 12610
Let’s try one more example, what about the binary number 1011012? Again, see if you can work through this one on your own before you look at my workings.
Multiplication |
Binary Digit to Add |
Total |
0 * 2 = 0 |
1 |
1 |
1 * 2 = 2 |
0 |
2 |
2 * 2 = 4 |
1 |
5 |
5 * 2 = 10 |
1 |
11 |
11 * 2 = 22 |
0 |
22 |
22 * 2 = 44 |
1 |
45 |
In this case our result is 4510
As you can see, the process of getting from binary to it’s decimal equivalent is relatively easy once you have learnt this technique and with a bit of mental arithmetic you should be all set!
Summary
We’ll leave things there for today but rounding up todays post what have we learnt?
We’ve taken a look at both the binary and decimal numbering systems. We’ve seen how numbers written in a particular notation are all expressed in terms of that notations radix or number base and we’ve seen how for decimal that radix is 10 and for binary that radix is 2.
We’ve also learnt about two conversion techniques, the multiplication method, for converting binary numbers into their decimal equivalents and the division method that is used to convert decimal numbers into binary. And during the course of the post we’ve worked through a number of examples.
With that, I’ll leave you today with two things.
Firstly, if you get stuck with anything I’ve explained in the post, get in touch and I’ll do my best to help you work though it.
Secondly I’ll leave you with one of the best (or worst) binary jokes I know. After what you’ve learnt today it should at least make you smile (or maybe that’s cringe? ?)…
There are 10 types of people in the world, those who understand binary and those who don’t.
Image source: http://flic.kr/p/4zw5Zx