Binary, Denary, Hexadecimal

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 11

Binary and denary

Our usual everyday number system is sometimes called ‘denary’. When we


count in denary we begin at 0, count all the way up to 9, then go back to 0
again, but have an additional digit in the next column. You could ask yourself,
why is it that we have ten separate symbols to represent all our numbers?
The answer is most probably because we have ten fingers and thumbs,
although it’s not really known.
However, computers use binary, in which we only have two digits available to
us, 1 and 0. So, after we count up to 1 we go back to 0 again, but have an
additional digit in the next column. Binary is called base 2 because there are
two digits available with which to count: 0 and 1. The term base 2 also refers
to the fact that each binary place is two times bigger than the previous one.
Our everday counting, denary, is described as base 10, as we haveten digits
available: 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9.
So how can we convert from one to the other? When you were learning about
numbers as a child, you probably used the terms thousand, hundreds, tens,
and units (or ones). These are all powers of 10. You can draw a table using all
these values as headings.

10⁰ = 1
10¹ = 10
10² = 100
10³ = 1000
10⁴ = 10000

Using these headings you can write in a number, say 09032. Make sure that
you use all of the columns that you have digits for, starting at the right-most
column, which represents units. In this case 09032 has five digits, so you start
filling the table from the right, beginning with the 2 in the units column:

Ten thousands Thousands Hundreds Tens Units


10000 1000 100 10 1
0 9 0 3 2
We can see that it is made up of zero 10000s, nine 1000s, zero 100s, three
10s, and two 1s.

In binary, we can do the same thing, but using powers of 2:

2⁰ = 1
2¹ = 2
2² = 4
2³ = 8
2⁴ = 16

Again, a table can be drawn up using these numbers as headings.

2⁴ = 16 2³ = 8 2² = 4 2¹ = 2 2⁰ = 1
1 0 1 1 1
As we did with denary, we can write in a binary number, such as 10111.
Again, we have to make sure that we use the units (2⁰) column, with no gaps.
Now we can see that the number represented in binary as 10111 is made up
of one 16, zero 8s, one 4, one 2, and one 1. So, in denary, we have 16 + 4 +
2 + 1 = 23.

Try converting the following binary numbers into denary. Share your answers
and any problems you have in the comments section below:

1010
1001
10001
Converting numbers back the other way isn’t much more difficult.

If we wanted to convert the denary number 21 into binary, we could start out
with an empty table like this:
16 8 4 2 1

We need to look in each of the columns to see what needs to be placed there
using only ones and zeros. It’s easiest to do this by starting from the left: in
this case we would consider the number 21 and look at the table to see which
of the columns has the biggest number that is less than 21. This is 16, so our
first 1 would be in the 16 column.

Step 1, running total = 16

16 8 4 2 1
1
Now, we still have 5 left (21 - 16 = 5) so we look to the right of the 16 column.
The next column along has an 8 in, which is too big to add on to our 16 and
would take us to 24. This means that we put a 0 in the 8 column and look at
the next column along. The next column is the 4 column which is useful (as it
is less than 5) and gets us closer to our target or 21, so we put a 1 in the 4
column.
Step 2, running total = 20

16 8 4 2 1
1 0 1
At this stage we have 16 + 4 = 20. We still need 1 to get to 21, so we put a 0
in the 2 column and a 1 in the 1 column. Now we have arrived at 21 and have
(1×16) + (0×8) + (1×4) + (0×2) + (1×1) = 21. This is shown in the table below:

Step 3, final total = 21

16 8 4 2 1
1 0 1 0 1
Reading off from the table, we can see that 21 in binary is 10101.
Binary shifts

Binary left shift

In a binary left shift we just shift the bits one column to the left. So, in the
image below you can see that the 0 in the 128 column disappears off the
table, the bit in the 64 column moves to the 128 column, the bit in the 32
column moves to the 64 column, and so on; we add a 0 in the now empty 1
column. This means that this example, which began as 00010110 (i.e. 16 + 4
+ 1 + 1 = 22), after being shifted one place to the left becomes 00101100 (i.e.
32 + 8 + 4 =44), which you will notice is 22 multiplied by 2.

So, if you left shift any binary number by 1 place it becomes 2 times bigger,
because binary is in base 2. This is exactly the same process as left shifting
denary numbers, except in denary numbers the left shift means multiply by
10. For example, if you left shifted 10 by one place it would become 10 times
bigger and would be 100.

Try a few examples yourself: 0010101, 11, 00110. You can check them by
converting to denary.

You don’t have to just shift by one bit, you could shift by 2 or more too,
effectively multiplying the numbers by powers of 2 each time. So a shift of
three places to the left would be the same as multiplying the number by 8 (i.e.
2×2×2).
Binary right shift

In a binary right shift we just shift the bits one column to the right to achieve
floor division. So, in the image below you can see that the bit in the 128
column moves to the 64 column, the bit in the 64 column moves to the 32
column and so on, until the bit in the 1 column disappears off the edge of the
table. In the example you can see that the binary number 00010100 (i.e. 16 +
4 = 20), after having shifted one place to the right, becomes 00001010 (i.e. 8
+ 2 =10), which you will notice is 20 divided by 2.

So, if you right shift any binary number by 1 place it becomes 2 times smaller,
because binary is in base 2. A binary shift of one place to the right is the same
as dividing by 2.

What if there is a 1 in the 1 column?

