45
Introduction to Introduction to Computer Science: Computer Science: Algorithms and Numbering Systems Algorithms and Numbering Systems Arber Borici May 08, 2009

Introduction to Computer Science: Algorithms and Numbering Systems Arber Borici May 08, 2009

Embed Size (px)

Citation preview

Page 1: Introduction to Computer Science: Algorithms and Numbering Systems Arber Borici May 08, 2009

Introduction to Introduction to Computer Science:Computer Science:

Algorithms and Numbering SystemsAlgorithms and Numbering Systems

Arber BoriciMay 08, 2009

Page 2: Introduction to Computer Science: Algorithms and Numbering Systems Arber Borici May 08, 2009

CPSC 126 - A. Borici 2

OutlineOutline Review

Definition of Computer Science: Gibbs & Tucker Definition of Algorithm. Examples Positional Number Systems

Conversion Algorithms Exercises

The Binary Numbering System Introduction to the binary system Why the binary system?

About the Lab Assignments

Page 3: Introduction to Computer Science: Algorithms and Numbering Systems Arber Borici May 08, 2009

CPSC 126 - A. Borici 3

ReviewReview

Formal Definition of Computer Science

Computer Science is the study of algorithms, including:1. Their formal properties

2. Their hardware realizations

3. Their software realizations

4. Their applications (Gibbs & Tucks)

Keywords: algorithm, formal, hardware, software, applications.

Page 4: Introduction to Computer Science: Algorithms and Numbering Systems Arber Borici May 08, 2009

CPSC 126 - A. Borici 4

What CS is not…What CS is not…

“Arber, my floppy disk doesn’t work. Can you fix it?” (A friend of mine, before being introduced to the science of computing.)

“Arber, my Internet connection is slow. Can you make it faster?” (That same friend of mine, after taking a computing science course.)

“I forgot how to fix it.”

Page 5: Introduction to Computer Science: Algorithms and Numbering Systems Arber Borici May 08, 2009

CPSC 126 - A. Borici 5

What is an algorithm?What is an algorithm?

A set of a finite number of instructions for solving a particular problem.

Instructions are:1. Sequential: A task is accomplished in a

particular step.

2. Conditional: A question is asked at a particular step. The next instruction depends upon this answer.

3. Iterative: A certain number of steps is repeated back and forth until a stopping criterion is met.

Page 6: Introduction to Computer Science: Algorithms and Numbering Systems Arber Borici May 08, 2009

CPSC 126 - A. Borici 6

Algorithms: ExampleAlgorithms: Example

Multiply 32 and 19. Step 1: Start with the next least significant digit of the

second integer. Step 2: Multiply, right to left, each digit of the first

integer with the digit chosen in Step 1. Step 3: Is there a carry-on? If yes, add it to the next

multiplication result. Step 4: Repeat Steps 2 to 3 until there are no more

digits of the first number to multiply. Step 5: Repeat Steps 1 to 4 until there are no more

digits of the second number to multiply. Step 6: Add the results.

Page 7: Introduction to Computer Science: Algorithms and Numbering Systems Arber Borici May 08, 2009

CPSC 126 - A. Borici 7

Algorithms: ExampleAlgorithms: Example

Serious issue: Given a coffee mug filled with tea and a tea cup filled with coffee, how can one swap the contents? Use any reasonable, but efficient means.

Solution: STEP 1

STEP 2

STEP 3

Page 8: Introduction to Computer Science: Algorithms and Numbering Systems Arber Borici May 08, 2009

CPSC 126 - A. Borici 8

Algorithms: Bottom lineAlgorithms: Bottom line

We need an algorithm in order to automate the solution of a problem. That’s why we study formal, mathematical properties of algorithms.

An algorithm is general. We can implement it in a machine, unless a

human being is willing to follow its steps towards the solution.

Such an entity is referred to as a computer agent.

Page 9: Introduction to Computer Science: Algorithms and Numbering Systems Arber Borici May 08, 2009

CPSC 126 - A. Borici 9

Positional Number Positional Number SystemsSystems Infinite possible numbering systems. Most common:

1. The Decimal System: 0, 1, 2, …, 9

2. The Binary System: 0 and 1

3. The Octal System: 0, 1, 2, …, 7

4. The Hexadecimal System: 0, 1, 2, …, 15 Properties:

Base (a.k.a. radix): 10, 2, 8, 16, etc. The right-most digit is the least significant digit.

Page 10: Introduction to Computer Science: Algorithms and Numbering Systems Arber Borici May 08, 2009

CPSC 126 - A. Borici 10

Positional?!Positional?!

Yes! Because the value of a digit depends on its specific position within a number, determined by the integer part and the real part.

