# converting betweendifferent number systems ## index > 2.1.4 Representation of data in computer systems

### Numbers

Denary is the base-10 number system which we use everyday. It has 10 different digits: 0,1,2,3,4,5,6,7,8 and 9. This is used by people because it is easy to understand and learn.

Computers use binary to store all data and instructions. Each of the binary numbers is called a bit (Binary digit) Computers use binary and logic gates to make calculations. Binary is in 1s and 0s. 1 represents ON and 0 represents OFF. You can use these basic inputs/outputs and boolean logic to create a system for operations. Binary is usually written in 8 bits which means it has 8 digits. E.g. 01001001

### Converting Binary and Denary

We can convert from binary to denary or from denary to binary using the table below:

128 64 32 16 8 4 2 1
27 26 25 24 23 22 21 20

In order to find out what the denary equivalent of a binary string is, we need to put the binary string and the denary equivalents into the table and add together the column values where there is a ’1’. Here is an example of the conversion of 00110100 into denary:

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

32 + 16 + 4 = 50 Therefore 00110100 = 50

The method to convert from denary to binary is less straightforward, but can be understood with some practice.
The method you will learn here is to half the denary number, and if there is a reminder, record a 1, otherwise leave it as a 0. You keep halving until you reach 0. Here is an example of converting 231 into binary:

Result Remainder Yes/No
231 ÷ 2 = 115.5 1
115 ÷ 2 = 57.5 1
57 ÷ 2 = 28.5 1
28 ÷ 2 = 14 0
14 ÷ 2 = 7 0
7 ÷ 2 = 3.5 1
3 ÷ 2 = 1.5 1
1 ÷ 2 = 0.5 1

This tells us that 231 = 11100111

Sometimes it is more efficient to add 2 binary numbers without having to convert them into denary and convert back to binary. In order to do so, there are some mathematical laws that need to be followed:

1. 0 + 0 = 0
2. 1 + 0 = 1
3. 1 + 1 = 10 (so write 0 and carry the 1)
4. 1 + 1 + 0 = 10 (so write 0 and carry the 1)
5. 1 + 1 + 1 = 11 (so write 1 and carry the other 1)

Here is an example of the addition of 01110100 (116) + 10001010 (138):

116 138 1 1 1 1 1 1 0 1 1 1 0 1 0 0 + 1 0 0 0 1 0 1 0

Here is another example of binary addition:

244 138 (1) 1 1 1 1 1 1 1 1 1 1 0 1 0 0 + 1 0 0 0 1 0 1 0

In this case the result is not in the form of an 8-bit number. If it goes beyond the 8-bit range it is known as an overflow error. An overflow error is caused when the processor does not have enough bytes available to process an instruction. There is an extra bit which cannot fit into the 8-bits as the result is greater than 255 of 11111111

Hexadecimal is a base-16 number system meaning meaning it is made up of 16 different symbols: 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E and F. Hexadecimal is often used by programmers as it is easier to read and quicker to use.

Denary (Base-10) Binary (Base-2) Hexadecimal (Base-16)
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

You will also need to know how to convert hexadecimal to other number systems. In this example we will convert 24 in hexadecimal to denary. We need to use a column method like this:

16 1
2 4
16 x 2 = 32 4 x 1 = 4

32 + 4 = 36
This means 24 in hexadecimal is 36 in denary.

Here is another example using a letter from the hexadecimal table:

16 1
3 D
16 x 3 = 48 13 x 1 = 13

48 + 13 = 61
This means 3D in hexadecimal is 61 in denary.

There are many ways to convert denary to hexadecimal but you should learn this method. In this example we will convert from 45 in denary to hexadecimal. First we convert 45 into binary using the method from before which you can learn about here: (If needed go back and take a look at converting using binary to remind yourself)

45 is 00101101. We then convert that into 2 nibbles and calculate the value of each nibble.

8 4 2 1 --- 8 4 2 1
0 0 1 0 --- 1 1 0 1
0+0+2+0 = 2 --- 8+4+0+1 = 13 = D

We then just put the two results next to each other which gives us: 2D. This means 45 in denary is 2D in hexadecimal.

Here is another example with 237. Using the other method we know 237 is 11101101. We now convert it into 2 nibbles and calculate the value of each nibble.

8 4 2 1 --- 8 4 2 1
1 1 1 0 --- 1 1 0 1
8+4+2+0 = 14 = E --- 8+4+0+1 = 13 = D

We then just put the two results next to each other which gives us: ED. This means 237 in denary is ED in hexadecimal.