Number Systems - De Montfort Universityjordan/teaching/elec1099/NumberSystems.pdf · Number Systems...

49
Number Systems J. Dimitrov [email protected] Software Technology Research Laboratory (STRL) De Montfort University Leicester, UK. J.Dimitorv, STRL, DMU, [email protected] – p. 1

Transcript of Number Systems - De Montfort Universityjordan/teaching/elec1099/NumberSystems.pdf · Number Systems...

Number SystemsJ. Dimitrov

[email protected]

Software Technology Research Laboratory (STRL)

De Montfort University

Leicester, UK.

J.Dimitorv, STRL, DMU, [email protected] – p. 1

Overview

Octal and Hex

Conversion from base 10

Binary, Octal and Hexadecimal arithmetic

Negative numbers

2’s complement

Real numbers

J.Dimitorv, STRL, DMU, [email protected] – p. 2

Binary Numbers

Let us recall from last week!

Computers use Binary (base 2).

Base b = 2.

Digits dj = 0, 1.

N = dn−1×bn−1+dn−2×bn−2+...+d1×b1+d0×b0.

Example:N = 10011101(2) = 1706051413120110 = 1×27+0×

26+0×25+1×24+1×23+1×22+0×21+1×20 = 157

However, these are the positive numbers only!

J.Dimitorv, STRL, DMU, [email protected] – p. 3

Octal and Hexadecimal

With octal we have that the base is 8 and the digitsare from 0 to 8 − 1 = 7. Example:

N = 75361203(8) = 7756356413220130 = 7×87+5×86+

3×85+6×84+1×83+2×82+0×81+3×80 = 16114307.

Hexadecimal is base 16 and the digits are from 0 to15. However, with digits larger then 9, e.g. 13, we arerisking confusion between the digit 13 and the twodigit number 1316 = 1910. So, we use the notation,10 = A, 11 = B, 12 = C, 13 = D, 14 = E, 15 = F .

N = A3B6(16) = A332B160 =

10 × 163 + 3 × 162 + 11 × 161 + 6 × 160 = 41910.

J.Dimitorv, STRL, DMU, [email protected] – p. 4

From base 10 to 2, 8 and 16

We saw how we convert from Binary, Octal and Hexinto Decimal. Let us look into the dual problem.

We go back to

N = dn−1× bn−1 +dn−2× bn−2 + ...+d1× b1 +d0× b0

and convert this into

N = b(dn−1 × bn−2 + dn−2 × bn−3 + ... + d1 × b0)+d0

and further

N = b × N1 + b0.

J.Dimitorv, STRL, DMU, [email protected] – p. 5

From base 10 to 2, 8 and 16

N = b(dn−1 × bn−2 + dn−2 × bn−3 + ... + d1 × b0)+d0

N = b × N1 + b0.

This tells us that the rightmost digit b0 in N ’srepresentation in base b is the remainder of thedevision of N with b. Again we can do the samewith N1 and find the second digit b1, etc.

J.Dimitorv, STRL, DMU, [email protected] – p. 6

From base 10 to 2, 8 and 16

Example: Let us convert 25(10) into binary.

25

J.Dimitorv, STRL, DMU, [email protected] – p. 7

From base 10 to 2, 8 and 16

Example: Let us convert 25(10) into binary.

2 2512 r1 = d0

J.Dimitorv, STRL, DMU, [email protected] – p. 7

From base 10 to 2, 8 and 16

Example: Let us convert 25(10) into binary.

2 252 12 r1 = d0

6 r0 = d1

J.Dimitorv, STRL, DMU, [email protected] – p. 7

From base 10 to 2, 8 and 16

Example: Let us convert 25(10) into binary.

2 252 12 r1 = d0

2 6 r0 = d1

3 r0 = d2

J.Dimitorv, STRL, DMU, [email protected] – p. 7

From base 10 to 2, 8 and 16

Example: Let us convert 25(10) into binary.

2 252 12 r1 = d0

2 6 r0 = d1