The decimal number 126.101:

In the decimal system, there are 10 unique digits (0-9), i.e. the base is 10. Therefore, the value of positions is based on powers of 10.

partreal

part integer

101 .126

Page 11: Introduction to Computer Science: Algorithms and Numbering Systems Arber Borici May 08, 2009

CPSC 126 - A. Borici 11

Hmm, Positional…Hmm, Positional… E.g. 126.101, as we know it, is indeed expressed

as:

Notice how the decimal number is expressed in terms of the system’s unique digits (0-9) and powers of the base (10).

Bottom line: Every number can be expressed in terms of its system’s digits and powers of its base.

Identify the base to avoid confusion: (126.101)10

321

012

101100101

106102101101.126

Page 12: Introduction to Computer Science: Algorithms and Numbering Systems Arber Borici May 08, 2009

CPSC 126 - A. Borici 12

The Binary SystemThe Binary System

Two values: 0 and 1. Based on powers of 2. Moving from right to left, what are the

powers of the first 5 positions? The two digits are referred to as bits, a

portmanteau word derived from the contraction of words BInary digitTS.

Page 13: Introduction to Computer Science: Algorithms and Numbering Systems Arber Borici May 08, 2009

CPSC 126 - A. Borici 13

The Octal SystemThe Octal System

Eight values: 0, 1, …, 7 Based on powers of 8. Moving from right to left, the first three

powers of 8 are: 1, 8, and 64. E.g.: (100)8 is NOT the decimal number one

hundred.

Page 14: Introduction to Computer Science: Algorithms and Numbering Systems Arber Borici May 08, 2009

CPSC 126 - A. Borici 14

The Hexadecimal SystemThe Hexadecimal System

Also known as the hex system 16 unique digits: 0, 1, …, 15. Because digits 10-15 are, indeed, groups of two

atoms, we use letters A, B, C, D, E, and F to represent digits 10, 11, 12, 13, 14, and 15, respectively.

Based on powers of 16. Moving from right to left, the first three powers of

16 are: 1, 16, and 256. Can we convert numbers of a system into their

counterparts in other systems?

Page 15: Introduction to Computer Science: Algorithms and Numbering Systems Arber Borici May 08, 2009

CPSC 126 - A. Borici 15

Yes! Guess how…Yes! Guess how…

…using algorithms! We will focus on the following conversions:

1. Binary to Decimal and vice versa

2. Binary to Hexadecimal and vice versa

3. Binary to Octal and vice versa

4. Octal to Decimal and vice versa

5. Octal to Hexadecimal and vice versa

6. Decimal to Hexadecimal and vice versa Examples with fractional numbers as well.

Page 16: Introduction to Computer Science: Algorithms and Numbering Systems Arber Borici May 08, 2009

CPSC 126 - A. Borici 16

Binary to DecimalBinary to Decimal

Algorithm: Starting from the right and proceeding to the left, we multiply each binary digit with the corresponding positional power of 2 and sum results.

Example: 100111002=(?)10. Solution:

Result: 0*1 + 0*2 + 1*4 + 1*8 + 1*16 + 1*128 = 156. Therefore, 100111002=(156)10.

Powers of 2: 256 128 64 32 16 8 4 2 1

Our number: 1 0 0 1 1 1 0 0

Page 17: Introduction to Computer Science: Algorithms and Numbering Systems Arber Borici May 08, 2009

CPSC 126 - A. Borici 17

Decimal to BinaryDecimal to Binary

Algorithm 1: Comparison with descending powers of 2 and subtraction.

1. List powers of 2 from right to left until the power value exceeds the value of the decimal number.

2. If the number fits into a power, write 1 and subtract the power from the number.

3. Move to the next lower power of 2 and repeat steps 1 and 2 until there are no more powers of 2 left.

Page 18: Introduction to Computer Science: Algorithms and Numbering Systems Arber Borici May 08, 2009

CPSC 126 - A. Borici 18

Decimal to Binary: Algorithm 1 Decimal to Binary: Algorithm 1 ExampleExampleConvert 15610 into binary: List as many powers of 2 as necessary:

Does 256 fit in 156? No, write 0. Does 128 fit in 156? Yes, write 1 and subtract 128 from 156:

156-128=28. Do 64 or 32 fit in 28? No, write 00. But 16 fits in 28, so write

1 and subtract 28-16 = 12. 8 fits into 12, so we write 1 and subtract to get 4. 4 fits into

4, we write 1 and we subtract to get 0. 2 does not fit into 0, so we write 0. 1 does not fit into zero, we write 0.

Because there are no more powers of 2, we stop. The result is: 010011100. Removing the left-most

