st10124 numbering systems

9
ST10124_01_Binary_Mathematics 1 Binary Mathematics Simply based upon two states on or off in our case 1 or 0. These are the only two numbers that can be used. It may seem restrictive, but the methods still conform to standard mathematical rules. The system is capable of containing or writing large numbers. In our more usual decimal mathematics (that is mathematics to base 10) you can count from 0 to 9 by using a single figure:- 0, 1, 2, 3, 4, 5, 6, 7, 8, 9. If another unit is added to 9 then a single digit answer is not possible (in the decimal system). What the mathematician does is to reset the units number back to zero, 0, and carry a 1 forward. The 1 represents a 10. Counting can then continue as before, incrementing the units number again:- 10, 11, 12, etc., to 19. Once again when another single unit is added to the unit number it is reset to zero and another 10 is added to the 1 , making 20. Now for the binary version. A person counting from zero adds a single unit to 0 to make 1. The counter then adds anther 1 to 1, but it cannot take a value of 2, as binary has only two numbers 1 and 0. So as in the previous example, the units is reset to 0 and 1 is added to the next higher column. Thus the sequence is:- 0, 1, 10. The counter then adds another single unit (1) to the right hand zero, getting the answer 11. Now if another 1 is added the first column is reset to 0 and 1 is added to the next column, where a 1 already exists, so that to is reset to 0 and 1 is added to the next higher column, which being 0 is made into 1. Thus the series, comparing decimal to binary is as follows:- In the decimal system the first column numbers can be considered as being n X 10 0 , where n is the unit number. Thus a table can be constructed for the decimal system, where n is a number between 0 and 9. That is, n can have one of 10 values. Column 4 Column 3 Column 2 Column 1 Thousands Hundreds Tens Units n x 10 3 n x 10 2 n x 10 1 n x 10 0 Looking at the table it can be seen that the following decimal numbers have special binary equivalents. 1 00001 2 00010 4 00100 8 01000 16 10000 In the same way that each part of a decimal number can have any one of 10 values, so each figure of a binary number can have any one of 2 values. So the table above looking at the construction of decimal numbers can be amended as overleaf. Decimal Binary 0 0 1 1 2 10 3 11 4 100 5 101 6 110 7 111 8 1000 9 1001 10 1010 11 1011 12 1100 13 1101 14 1110 15 1111 16 10000

Upload: john-hilton

Post on 30-Mar-2016

216 views

Category:

Documents


0 download

DESCRIPTION

Brief Intro

TRANSCRIPT

ST10124_01_Binary_Mathematics 1

Binary Mathematics Simply based upon two states – on or off – in our case 1 or 0. These are the only two numbers that can be used. It may seem restrictive, but the methods still conform to standard mathematical rules. The system is capable of containing or writing large numbers. In our more usual decimal mathematics (that is mathematics to base 10) you can count from 0 to 9 by using a single figure:- 0, 1, 2, 3, 4, 5, 6, 7, 8, 9. If another unit is added to 9 then a single digit answer is not possible (in the decimal system). What the mathematician does is to reset the units number back to zero, 0, and carry a 1 forward. The 1 represents a 10. Counting can then continue as before, incrementing the units number again:- 10, 11, 12, etc., to 19. Once again when another single unit is added to the unit number it is reset to zero and another 10 is added to the 1 , making 20. Now for the binary version. A person counting from zero adds a single unit to 0 to make 1. The counter then adds anther 1 to 1, but it cannot take a value of 2, as binary has only two numbers 1 and 0. So as in the previous example, the units is reset to 0 and 1 is added to the next higher column. Thus the sequence is:- 0, 1, 10. The counter then adds another single unit (1) to the right hand zero, getting the answer 11. Now if another 1 is added the first column is reset to 0 and 1 is added to the next column, where a 1 already exists, so that to is reset to 0 and 1 is added to the next higher column, which being 0 is made into 1. Thus the series, comparing decimal to binary is as follows:-

In the decimal system the first column numbers can be considered as being n X 100, where n is the unit number. Thus a table can be constructed for the decimal system, where n is a number between 0 and 9. That is, n can have one of 10 values.

Column 4 Column 3 Column 2 Column 1

Thousands Hundreds Tens Units

n x 103 n x 102 n x 101 n x 100

Looking at the table it can be seen that the following decimal numbers have special binary equivalents.

1 00001 2 00010 4 00100 8 01000 16 10000

In the same way that each part of a decimal number can have any one of 10 values, so each figure of a binary number can have any one of 2 values. So the table above looking at the construction of decimal numbers can be amended as overleaf.