2 3 r0 = d2

1 r1 = d3

J.Dimitorv, STRL, DMU, [email protected] – p. 7

From base 10 to 2, 8 and 16

Example: Let us convert 25(10) into binary.

2 252 12 r1 = d0

2 6 r0 = d1

2 3 r0 = d2

2 1 r1 = d3

0 r1 = d4

J.Dimitorv, STRL, DMU, [email protected] – p. 7

From base 10 to 2, 8 and 16

Example: Let us convert 25(10) into binary.

2 252 12 r1 = d0

2 6 r0 = d1

2 3 r0 = d2

2 1 r1 = d3

0 r1 = d4

Therefore our number 25(10) = 1d41d30d20d11d0

(2).

J.Dimitorv, STRL, DMU, [email protected] – p. 7

Binary arithmetic

Let us start with decimal arithmetic.1 1

7 5+ 3 8

1 1 3In binary we have exactly the same process but weremember that 0 + 0 = 0, 0 + 1 = 1 + 0 = 1 and1 + 1 = 10, i.e. 0 and 1 carry.Example:

1 11 1 = 3

+ 1 1 = 31 1 0 = 6

J.Dimitorv, STRL, DMU, [email protected] – p. 8

Octal and Hexadecimal arithmetic

Similarly, in the case of octal we have1 1

5 7 = 47+ 2 3 = 19

1 0 2 = 66and hexadecimal

1 15 A = 90

+ D B = 2191 3 5 = 309

J.Dimitorv, STRL, DMU, [email protected] – p. 9

Negative numbers

Negative numbers come from the need to solveequations like

N + x = 0

where x is the unknown. In the arithmetic HomoSapiens uses, we denote x = −N . With binary wewill do the same:

0 1 0 0 1 1 0 1+

0 0 0 0 0 0 0 0

J.Dimitorv, STRL, DMU, [email protected] – p. 10

Negative numbers

Negative numbers come from the need to solveequations like

N + x = 0

where x is the unknown. In the arithmetic HomoSapiens uses, we denote x = −N . With binary wewill do the same:

10 1 0 0 1 1 0 1

+ 10 0 0 0 0 0 0 0

J.Dimitorv, STRL, DMU, [email protected] – p. 10

Negative numbers

Negative numbers come from the need to solveequations like

N + x = 0

where x is the unknown. In the arithmetic HomoSapiens uses, we denote x = −N . With binary wewill do the same:

1 10 1 0 0 1 1 0 1

+ 1 10 0 0 0 0 0 0 0

J.Dimitorv, STRL, DMU, [email protected] – p. 10

Negative numbers

Negative numbers come from the need to solveequations like

N + x = 0

where x is the unknown. In the arithmetic HomoSapiens uses, we denote x = −N . With binary wewill do the same:

1 1 10 1 0 0 1 1 0 1

+ 0 1 10 0 0 0 0 0 0 0

J.Dimitorv, STRL, DMU, [email protected] – p. 10

Negative numbers

Negative numbers come from the need to solveequations like

N + x = 0

where x is the unknown. In the arithmetic HomoSapiens uses, we denote x = −N . With binary wewill do the same:

1 1 1 10 1 0 0 1 1 0 1

+ 0 0 1 10 0 0 0 0 0 0 0

J.Dimitorv, STRL, DMU, [email protected] – p. 10

Negative numbers

Negative numbers come from the need to solveequations like

N + x = 0

where x is the unknown. In the arithmetic HomoSapiens uses, we denote x = −N . With binary wewill do the same:

1 1 1 1 10 1 0 0 1 1 0 1

+ 1 0 0 1 10 0 0 0 0 0 0 0

J.Dimitorv, STRL, DMU, [email protected] – p. 10

Negative numbers

Negative numbers come from the need to solveequations like

N + x = 0

where x is the unknown. In the arithmetic HomoSapiens uses, we denote x = −N . With binary wewill do the same:

1 1 1 1 1 10 1 0 0 1 1 0 1

