1 / 45

Introduction to Computer Science: Algorithms and Numbering Systems

Arber Borici May 08, 2009. Introduction to Computer Science: Algorithms and Numbering Systems. Outline. Review Definition of Computer Science: Gibbs & Tucker Definition of Algorithm. Examples Positional Number Systems Conversion Algorithms Exercises The Binary Numbering System

gmcmahan
Download Presentation

Introduction to Computer Science: Algorithms and Numbering Systems

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. CPSC 126 - A. Borici Outline 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

  3. CPSC 126 - A. Borici Review • Formal Definition of Computer Science Computer Science is the study of algorithms, including: • Their formal properties • Their hardware realizations • Their software realizations • Their applications (Gibbs & Tucks) • Keywords: algorithm, formal, hardware, software, applications.

  4. CPSC 126 - A. Borici “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.” What CS is not…

  5. CPSC 126 - A. Borici What is an algorithm? • A set of a finite number of instructions for solving a particular problem. • Instructions are: • Sequential: A task is accomplished in a particular step. • Conditional: A question is asked at a particular step. The next instruction depends upon this answer. • Iterative: A certain number of steps is repeated back and forth until a stopping criterion is met.

  6. CPSC 126 - A. Borici 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. Algorithms: Example

  7. CPSC 126 - A. Borici 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: Algorithms: Example

  8. CPSC 126 - A. Borici 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. Algorithms: Bottom line

  9. CPSC 126 - A. Borici Positional Number Systems • Infinite possible numbering systems. • Most common: • The Decimal System: 0, 1, 2, …, 9 • The Binary System: 0 and 1 • The Octal System: 0, 1, 2, …, 7 • 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.

  10. CPSC 126 - A. Borici 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. Positional?!

  11. CPSC 126 - A. Borici 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 Hmm, Positional…

  12. CPSC 126 - A. Borici 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. The Binary System

  13. CPSC 126 - A. Borici 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)8is NOT the decimal number one hundred. The Octal System

  14. CPSC 126 - A. Borici 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? The Hexadecimal System

  15. CPSC 126 - A. Borici Yes! Guess how… • …using algorithms! • We will focus on the following conversions: • Binary to Decimal and vice versa • Binary to Hexadecimal and vice versa • Binary to Octal and vice versa • Octal to Decimal and vice versa • Octal to Hexadecimal and vice versa • Decimal to Hexadecimal and vice versa • Examples with fractional numbers as well.

  16. CPSC 126 - A. Borici 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. Binary to Decimal

  17. CPSC 126 - A. Borici Decimal to Binary Algorithm 1: Comparison with descending powers of 2 and subtraction. • List powers of 2 from right to left until the power value exceeds the value of the decimal number. • If the number fits into a power, write 1 and subtract the power from the number. • Move to the next lower power of 2 and repeat steps 1 and 2 until there are no more powers of 2 left.

  18. CPSC 126 - A. Borici Convert 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. Decimal to Binary: Algorithm 1 Example

  19. CPSC 126 - A. Borici 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. Algorithm 1: Why does it work?

  20. CPSC 126 - A. Borici Decimal to Binary Algorithm 2: Repetitive division by 2. • Divide the decimal number by 2. Record the division result and write the remainder. • Repeat step 1 until the division result is zero. • Division by 2 either yields no remainder or the remainder is 1.

  21. CPSC 126 - A. Borici Decimal to Binary: Algorithm 1 Example Convert 15610 into binary: • Divide by 2 and record the remainder. • 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 = 0, remainder = 1 (done!) • 15610=100111002.

  22. CPSC 126 - A. Borici 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 work?

  23. CPSC 126 - A. Borici 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 work?

  24. CPSC 126 - A. Borici 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 work?

  25. CPSC 126 - A. Borici 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: Example

  26. CPSC 126 - A. Borici 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: Application

  27. CPSC 126 - A. Borici 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…

  28. CPSC 126 - A. Borici 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: Exercises

  29. CPSC 126 - A. Borici 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 Hexadecimal

  30. CPSC 126 - A. Borici 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 Binary

  31. CPSC 126 - A. Borici 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 Octal

  32. CPSC 126 - A. Borici 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 Binary

  33. CPSC 126 - A. Borici 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 Decimal

  34. CPSC 126 - A. Borici 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 Octal

  35. CPSC 126 - A. Borici 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 Hexadecimal

  36. CPSC 126 - A. Borici 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 Hexadecimal

  37. CPSC 126 - A. Borici 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 Decimal

  38. CPSC 126 - A. Borici B1. 1238 = (?)10 B2. 7916 = (?)8 B3. 25616 = (?)10 B4. 12710 = (?)8 B5. 3338 = (?)16 Extra-credit(noticethebases): 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: Exercises

  39. CPSC 126 - A. Borici 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 System

  40. CPSC 126 - A. Borici 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 System

  41. CPSC 126 - A. Borici 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 System

  42. CPSC 126 - A. Borici 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 System

  43. CPSC 126 - A. Borici 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 System

  44. CPSC 126 - A. Borici 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 System

  45. THE END

More Related