insignificant zero, we get 15610=100111002.

256 128 64 32 16 8 4 2 1

Page 19: Introduction to Computer Science: Algorithms and Numbering Systems Arber Borici May 08, 2009

CPSC 126 - A. Borici 19

Algorithm 1: Why does it Algorithm 1: Why does it work?work? Algorithm 1 is the reverse procedure of the

binary-to-decimal conversion algorithm, in which case each binary digit is multiplied, right to left, with incrementing powers of 2. Therefore, algorithm 1 attempts to find those 1’s which will yield the respective decimal number.

Page 20: Introduction to Computer Science: Algorithms and Numbering Systems Arber Borici May 08, 2009

CPSC 126 - A. Borici 20

Decimal to BinaryDecimal to Binary

Algorithm 2: Repetitive division by 2.

1. Divide the decimal number by 2. Record the division result and write the remainder.

2. Repeat step 1 until the division result is zero.

Division by 2 either yields no remainder or the remainder is 1.

Page 21: Introduction to Computer Science: Algorithms and Numbering Systems Arber Borici May 08, 2009

CPSC 126 - A. Borici 21

Decimal to Binary: Algorithm 1 Decimal to Binary: Algorithm 1 ExampleExampleConvert 15610 into binary:

1. Divide by 2 and record the remainder.

2. Done when division yields 0.156 / 2 = 78, remainder = 0

78 / 2 = 39, remainder = 0

39 / 2 = 19, remainder = 1

19 / 2 = 9, remainder = 1

9 / 2 = 4, remainder = 1

4 / 2 = 2, remainder = 0

2 / 2 = 1, remainder = 0

1 / 2 = 00, remainder = 1 (done!)

15610=100111002.

Page 22: Introduction to Computer Science: Algorithms and Numbering Systems Arber Borici May 08, 2009

CPSC 126 - A. Borici 22

Not just because… Mathematical reason! Consider the decimal system: every

number can be expressed in terms of the system’s digits and their positional powers of 10:

Let us factor the expression on the right:

Now, if we divide 794 by 10, we get the remainder 4. Compare this to the division by 10 of the factored expression.

Algorithm 2: Why does it Algorithm 2: Why does it work?work?

4109107794 2

