Upcoming SlideShare
×

# The Idea of Algorithms and Number Systems

1,647 views

Published on

An introduction to Computer Science and its crux -- algorithms. A coverage of the most typical numeral systems: binary, octal, decimal, hexadecimal.

0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

Views
Total views
1,647
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
32
0
Likes
0
Embeds 0
No embeds

No notes for slide

### The Idea of Algorithms and Number Systems

1. 1. CPSC 126Introduction to Computer Science Arber Borici May 08, 2009
2. 2. 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 CPSC 126 - A. Borici 2
3. 3. Review 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. CPSC 126 - A. Borici 3
4. 4. 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.” CPSC 126 - A. Borici 4
5. 5. 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. CPSC 126 - A. Borici 5
6. 6. Algorithms: 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. CPSC 126 - A. Borici 6
7. 7. Algorithms: 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: CPSC 126 - A. Borici 7
8. 8. Algorithms: 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. CPSC 126 - A. Borici 8
9. 9. Positional Number Systems 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. CPSC 126 - A. Borici 9
10. 10. 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: 126 . 101   integer real part part 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. CPSC 126 - A. Borici 10
11. 11. Hmm, Positional… E.g. 126.101, as we know it, is indeed expressed as: 126.101 = 1 ⋅10 2 + 2 ⋅101 + 6 ⋅100 + 1 ⋅10 −1 + 0 ⋅10 −2 +1 ⋅10 −3 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 CPSC 126 - A. Borici 11
12. 12. The 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. CPSC 126 - A. Borici 12
13. 13. The 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. CPSC 126 - A. Borici 13
14. 14. The 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? CPSC 126 - A. Borici 14
15. 15. 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. CPSC 126 - A. Borici 15
16. 16. Binary 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: Powers of 2: 256 128 64 32 16 8 4 2 1 Our number: 1 0 0 1 1 1 0 0 Result: 0*1 + 0*2 + 1*4 + 1*8 + 1*16 + 1*128 = 156. Therefore, 100111002=(156)10. CPSC 126 - A. Borici 16
17. 17. Decimal to BinaryAlgorithm 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. CPSC 126 - A. Borici 17
18. 18. Decimal to Binary: Algorithm 1 ExampleConvert 15610 into binary: List as many powers of 2 as necessary: 256 128 64 32 16 8 4 2 1 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. CPSC 126 - A. Borici 18
19. 19. Algorithm 1: Why does it 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. CPSC 126 - A. Borici 19
20. 20. Decimal to BinaryAlgorithm 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. CPSC 126 - A. Borici 20
21. 21. Decimal to Binary: Algorithm 1 ExampleConvert 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 = 0, remainder = 1 (done!) 15610=100111002. CPSC 126 - A. Borici 21
22. 22. Algorithm 2: Why does it work? 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: 794 = 7 ⋅102 + 9 ⋅10 + 4 Let us factor the expression on the right: 794 = (7 ⋅10 + 9) ⋅10 + 4 Now, if we divide 794 by 10, we get the remainder 4. Compare this to the division by 10 of the factored expression. CPSC 126 - A. Borici 22
23. 23. Algorithm 2: Why does it work? Continue dividing the factored expression until no digit is left to divide, and record the remainders. (7 ⋅10 + 9) ⋅10 + 4 Division 1: 10 = 7 ⋅10 + 9, remainder = 4 7 ⋅10 + 9 Division 2: = 7, remainder = 9 10 7 = 0, remainder = 7 Division 3 10 (done!) CPSC 126 - A. Borici 23
24. 24. Algorithm 2: Why does it work? In general, given a number X and a number system base b: ( xn xn −1...x1 )b = xn ⋅ b n −1 + xn −1 ⋅ b n − 2 + ... + x1 = = ((( xn ⋅ b + xn −1 ) ⋅ b + xn − 2 ) ⋅ b + ...) ⋅ b + x1. 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. CPSC 126 - A. Borici 24
25. 25. Algorithm 2: Example We want to write 1510 in binary. So: 1510 = ( x3 x2 x1x0 ) 2 From the previous formula, we know that: 1510 = (( x3 ⋅ 2 + x2 ) ⋅ 2 + x1 ) ⋅ 2 + x0 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. CPSC 126 - A. Borici 25
26. 26. Algorithm 2: Application 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.) CPSC 126 - A. Borici 26
27. 27. Algorithm 2. In General… 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. CPSC 126 - A. Borici 27
28. 28. Bin2Dec and Dec2Bin:ExercisesA1. 00000100012 = (?)10A2. 1110002 = (?)10A3. 25610 = (?)2A4. 17910 = (?)2Extra-credit (note the decimal point!)A5. (101.11)2 = (?.?)10A6. (5.75)10 = (?.?)2A7. (222)3 = (?)10A8. (10)10 = (?)3 CPSC 126 - A. Borici 28
29. 29. Binary to Hexadecimal 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. CPSC 126 - A. Borici 29
30. 30. Hexadecimal to Binary 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. CPSC 126 - A. Borici 30
31. 31. Binary to Octal 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. CPSC 126 - A. Borici 31
32. 32. Octal to Binary 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. CPSC 126 - A. Borici 32
33. 33. Octal to Decimal Remember our friend, the formula: ( xn xn −1...x1 )b = xn ⋅ b n −1 + xn −1 ⋅ b n − 2 + ... + x1 = = ((( xn ⋅ b + xn −1 ) ⋅ b + xn − 2 ) ⋅ b + ...) ⋅ b + x1. 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. CPSC 126 - A. Borici 33
34. 34. Decimal to Octal 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 CPSC 126 - A. Borici 34
35. 35. Octal to HexadecimalSeveral 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. CPSC 126 - A. Borici 35
36. 36. Decimal to Hexadecimal 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. CPSC 126 - A. Borici 36
37. 37. Hexadecimal to Decimal 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. CPSC 126 - A. Borici 37
38. 38. Some Fun: ExercisesB1. 1238 = (?)10B2. 7916 = (?)8B3. 25616 = (?)10B4. 12710 = (?)8B5. 3338 = (?)16Extra-credit (notice the bases):B6. 5555 = (?)10B7. 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 CPSC 126 - A. Borici 38
39. 39. The Binary Numbering System 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…) CPSC 126 - A. Borici 39
40. 40. The Binary Numbering System 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. CPSC 126 - A. Borici 40
41. 41. The Binary Numbering System 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. CPSC 126 - A. Borici 41
42. 42. The Binary Numbering System 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. CPSC 126 - A. Borici 42
43. 43. The Binary Numbering System 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! CPSC 126 - A. Borici 43
44. 44. The Binary Numbering System 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. CPSC 126 - A. Borici 44
45. 45. THE END