In the previous example the last digit in the 1 column was a 0, so we were not
concerned about what happened to that digit as it made no difference to the
size of the number. However, if we had a binary number which ended in a 1,
like 11010101 (which in denary is 128 + 64 + 16 + 4 + 1 = 213) in the example
below, what happens to the last 1 if it is right shifted? We mentioned earlier
that a binary right shift is the same as floor division, so only integer (or whole)
numbers are represented and remainders are ignored. That last 1 is a
remainder, so is ignored and simply does not appear in the answer.
You will see above that 11010101 right shifted becomes 01101010, so when
we convert this back to denary we see that we are effectively integer dividing
it (i.e. just looking at the whole number result), and we get that 213 integer
divided (or floor divided) by 2 is 106.

Binary addition of one-bit numbers

For the first two digits in the binary numbering system, addition is the same as
addition in denary, i.e. 0 + 0 = 0 and 1 + 0 = 1. But, as soon as we reach 1 + 1
we hit an issue. In denary this would be 2. But we don’t have the numeral 2 in
the binary base 2 numbering system.

If we need to add 1 + 1 we have to go one column to the left in the place


table. We effectively go back to 0 again, but have an additional digit to add
into the next column.

So, 1 + 1 = 10.

Viewing this as column addition, 1 + 1 gives us a 0, and carry a 1. Since there


is nothing else to add to the carried 1, it goes into the left-hand column, and
the answer is shown as 10. This is correct, as 10 in binary converts to denary
as 2 (i.e. 1 + 1).
Binary addition of larger numbers

Let’s try column addition with an eight-bit binary number. The process is the
same, carrying a 1 each time we add together two or three 1s, as shown in
the example below:

With binary, it is always sensible to convert back to denary to check for any
addition errors. So, with the eight-bit addition above, we can check the
addition as below:

128 64 32 16 8 4 2 1 Total

0 1 1 0 0 1 1 0 102

0 0 1 0 1 1 0 1 45

1 0 0 1 0 0 1 1 147

The first number converts to 102, the second to 45, and the answer to 147.
Indeed, the sum of 102 and 45 equals 147, so the binary addition is correct.
Hexadecimal Numbers
Although numbers are processed inside a computer as binary, that doesn’t
mean that we need to always represent these numbers as binary to humans.

Why we use hexadecimal


Which of the following numbers is easier to remember?

1010 0101
A5
If you think A5 is easier and more convenient to remember and use, you now
understand why some binary is expressed in hexadecimal format. A5 is
actually the same value as 1010 0101; however, it is represented as
hexadecimal instead of binary.

We often see hexadecimal used to represent binary numbers for:

 Colours. These are often represented as intensities of red, green, and


blue light, each of which can be represented by two hexadecimal digits.
See our Representing Data course for more.
 MAC addresses used in networking. See our Introduction to Computer
Networking course for more.
 Memory addresses: the specific place in memory in which a piece of
data is stored. These are often important in error messages.
Hexadecimal is a base 16 number system: it makes use of 16 numbers. So if
we had a creature with 16 fingers, this is how they would count using
hexadecimal:

Picture of creature with 16 fingers, each finger counting up from:


1,2,3,4,5,6,7,8,9,A,B,C,D,E,F, 10

This table shows equivalent denary, binary, and hexadecimal values:


Denary Binary Hex
0 0000 0
1 0001 1
2 0010 2
3 0011 3
4 0100 4
5 0101 5
6 0110 6
7 0111 7
8 1000 8
9 1001 9
10 1010 A
11 1011 B
12 1100 C
13 1101 D
14 1110 E
15 1111 F

Hexadecimal is commonly used in computing because it can represent a byte


of data with just two characters (instead of eight). Each hexadecimal
character represents half a byte, also called a nibble.

So, hexadecimal 7D is equivalent to binary 0111 1101.


Simiarly, the binary number 0001 1010 is converted to hexadecimal like this:

0001 is the binary representation of 1 (which is the same in denary and


hexadecimal).
1010 is the binary representation of the denary number 10, which is
represented in hexadecimal as A.
So, binary 0001 1010 is equivalent to hexadecimal 1A.
Converting hexadecimal to denary, via binary
You already know how to convert binary numbers to denary, and since you
just learned how to convert a hexadecimal number to binary, you can also
convert it to denary.

For example, the number A9 in hexadecimal is 1010 1001 in binary. And we


can convert this to denary using the table we’ve used before:

128 64 32 16 8 4 2 1
1 0 1 0 1 0 0 1
128 + 32 + 8 + 1 = 169

Converting hexadecimal directly to denary


We can also convert hexadecimal directly into denary by thinking about the
place values. You know that the place values of denary numbers work like
this:

1089 = (1 × 1000) + (0 × 100) + (8 × 10) + (9 × 1)

Base 10 place values 1000 (10³) 100 (10²) 10 (10¹) 1 (10⁰)


Denary value 1 0 8 9
Hexadecimal works very similarly, with base 16 instead of base 10. Let’s
convert hexadecimal A9 to denary:

Base 16 place values 16 (16¹) 1 (16⁰)


Hexadecimal value A 9
From the table above, we know that hexadecimal A represents denary 10 and
hexadecimal 9 represents denary 9.
Just like in 1089, there is a 9 in the 1 position: (9 × 1).
There is a 10 in the 16 position: (10 × 16).
So that means hexadecimal A9 = (10 × 16) + (9 × 1) = 169 in denary.
Base 16 place values 16 (16¹) 1 (16⁰)
Hexadecimal value A 9
Conversion (10 × 16) = 160 (9 × 1) = 9
Converting denary to hexadecimal
Let’s convert denary 200 to hexadecimal. We do this by finding out how many
times 16 fits into 200 in denary in order to represent 200 in base 16:

200 / 16 = 12, remainder 8.


So, we have 12 lots of 16, with 8 units left over.
Denary 12 is hexadecimal C, and denary 8 is hexadecimal 8 (check out the
table above if you forget).
So, denary 200 = (12 × 16) + (8 × 1) = C8 in hexadecimal.

You might also like