+ 1 1 0 0 1 10 0 0 0 0 0 0 0

J.Dimitorv, STRL, DMU, [email protected] – p. 10

Negative numbers

Negative numbers come from the need to solveequations like

N + x = 0

where x is the unknown. In the arithmetic HomoSapiens uses, we denote x = −N . With binary wewill do the same:

1 1 1 1 1 1 10 1 0 0 1 1 0 1

+ 0 1 1 0 0 1 10 0 0 0 0 0 0 0

J.Dimitorv, STRL, DMU, [email protected] – p. 10

Negative numbers

Negative numbers come from the need to solveequations like

N + x = 0

where x is the unknown. In the arithmetic HomoSapiens uses, we denote x = −N . With binary wewill do the same:1 1 1 1 1 1 1 1

0 1 0 0 1 1 0 1+ 1 0 1 1 0 0 1 1

0 0 0 0 0 0 0 0

J.Dimitorv, STRL, DMU, [email protected] – p. 10

Negative numbers

Negative numbers come from the need to solveequations like

N + x = 0

where x is the unknown. In the arithmetic HomoSapiens uses, we denote x = −N . With binary wewill do the same:1 1 1 1 1 1 1 1

0 1 0 0 1 1 0 1+ 1 0 1 1 0 0 1 1

0 0 0 0 0 0 0 0and we will ignore the last carry 1.

J.Dimitorv, STRL, DMU, [email protected] – p. 10

2’s complement in base 2

In our previous example we saw that the negativefor 01001101 is 10110011. We can also find thenegative if we do a simple procedure.

0 1 0 0 1 1 0 1

+

J.Dimitorv, STRL, DMU, [email protected] – p. 11

2’s complement in base 2

In our previous example we saw that the negativefor 01001101 is 10110011. We can also find thenegative if we do a simple procedure.

0 1 0 0 1 1 0 10

+

J.Dimitorv, STRL, DMU, [email protected] – p. 11

2’s complement in base 2

In our previous example we saw that the negativefor 01001101 is 10110011. We can also find thenegative if we do a simple procedure.

0 1 0 0 1 1 0 11 0

+

J.Dimitorv, STRL, DMU, [email protected] – p. 11

2’s complement in base 2

In our previous example we saw that the negativefor 01001101 is 10110011. We can also find thenegative if we do a simple procedure.

0 1 0 0 1 1 0 10 1 0

+

J.Dimitorv, STRL, DMU, [email protected] – p. 11

2’s complement in base 2

In our previous example we saw that the negativefor 01001101 is 10110011. We can also find thenegative if we do a simple procedure.

0 1 0 0 1 1 0 10 0 1 0

+

J.Dimitorv, STRL, DMU, [email protected] – p. 11

2’s complement in base 2

In our previous example we saw that the negativefor 01001101 is 10110011. We can also find thenegative if we do a simple procedure.

0 1 0 0 1 1 0 11 0 0 1 0

+

J.Dimitorv, STRL, DMU, [email protected] – p. 11

2’s complement in base 2

In our previous example we saw that the negativefor 01001101 is 10110011. We can also find thenegative if we do a simple procedure.

0 1 0 0 1 1 0 11 1 0 0 1 0

+

J.Dimitorv, STRL, DMU, [email protected] – p. 11

2’s complement in base 2

In our previous example we saw that the negativefor 01001101 is 10110011. We can also find thenegative if we do a simple procedure.

0 1 0 0 1 1 0 10 1 1 0 0 1 0

+

J.Dimitorv, STRL, DMU, [email protected] – p. 11

2’s complement in base 2

In our previous example we saw that the negativefor 01001101 is 10110011. We can also find thenegative if we do a simple procedure.

0 1 0 0 1 1 0 11 0 1 1 0 0 1 0

+

J.Dimitorv, STRL, DMU, [email protected] – p. 11

2’s complement in base 2

In our previous example we saw that the negativefor 01001101 is 10110011. We can also find thenegative if we do a simple procedure.