Decimal Binary

0 0

1 1

2 10

3 11

4 100

5 101

6 110

7 111

8 1000

9 1001

10 1010

11 1011

12 1100

13 1101

14 1110

15 1111

16 10000

ST10124_01_Binary_Mathematics 2

In a binary numbering system, where n can have the values of 0 or 1; that is n can have one of 2 values:-

Column 6 Column 5 Column 4 Column 3 Column 2 Column 1

16’s 8’s 4’s 2’s 1’s

n x 24 n x 23 n x 22 n x 21 n x 20

Fill in Column 6. Further in the same way as the decimal number “604” can be written as 6 100’s and 4 1’s, so a binary number “00101000” can be written as 1 32’s and 1 8’s. (That is the decimal equivalent is 32 + 8 = 40). There are some names associated with binary numbers and microprocessors. A single binary digit is called a ‘bit’. Four bits make a ‘nibble’ and 8 bits, or 2 nibbles, make a ‘byte’. Addition follows the same rules. 7 + 8 makes too many to be shown in a single digit, one 10 is taken away, leaving 5 units and the single ten is added to the next higher column, making 15. In binary something very similar occurs. There are just four possible rules:-

0 + 0 = 0 (Rule a) 0 + 1 = 1 (Rule b) 1 + 0 = 1 (Rule c) 1 + 1 = 10 (Rule d)

So let us consider the problems below:-

For the first example working from lowest to highest (right to left) as usual:- 1 plus 0 (Rule c) = 1, 1 plus 0 (Rule c) = 1, 1 plus 0 (Rule c) = 1, 0 plus 1 (Rule b) = 1. Thus the answer is ‘1111’. For the second Example :- 0 plus 0 (Rule a) = 0, 1 plus 1 (Rule d) = 0 and carry forward 1, 0 plus 0 (Rule a) = 0 then 0 plus 1 (Carried over from the lower bit) (Rule b) = 1, 1 plus 0 (Rule c) = 1. Thus the answer is ‘1100’. For the third example :- 0 plus 0 (Rule a) = 0, 1 plus 1 (Rule d) = 0 carry forward 1 1 plus 1 (Rule d) = 0 carry forward 1, 0 plus1 (Carried forward) (Rule b) = 1 0 plus 0 (Rule a) = 0, 0 plus 1 (Carried forward) (Rule b) = 1 Thus the answer is ‘1100’.

0111 + 1000

1010 + 0010

0110 + 0110

ST10124_01_Binary_Mathematics 3

To sum up:- In decimal numbering the columns of the number go up as follows,

units, tens, hundreds, thousands, ten thousands, hundred thousand, million, etc.. In

Binary the columns increase as follows:- one, two, four, eight, sixteen, thirty two,

sixty four, one hundred and twenty eight, etc.. The table contrasts them.

Decimal 100M 10M 1M 100K 10K 1K 100 10 1

Binary 64 32 16 8 4 2 1

Fill in the last two entries for the binary row.

Next let us consider how to convert a decimal number into a binary number.. For

example, how to convert 14 into binary.

14 is less than 16, so there are no 16’s or larger numbers in 14. So you will begin

with the next lowest option, which on the number ladder is 8. Can 8 be taken away

from 14? Yes it can. So a 1 will appear in the 8 column. The binary digit

representing 8 appears in the fourth column. So the binary number begins:-

1XXX, where X is an unknown value, as yet.

Take 8 away from 14 (as you have represented it) and you are left with 6. The next

binary number is 4. Can 4 be taken away from 6? Yes it can. So a 1 will appear in

the 4 column, which is the next down. So the binary number grows as below:-

11XX, where X is an unknown value, as yet.

Take 4 away from 6 (as you have represented it) and you are left with 2. The next

binary number is 2. Can 2 be taken from 2? Yes it can. So a 1 will appear in the 2

column, which is the next down. So the binary number grows as below:-

111X, where X is an unknown value, as yet.

Taking 2 away from 2 leaves 0 (zero). The next binary column is 1. Can 1 be taken

away from 0? No it cannot. So the last digit of the binary number is 0. The final

binary number is:-

1110

To check convert it back into decimal.

When dealing with microprocessors it is usual to deal with eight bit numbers, as that

is the most common size for data words. It is also the usual size for the ports and

data busses. Thus, in the context of microprocessors the decimal number 14 would

be represented in binary as 00001110.

ST10124_01_Binary_Mathematics 4

Within computing fields a single binary digit is known as a ‘bit’. An 8-bit word is

