Binary
Learn how computers use binary to store numbers.
Introduction
All data within a computer is represented using a system called binary.
Binary is a number system that only uses 1's and 0's
In everyday life we use a number system called decimal, denary, or base 10. At National 5 level this will be referred to as denary.
A number base indicates how many digits are availible to use within a number system. Denary is known as base 10 because there a 10 digits availible to use (0-9).
The number system that computers use is called binary, or base 2. This is because there are only 2 digits available to use (0, 1).
Place Values
In all number systems we use place values to denote the value of each digit.
For example, within the denary system we have the following place values:
1000 | 100 | 10 | 1 |
For example, within the denary system we have the following place values:
1000 | 100 | 10 | 1 |
6 | 4 | 5 | 2 |
Meaning that we have:
- 6 thousands
- 4 hundreds
- 5 tens
- 2 ones
Notice that in the denary (base 10) system our place values start at 1 each time we move to the left we multpily by 10.
The binary (base 2) system works in a similar way. Our place values still start at 1 but this time ase we move left we multiply by 2.
This gives us the following place values:
128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
We could continue multiplying by 2 to get more place values, but the binary numbers you will encounter at National 5 will be limited to 8 digits.
Binary to Denary
We can convert any binary number into its decimal equivalent.
For example,
Convert the binary number 0010 1101 to denary.
We start by writing our place values. Remember, this start at the right-hand side with one and moves left doubling each time.
128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
We then add the binary number we are trying to convert into the table. You should start at the left, adding one binary number under each place value.
128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
0 | 0 | 1 | 0 | 1 | 1 | 0 | 1 |
We can then add up the values from the table.
(0 x 128) + (0 x 64) + (1 x 32) + (0 x 16) + (1 x 8) + (1 x 4) + (0 x 2) + (1 x 1) = 45
In other words, you are adding up the numbers that appear above the 1's.
Looking along the bottom row we find 4 1's and can add the numbers that appear above them.
32 + 8 + 4 + 1 = 45
Therefore:
0010 1101 -> 45
Denary to Binary
If we wish to take a denary number and convert it to binary we can follow one of two methods.
Method 1 is quick and is best used for smaller numbers.
If we encouter a larger number it is best to use method 2. This takes more time but will allow you to convert large numbers with ease.
Denary to Binary - Method 1
As mentioned above, this method is best used for smaller numbers.
For example, convert the number 19 to binary
We first start by writing our place values.
128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
We then look at our place values and try to find numbers which add to give use our target number, in this case 19. You can only use each number once!
We can see that 16, 2 and 1 add to give us 19. We put 1's under these numbers.
128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
1 | 1 | 1 |
We can then put 0's under all other numbers.
128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
0 | 0 | 0 | 1 | 0 | 0 | 1 | 1 |
Therefore:
19 -> 0001 0011
Denary to Binary - Method 2
Method 1 is quite difficult to use with larger numbers.
For example, if we wanted to convert the denary number 195 to binary, it would be quite difficult to intuatively find numbers that add up to give 195.
In this case, method 2 should be used. This method will work with any denary to binary conversion.
For example,
Convert the denary number 195 to binary.
We first begin by writing the binary place values.
128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
For this method, we will start at the leftmost place value (128) and fill each place value in as we move right.
We start with 128 and ask ourselves "is 128 less that or equal to 195?"
If the answer to this question is yes, then we add a 1 below this place value.
If the answer to this question is no, then we add a 0 below this place value.
In this case, 128 is less than 195, therefore we add a 0 below 128.
128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
1 |
If we add a 1 we have to follow an additional step. We must take the place value off of the number we are trying to find.
In other words:
195 - 128 = 67
67 is now the new number that we will concentrate on.
We then move to our next place value and ask ourselves the question again. This time using our new number.
"Is 64 less than or equal to 67?"
Again, the answer is yes so we add a 1.
128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
1 | 1 |
As we have added a 1, we have to update our number.
67 - 64 = 3
3 is our new number.
We continue this process
"Is 32 less than or equal to 3?"
The answer this time is no. This means that we put a 0.
128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
1 | 1 | 0 |
When we add a 0 we dont need to perform any calculation. We simply continue...
"Is 16 less than or equal to 3?"
Again, the answer is no so we add a 0 and continue
128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
1 | 1 | 0 | 0 |
"Is 8 less than or equal to 3?"
No, so add a 0 and continue
128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
1 | 1 | 0 | 0 | 0 |
"Is 4 less than or equal to 3?"
No, so add a 0 and continue
128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
1 | 1 | 0 | 0 | 0 | 0 |
"Is 2 less than or equal to 3?""
Yes, so we add a 1.
128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
1 | 1 | 0 | 0 | 0 | 0 | 1 |
We have another 1, so we need to update our current number.
3 - 2 = 1
1 is our new number
Is 1 less than or equal to 1?
Yes! It is equal, therefore we add a 1.
128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
1 | 1 | 0 | 0 | 0 | 0 | 1 | 1 |
Our table is complete!
Therefore:
195 -> 1100 0011
Denary to Binary - Method 2 (Another Example)
Convert the denary number 192 to binary.
We always start by writing our place values.
128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
We ask ourselves the question "is 128 less than or equal to 192?"
Yes so we add a 1.
128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
1 |
We then perform our calculation.
192 - 128 = 64
64 is our new number.
Is 64 less than or equal to 64?
Yes, it is equal so we add a 1.
128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
1 | 1 |
We perform the calculation again.
64 - 64 = 0
This time we have hit 0 before the end. In this case we fill all other columns with 0's.
128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
1 | 1 | 0 | 0 | 0 | 0 | 0 | 0 |
Therefore:
192 -> 1100 0000