0 1 0 0 1 1 0 11 0 1 1 0 0 1 0

+ 11 0 1 1 0 0 1 1

J.Dimitorv, STRL, DMU, [email protected] – p. 11

2’s complement in base 2

In our previous example we saw that the negativefor 01001101 is 10110011. We can also find thenegative if we do a simple procedure.

0 1 0 0 1 1 0 11 0 1 1 0 0 1 0

+ 11 0 1 1 0 0 1 1

What did we actually do? We will demonstrate itwith 2’s complement in Hex

J.Dimitorv, STRL, DMU, [email protected] – p. 11

2’s complement in base 16

Let us arrange the digits in base 16 as0 1 2 3 4 5 6 7 8 9 A B C D E F

How will we find the inverse for 75(16)?

7 5

J.Dimitorv, STRL, DMU, [email protected] – p. 12

2’s complement in base 16

Let us arrange the digits in base 16 as0 1 2 3 4 5 6 7 8 9 A B C D E F

How will we find the inverse for 75(16)?

7 5

J.Dimitorv, STRL, DMU, [email protected] – p. 12

2’s complement in base 16

Let us arrange the digits in base 16 as0 1 2 3 4 5 6 7 8 9 A B C D E F

↑ ↑

How will we find the inverse for 75(16)?

7 5

J.Dimitorv, STRL, DMU, [email protected] – p. 12

2’s complement in base 16

Let us arrange the digits in base 16 as0 1 2 3 4 5 6 7 8 9 A B C D E F

↑ ↑

How will we find the inverse for 75(16)?

7 5A

J.Dimitorv, STRL, DMU, [email protected] – p. 12

2’s complement in base 16

Let us arrange the digits in base 16 as0 1 2 3 4 5 6 7 8 9 A B C D E F

↑ ↑

How will we find the inverse for 75(16)?

7 5A

J.Dimitorv, STRL, DMU, [email protected] – p. 12

2’s complement in base 16

Let us arrange the digits in base 16 as0 1 2 3 4 5 6 7 8 9 A B C D E F

↑ ↑

↑ ↑

How will we find the inverse for 75(16)?

7 5A

J.Dimitorv, STRL, DMU, [email protected] – p. 12

2’s complement in base 16

Let us arrange the digits in base 16 as0 1 2 3 4 5 6 7 8 9 A B C D E F

↑ ↑

↑ ↑

How will we find the inverse for 75(16)?

7 58 A

J.Dimitorv, STRL, DMU, [email protected] – p. 12

2’s complement in base 16

Let us arrange the digits in base 16 as0 1 2 3 4 5 6 7 8 9 A B C D E F

↑ ↑

↑ ↑

How will we find the inverse for 75(16)?

7 58 A

+ 18 B

Therefore 8A is the inverse and 8B is the 2’scomplement in Hex. We can check this by adding75 + 8B

J.Dimitorv, STRL, DMU, [email protected] – p. 12

Real numbers

Do computers work with reals?

J.Dimitorv, STRL, DMU, [email protected] – p. 13

Real numbers

Do computers work with reals?

Answer: NO

J.Dimitorv, STRL, DMU, [email protected] – p. 13

Real numbers

Do computers work with reals?

Answer: NO

They work with rational numbers thatapproximate reals.

J.Dimitorv, STRL, DMU, [email protected] – p. 13

Real numbers

Do computers work with reals?

Answer: NO

They work with rational numbers thatapproximate reals.

We use mantissa M and exponent E torepresent numbers as

M × 10E ,

where M and E are signed numbers.

J.Dimitorv, STRL, DMU, [email protected] – p. 13

Real numbers

Do computers work with reals?

Answer: NO

They work with rational numbers thatapproximate reals.

We use mantissa M and exponent E torepresent numbers as

M × 10E ,

where M and E are signed numbers.

This representation is called floating pointbecause by changing the exponent we movethe decimal point.

J.Dimitorv, STRL, DMU, [email protected] – p. 13