known as a ‘byte’. A byte can be split into two 4-bit portions. These are called

‘nibbles’. There use will be explained later.

Now to convert 71 into binary. The number is between 64 and 128, so the binary

number will begin:-

71 – 64 = 7 01XXXXXX

7 – 32 (cannot do) 010XXXXX

7 – 16 (cannot do) 0100XXXX

7 – 8 (cannot do) 01000XXX

7 – 4 = 3 010001XX

3 – 2 = 1 0100011X

1 – 1 = 0 01000111

The maximum sized number that can be converted in this way would be the binary

number 11111111. This converts to what, in decimal?

This system works fine provided the number to be converted from decimal into

binary is:-

An integer

Positive in value

Less than 255

If a number is larger than 255 then a second byte (a high-byte) will be required. The

column values will begin with 256, then 512, 1024, etc.. What will be the value of the

most significant bit of this high-byte?

There are ways in which binary mathematics, and thus computers, can cope with

numbers outside this narrow scope. (That is numbers such as -15, 67.08, and so

on.) This is beyond our scope and requirements and will not be covered here.

Now to explore binary mathematics further.

What happens if the binary numbers 11111111 and 00000001 are added together?

We have created a Carry bit that cannot be stored within the byte. If this was

required for computational reasons, then it would have to be stored in a ‘high-byte’.

The carry is not the only thing to note. The result is zero in the main (low) byte.

11111111

00000001

1 00000000

+

ST10124_01_Binary_Mathematics 5

This zero result can be used in decision making in programs, as can carries.

Moving on; if we take the binary number 00000001 and move it one place to the left,

we obtain the following number:- 00000010. Referring back to their decimal values,

the first was valued at 1 and the second at 2. Move it another place to the left to get;

00000100, which is 4. Again to get 00001000 which is 8. Each time the binary digit

is moved to the left it is increased in value by a factor of 2.

A similar situation occurs when the reverse is applied:-

10000000 = 128

01000000 = 64

00100000 = 32

.. and so on

This works for more than single digits. Take the binary representation of 15 and

move it to the left one place and 30 is obtained:-

00001111

00011110

So integer multiplication and simple division by 2 is easily accomplished.

How does larger multiplication work? For example 12 X 11? Or 132 X101?

Expanding the problem:-

Similar rules apply to binary mathematics.

So if we multiply 00001100 X 00001011 it appears as

below and uses the same rules:- zero times something is zero and one times a

number is the number. Even the shifting to the left (shown by adding zeros in the

decimal examples) still holds true.

To verify the procedure, change the binary numbers to

decimal, perform the multiplication and reconvert back to

binary.

Another facet that appears from this is that adding four lines

of binary bits can be complicated. What actually happens is that as each line is

acquired it is added to the result, before proceeding onto the next bit.

00001100 x 00001011 Consider the first bit, which is one. Therefore the running

answer is

00000000 + 00001100 = 00001100 (Result)

12

11

12

120

132

132

101

132

0000

13200

13332

x x

00001100

00001011

00001100

000011000

0000000000

00001100000

00010000100

x

ST10124_01_Binary_Mathematics 6

The second bit is 1, so the top line is shifted left by one place and added to the

result.

00001100 + 00011000 = 00100100

The next bit is a zero, so nothing added (but the top line is shifted once to the left)

00100100 + 00000000 = 00100100 (but the top line has shifted to 00110000)

The next bit is 1, so the top line is shifted to the left and added to the result.

00100100 + 01100000 = 10000100

All the higher bits are zero, so can be ignored, for this example.

One thing that needs to be remembered about binary multiplication is that two 8-bit

numbers multiplied together can result in a 16 bit number, i.e. 2 bytes.

Microprocessors are not exceptionally clever. It is just that they can do simple things

very quickly. Subtractions are an example. The following decimal example will

illustrate the method.

Consider the problem:- 927 – 492.

The number 927 is stored. The number 492 is ‘complimented’, and that doesn’t

mean telling it ‘You’ve got a fine figure.’ The following table may be used.

It shows the values to be substituted. The largest value, 9, is replaced by

the lowest value, 0, and vice versa. Thus in our example above 4 is

replaced by 5, 9 by 0 and 2 by 7. Thus 492 is replaced by 507. Now this

number is added to our original first number, 927. The problem now

becomes 927 + 507 = 1434. Next add an extra ‘1’ (This is to make it a true 10’s

compliment) making our new total 1435. Finally discard the first ‘1’ giving 435.

Indeed 927 – 492 is 435.

This may seem complicated, but in binary the process is easier and crucially suits

