# lec05-cs110 computational engineering

Post on 22-May-2015

60 views

Embed Size (px)

DESCRIPTION

A keynote on Problem Solving using ComputersTRANSCRIPT

- 1. CS110: Models of Computing Lecture 5 V. Kamakoti 10th January 2008

2. Todays Lecture Contents Number Systems and conversions Decimal Binary Octal Hexadecimal Representation of signed integers in Binary Twos complement representation Floating point in Binary system IEEE 754 - later in this course 3. Decimal System 986 = 9 * 102 + 8 * 101 + 6 * 100 The digits used are 0,1,2,,9 Adding and subtracting numbers in decimal is well known. Some interesting facts about multiplication and division are as follows. 4. Decimal Mult and Div Multiply by 10 is equivalent to Left shift the digits by one position and appending a zero For eg. 986 * 10 = 9860 Dividing by 10 is to right shift the digits by one. What remains is the quotient and gets out is the reminder. 986/10, gives 98 as quotient and 6 as reminder. 5. More interesting facts Multiplying a number by 10k is to left shift the number k times appending zeros. 986 * 103 = 986000 Dividing a number by 10k is to right shift it by k times. What remains is the quotient and leaves is the reminder. 98657 / 104 gives 9 as quotient and 8657 as reminder. True for any base b. 6. The Binary system Digits used are 0 and 1 Best suited for computers (1110)2 = 1*23 + 1*22 + 1*21 + 1*20 = (14)10 Multiplying a binary number by 2k is equivalent to left shifting the same k times appending zeros (1110)2 * 23 = (1110000)2 = 1*26 + 1*25 + 1*24 + 0*23 + 0*22 + 0*21 +0*20 = (112)10 This also teaches you conversion from binary to decimal 7. Any base to decimal conversion (1345)6 = 1*63 + 3*62 + 4*61 + 6*60 = (354)10 Systems with base k greater than 10 uses digits (0,1,2,3,,k-1). To avoid confusion 10 is represented by A, 11 by B and so on. For eg. The hexadecimal (base 16) system uses the alphabets A to F for representing the digits 10 to 15 in order. 8. The Binary system Dividing a binary number by 2k is equivalent to right shifting the same k times. (1110)2 / 23 gives (1)2 as quotient and (110)2 (14)10/(8)10 gives (1)10 as quotient and (6)10 as reminder. The rightmost bit is called the Least Significant Bit (LSB) and the leftmost bit is called the Most Significant Bit (MSB). For eg. in (1110)2, 1 is the MSB and 0 is the LSB. 9. Interesting facts If a binary number is divided by 2, the least significant bit is the reminder. The next to least significant bit becomes the lsb (right shifting). 1110 / 2 gives 111 as Q and 0 as reminder 111/2 gives 11 as Q and 1 as reminder 11/2 gives 1 as Q and 1 as reminder 1/2 gives 0 as Q and 1 as reminder Read the reminders from bottom to top and that is the original number. 10. Decimal to Binary Repeatedly divide the given number by 2 till the quotient becomes zero Read out the remainders in the reverse order. 14/2 = 7 (Quo) and 0 (Rem) 7/2 = 3 (Quo) and 1 (Rem) 3/2 = 1 (Quo) and 1 (Rem) 1/2 = 0 (Quo) and 1 (Rem) Therefore (14)10 = (1110)2 11. Octal System Base 8 and digits are 0,1,2,3,4,5,6,7. (756)8 = 7 * 82 + 5 * 81 + 6*80 = (494)10 As 8 = 23, (756)8 = (111)2*26 +(101)2*23+(110)2*20 = (111 101 110)2 Replace every digit in octal by equivalent 3-bit binary to convert octal to binary. Do vice versa for binary to octal So this is very useful for number representation in binary computers. 12. Hexadecimal System Base 16 and digits are 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F. (AB6)16 = 10 * 162 + 11 * 161 + 6*160 = (2742)10 As 16 = 24, (AB6)16 = (1010)2*28 +(1011)2*24+(0110)2*20 = (1010 1011 0110)2 Replace every digit in hexa by equivalent 4-bit binary to convert hexa to binary. Do vice versa for binary to hexa So this is also very useful for number representation in binary computers. 13. Signed Integer Representation We cannot store integers of any size There is limited space allotted by the system to store a number - say n bits. So if the number you try to store exceeds 2n 1, then only the first n-bits from right are stored. The most significant bits are lost. So let A be any number represented using n bits, then in our computer arithmetic, 2n + A = A. Given n = 4 and A = 11, 16 + 11 = 27, which is (11011)2. Since only 4 bits for representation, we get (1011)2 which is 11. 14. Signed Integers Given A, define Ac as the number got by toggling every bit. Given A = 1010, then Ac = 0101. If A is represented using n bits, then A + Ac = 2n - 1. Above example, n = 4, 1010 + 0101 = 1111 = 15. Ac + 1 = 2n - A = 2n + (-A) = -A To get negative of a number take its positive representation, toggle (complement) every bit and add one. Hence, named 2s complement. 15. Signed Integers n=3 0 = (000) 1 = (001); -1 = 110 + 1 = (111) 2 = (010); -2 = 101 + 1 = (110) 3 = (011); -3 = 100 + 1 = (101) 4 = (100); -4 = 011 + 1 = (100) So we represent -4,-3,-2,-1,0,1,2,3 Note that the MSB is 1 for negative and 0 for positive numbers. Removed +0 and -0 problem. Easy to see that with n bits the range is -2n-1 to 2n-1 -1 16. The Circle of Numbers 000 = 0 111 = -1001 = 1110 = -2010 = 2101 = -3011 = 3 100 = -4 17. Addition Clockwise (Eg. 1+2) 000 = 0 111 = -1001 = 1110 = -2010 = 2101 = -3011 = 3 100 = -4 18. Addition Clockwise (Eg. -3+2) 000 = 0 111 = -1001 = 1110 = -2010 = 2101 = -3011 = 3 100 = -4 19. Addition Clockwise (Eg. -2+3) 000 = 0 111 = -1001 = 1110 = -2010 = 2101 = -3011 = 3 100 = -4 20. Addition Clockwise (Eg. 2+3) 000 = 0 3-bits range:111 = -1001 = 1-4 to +3 only. 110 = -2101 = -3010 = 2011 = 3100 = -4 Overflow!! Adding two +ve numbers result in a -ve number 21. Subtraction Anti Clockwise 000 = 0 111 = -1110 = -2101 = -3001 = 1010 = 2011 = 3100 = -4 1 - 3; start at 1 and move anticlockwise 3 steps 22. Subtraction Anti Clockwise 000 = 0 111 = -1110 = -2101 = -3001 = 1010 = 2011 = 3100 = -4 1 - 3; start at 1 and move clockwise 5 steps. -3 is related to 5!! 23. Subtraction Anti Clockwise 000 = 0 111 = -1110 = -2101 = -3 100 = -4 5 is twos complement of 3001 = 1010 = 2011 = 3 24. Subtraction Anti Clockwise 000 = 0 111 = -1110 = -2101 = -3001 = 1010 = 2011 = 3100 = -4 Subtraction is addition!!!!, A - B = A + Twos complement(B) 25. Subtraction Anti Clockwise 000 = 0 111 = -1110 = -2101 = -3001 = 1010 = 2011 = 3100 = -4 True for other numbers - Sub by 2 = add by 6 26. Subtraction Anti Clockwise 000 = 0 111 = -1110 = -2101 = -3001 = 1010 = 2011 = 3100 = -4 -2 -3 is -2 + 5 = 3; underflow. With 3 bits -4 to 3 only. 27. Conclusions Given that the two input numbers are representable using n bits Overflow occurs only when adding two +ve numbers Underflow occurs only when adding two negative numbers. 28. Test Your Understanding Convert to binary (27)10, (CAF)16, (653)8 Convert to Decimal (56A)13 Convert to Octal, Decimal and Hexadecimal (111001110111011)2 Assuming inputs are correctly represented perform the following 3 - 2; 5 - 6; 4 + 3; -4 -3; 2 + 7; All numbers are in decimal above. Indicate Underflow and overflows 29. Answers 30. Creative Question - 3 You are given a BLACK BOX - nothing is known what is inside it - but it does the following for you Give a sequence of n real numbers and an integer k, the box will output yes or no indicating whether there is a subset of the numbers whose sum is exactly k. It will not tell you the subset. Use this black box to find the subset whose sum is k, if it exists. You are allowed to use the black box exactly n times, where n is the length of the sequence. 31. Example (1.3,.7,2.5,1.7) and k=3YesThe problem is using the above box exactly four times you Should find out the subset that adds to 3, namely (1.3,1.7) 32. Thank You