410)9107(794

Page 23: Introduction to Computer Science: Algorithms and Numbering Systems Arber Borici May 08, 2009

CPSC 126 - A. Borici 23

Continue dividing the factored expression until no digit is left to divide, and record the remainders.

Division 1:

Division 2:

Division 3 (done!)

Algorithm 2: Why does it Algorithm 2: Why does it work?work?

4remainder ,910710

410)9107(

9remainder ,710

9107

7remainder ,010

7

Page 24: Introduction to Computer Science: Algorithms and Numbering Systems Arber Borici May 08, 2009

CPSC 126 - A. Borici 24

In general, given a number X and a number system base b:

Bottom line: A repetitive division of a number X in base b by the new base r will yield a number in the system represented by r.

Example: X = 15610. A repetitive division by the same base, 10, will yield the same number. A division by base 2, will yield number 10011100 in the binary system.

Algorithm 2: Why does it Algorithm 2: Why does it work?work?

....)))(((

...)...(

121

12

11

11

xbbxbxbx

xbxbxxxx

nnn

nn

nnbnn

Page 25: Introduction to Computer Science: Algorithms and Numbering Systems Arber Borici May 08, 2009

CPSC 126 - A. Borici 25

We want to write 1510 in binary. So: From the previous formula, we know that:

Now, if we divide by 2 both sides, the remainder of 15/2 is 1, and the remainder of the expression on the right is x0. Therefore, x0=1.

Proceed the same way until the end.

Algorithm 2: ExampleAlgorithm 2: Example

2012310 )(15 xxxx

012310 2)2)2((15 xxxx

Page 26: Introduction to Computer Science: Algorithms and Numbering Systems Arber Borici May 08, 2009

CPSC 126 - A. Borici 26

Unfortunately, because we are trained to divide in the decimal system, Algorithm 2 is used to convert decimal numbers to numbers of any other system of base b.

E.g. we are not trained on how to divide binary number 1111 by 10, if we wanted to convert it into decimal. (Indeed, the binary-to-decimal conversion is the opposite of division, in which case we multiply positional powers of 2 and add them up.)

Algorithm 2: ApplicationAlgorithm 2: Application

Page 27: Introduction to Computer Science: Algorithms and Numbering Systems Arber Borici May 08, 2009

CPSC 126 - A. Borici 27

In general, to convert a number from a base-b system into a decimal system, we multiply each digit of that number with the positional power of its base b.

Example: (25)4 = 5 + 2*4 = (13)10

Why does this work, in the general case? Grouping of unique digits implies repetitions

based on powers of the radix. Counting, as we know it, is done in decimal.

Algorithm 2. In General…Algorithm 2. In General…

Page 28: Introduction to Computer Science: Algorithms and Numbering Systems Arber Borici May 08, 2009

CPSC 126 - A. Borici 28

A1. 00000100012 = (?)10

A2. 1110002 = (?)10

A3. 25610 = (?)2

A4. 17910 = (?)2

Extra-credit (note the decimal point!)

A5. (101.11)2 = (?.?)10

A6. (5.75)10 = (?.?)2

A7. (222)3 = (?)10

A8. (10)10 = (?)3

Bin2Dec and Dec2Bin: Bin2Dec and Dec2Bin: ExercisesExercises

Page 29: Introduction to Computer Science: Algorithms and Numbering Systems Arber Borici May 08, 2009

CPSC 126 - A. Borici 29

Algorithm: Group the binary digits in groups of 4, starting from the right. If the need arises, add as many zeros to the left of the binary number as it is necessary to partition the entire binary number into groups of 4. For each group, write the corresponding hexadecimal digit.

Example: 111112 is grouped as 1111 and the left-most 1 needs three zeros (to the left) to form a group of 4: (0001 1111). The corresponding hex digit for 1111 is F and for 0001 is 1. So, 111112 = 1F16.

Binary to HexadecimalBinary to Hexadecimal

Page 30: Introduction to Computer Science: Algorithms and Numbering Systems Arber Borici May 08, 2009

CPSC 126 - A. Borici 30

Algorithm: Starting from the right, for each hex digit, write the 4 corresponding binary digits.

Example: 1F16: F16=11112 and 116=00012.

Example: ABCDEF16 =

1010 1011 1100 1101 1110 1111.

Hexadecimal to BinaryHexadecimal to Binary

Page 31: Introduction to Computer Science: Algorithms and Numbering Systems Arber Borici May 08, 2009

CPSC 126 - A. Borici 31

Algorithm: Group the binary digits in groups of 3, starting from the right. If the need arises, add as many zeros to the left of the binary number as it is necessary to partition the entire binary number into groups of 3. For each group, write the corresponding hexadecimal digit.

Example: 111112 is grouped as 111 and the left-most bits 11 need a zero (to the left) to form a group of 3: (011 111). The corresponding octal number for 111 is 7 and for 011 is 3. So, 111112 = 378.

Binary to OctalBinary to Octal

Page 32: Introduction to Computer Science: Algorithms and Numbering Systems Arber Borici May 08, 2009

CPSC 126 - A. Borici 32

Algorithm: Starting from the right, for each octal digit, write the 3 corresponding binary digits.

Example: 378: 78=1112 and 38=0112.

Example: 6668 = 110 110 110.

Octal to BinaryOctal to Binary

Page 33: Introduction to Computer Science: Algorithms and Numbering Systems Arber Borici May 08, 2009

CPSC 126 - A. Borici 33

Remember our friend, the formula:

Given an octal number, for which the base b=8, we multiply each digit with positional powers of 8 and sum up to get the decimal counterpart.

Example: 1378 =7 + 3*8 + 1*64 = 9510.

Octal to DecimalOctal to Decimal

....)))(((

...)...(

121

12

11

11

xbbxbxbx

xbxbxxxx

nnn

nn

nnbnn

Page 34: Introduction to Computer Science: Algorithms and Numbering Systems Arber Borici May 08, 2009

CPSC 126 - A. Borici 34

Algorithm: Divide repetitively by 8 until the quotient is zero.

Why? Our friend the formula comes into play… Perform the opposite operation – division.

Example: 17810 = (?)8

178 / 8 = 22, remainder = 2

22 / 8 = 2, remainder = 6

2 / 8 = 0, remainder = 2.

Therefore, 17810 = (262)8

Decimal to OctalDecimal to Octal

Page 35: Introduction to Computer Science: Algorithms and Numbering Systems Arber Borici May 08, 2009

CPSC 126 - A. Borici 35

Several ways: Convert the octal to decimal and then the decimal

to hexadecimal Convert the octal to binary (HOW?) and then the

binary to hexadecimal (HOW?) – easier! Example: 168 = (?)16.

168 = (001 110)2

(001 110)2 = (0000 1110)2

(1110)2 = E16. Hexadecimal to octal conversion is the reverse.

Octal to HexadecimalOctal to Hexadecimal

Page 36: Introduction to Computer Science: Algorithms and Numbering Systems Arber Borici May 08, 2009

CPSC 126 - A. Borici 36

You guessed correctly – the formula! Algorithm: Divide repetitively by 16 until the

quotient is zero. Example: 1610 = (?)16.

16:16 = 1, remainder = 0

1:16 = 0, remainder = 1.

Therefore, 1610 = 1016.

Decimal to HexadecimalDecimal to Hexadecimal

Page 37: Introduction to Computer Science: Algorithms and Numbering Systems Arber Borici May 08, 2009

CPSC 126 - A. Borici 37

One way is to convert the hexadecimal number into binary, and use either of the binary-to-decimal conversion algorithm.

The wise way is based on our formula – multiplication of hexadecimal digits by positional powers of 16.

Example: ABC16 = (?)10.

A*162 + B*16 + C = 10*256 + 11*16 + 12

Therefore, ABC16 = 274810.

Hexadecimal to DecimalHexadecimal to Decimal

Page 38: Introduction to Computer Science: Algorithms and Numbering Systems Arber Borici May 08, 2009

CPSC 126 - A. Borici 38

B1. 1238 = (?)10

B2. 7916 = (?)8

B3. 25616 = (?)10

B4. 12710 = (?)8

B5. 3338 = (?)16

Extra-credit (notice the bases):

B6. 5555 = (?)10

B7. Assume a number system with base 36 and digits 0-9 and A-Z for numbers 10-35. Then, 1036 = (?)10.

B8. (4999)5000 = (?)10

Some Fun: ExercisesSome Fun: Exercises

Page 39: Introduction to Computer Science: Algorithms and Numbering Systems Arber Borici May 08, 2009

CPSC 126 - A. Borici 39

What’s the big deal about this “binary” system?

Why bits and not some other fancy nomenclature?

“I’m used to the decimal system since kindergarten! Why bother with a meaningless two-valued system?” (A college friend of mine, Marketing major…)

The Binary Numbering The Binary Numbering SystemSystem

Page 40: Introduction to Computer Science: Algorithms and Numbering Systems Arber Borici May 08, 2009

CPSC 126 - A. Borici 40

There is solid reason behind every scientific application of a formal method, including the binary system.

However, there is no theoretical reason why computers couldn’t use another number system.

The major, practical reason is the reliability of the binary numbering system.

The Binary Numbering The Binary Numbering SystemSystem

Page 41: Introduction to Computer Science: Algorithms and Numbering Systems Arber Borici May 08, 2009

CPSC 126 - A. Borici 41

Computers are electronic machines, i.e. information is internally represented in the form of currents and voltage levels.

Voltage is increasing and decreasing like a wave sketch. That is, it can be high or low. (Ring any bells?)

It seems intuitive to use 0 and 1 to represent the energy states.

The Binary Numbering The Binary Numbering SystemSystem

Page 42: Introduction to Computer Science: Algorithms and Numbering Systems Arber Borici May 08, 2009

CPSC 126 - A. Borici 42

Computers store and transfer information in terms of charges, in a particular voltage range.

Example: assume there is a calculating device operating with voltage range 0 and +90 V.

If we were to use the decimal system, then for each number 0 to 9, we would have to assign a corresponding voltage value between 0 and 90.

The Binary Numbering The Binary Numbering SystemSystem

Page 43: Introduction to Computer Science: Algorithms and Numbering Systems Arber Borici May 08, 2009

CPSC 126 - A. Borici 43

Therefore, 0 = 0V, 1=10V, …, 9 = 90V. So far so good. No theoretical model

violated. BUT, from an electronic standpoint, devices become unreliable as they age.

Consequently, devices change their energy states. Our calculating device, for instance, could drop from 90V to 83V after 5 years.

Problem: Now that the voltage range is 0 to 83, which decimal digit from 0 to 9 will represent 83? Is it digit 8 or is it digit 9?

Solution: Binary system!

The Binary Numbering The Binary Numbering SystemSystem

Page 44: Introduction to Computer Science: Algorithms and Numbering Systems Arber Borici May 08, 2009

CPSC 126 - A. Borici 44

Bits solve our problem – use bit 0 to represent the lowest value of the voltage range and bit 1 to represent the highest value of the range.

From the previous example, 0 = 0V and 1 = 90V. We now have a bistable environment, characterized

by two stable states. If the voltage drops to 83 with aging, then this value

still belongs to the energy state represented by bit 1. That’s a huge advantage of the binary system. Computer reliability increases because of the

binary representation of its internal building blocks.

The Binary Numbering The Binary Numbering SystemSystem

Page 45: Introduction to Computer Science: Algorithms and Numbering Systems Arber Borici May 08, 2009

THE END