the electronic ‘on’ or ‘off’ environment. Now consider the decimal problem 237- 139

in binary. First the binary values are required. 237 in binary is 11101101 and 139 in

binary is 10001011. Complimenting in binary is very easy. Every ‘1’ becomes ‘0’

and vice versa. Thus the second number 10001011 becomes

01110100. We have discovered that addition is simple in binary.

So 11101101 + 01110100 = 101100001. As in the previous

example we add a further 1 (To obtain a true 2’s compliment).

This gives the answer 101100010. This is 9 bits long or

01100010 (1 byte) and a Carry bit. The value of the main byte is

98, which is the correct answer.

0 9

1 8

2 7

3 6

4 5

11101101

01110100

1 01100001

1

1 01100010

ST10124_01_Binary_Mathematics 7

Now you have got used to an 8-bit binary number, let us move on.

It has been found that a byte consists of two nibbles (that is, 4 bits each). Each four

bits can have a decimal value of 0 to 15, in binary 0000 to 1111. It would be useful if

these four bits could be expressed as a single character. The numbers from 0 to 9

are already catered for, with a single number. But we need some extra characters to

cope with the decimal values 10 to 15. In actual fact the letters A to F are used. The

table below shows all the numbers from 0 to 15 together with their Binary and

Hexadecimal equivalents. (Hexadecimal means to base 16)

Decimal 0 1 2 3 4 5 6 7

Binary 0000 0001 0010 0011 0100 0101 0110 0111

Hexadecimal 0 1 2 3 4 5 6 7

Decimal 8 9 10 11 12 13 14 15

Binary 1000 1001 1010 1011 1100 1101 1110 1111

Hexadecimal 8 9 A B C D E F

Hexadecimal is used to express 8-bit binary numbers in a more economical form.

For example decimal 15 is 00001111 in binary or 0F in hexadecimal.

The agreed convention for writing a number, such as 15, in the different formats is:-

D’15’

B’00001111’

H’0F’

The three formats above are interchangeable. You will use whatever suggests itself

as the most convenient and appropriate format.

ST10124_01_Binary_Mathematics 8

Exercises

1 Convert the following 8-bit binary numbers into decimal:-

a) 00000111

b) 00010001

c) 11111110

d) 01000000

2 Add the following binary numbers together using binary methods. Verify your answers by converting to decimal values. a) 00001000 + 00000111

b) 01111111 + 00000001

c) 01010101 + 10101010

d) 00111101 + 00111011

3 Convert the following decimal numbers into binary bytes. a) 10

b) 100

c) 200

d) 96

e) 77

4 Convert the following decimal numbers into binary and then multiply them together, using binary methods. Express your answer in 8-bit bytes. Verify your answer by back conversion into decimal. a) 8 x 2

b) 64 x 2

c) 55 x 3

d) 15 x 15

e) 20 x 20

5 Evaluate the following subtraction examples using the 2’s compliment and addition method; that is – in binary. Cross check your answer by converting into decimal. Comment on the answers obtained. a) 00001111 – 00000001

b) 10000000 – 00000001

c) 10110110 – 01110111

d) 00010000 – 00100000

6 Convert the following numbers from their given format into the desired format.

a) B’00001111’ Hexadecimal

b) D’150’ Hexadecimal

c) H’20’ Decimal

d) B’10101010’ Hexadecimal

ST10124_01_Binary_Mathematics 9

Exercises (2) 1 Convert the following binary numbers to decimal a) 1010

b) 00001111 c) 11111111 d) 00110101

2 Add the following binary numbers together using binary methods. Verify your answers by converting to decimal values. a) 10000000 + 01001010 b) 00011110 + 00000010 c) 00111000 + 01010101 3 Convert the following decimal numbers into binary bytes. a) 16 b) 236 c) 255 d) 15 e) 240 f) 29501 4 Convert the following decimal numbers into binary and then multiply them together, using binary methods. Express your answer in 8-bit bytes. a) 5 x 2 b) 32 x 4 c) 25 x 10 d) 144 x 12 e) 255 x 255 5 Evaluate the following subtraction examples using the 2’s compliment and addition method; that is – in binary. Comment on the answers obtained. a) 10000000 – 00001111 b) 00110011 – 00110011 c) 11111111 – 11110000 d) 00111111 – 01000000 e) 00010000 – 00000001

6 Convert the following numbers from their given format into the desired format.

a) B’10001000’ Hexadecimal

b) D’255’ Hexadecimal

c) H’5A’ Decimal

d) B’00111111’ Hexadecimal