Transcript
Page 1: Lec05-CS110 Computational Engineering

CS110: Models of Computing

Lecture 5V. Kamakoti

10th January 2008

Page 2: Lec05-CS110 Computational Engineering

• Number Systems and conversions• Decimal• Binary• Octal• Hexadecimal

• Representation of signed integers inBinary• Two’s complement representation

• Floating point in Binary system• IEEE 754 - later in this course

Today’s Lecture Contents

Page 3: Lec05-CS110 Computational Engineering

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 asfollows.

Page 4: Lec05-CS110 Computational Engineering

Decimal Mult and Div

• Multiply by 10 is equivalent to Left shift thedigits 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 getsout is the reminder.

• 986/10, gives 98 as quotient and 6 asreminder.

Page 5: Lec05-CS110 Computational Engineering

More interesting facts

• Multiplying a number by 10k is to left shift thenumber 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 andleaves is the reminder.

• 98657 / 104 gives 9 as quotient and 8657 asreminder.

• True for any base “b”.

Page 6: Lec05-CS110 Computational Engineering

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

Page 7: Lec05-CS110 Computational Engineering

Any base to decimal conversion

• (1345)6 = 1*63 + 3*62 + 4*61 + 6*60 = (354)10

• Systems with base k greater than 10 usesdigits (0,1,2,3,…,k-1). To avoid confusion 10is represented by A, 11 by B and so on.

• For eg. The hexadecimal (base 16) systemuses the alphabets A to F for representing the“digits” 10 to 15 in order.

Page 8: Lec05-CS110 Computational Engineering

The Binary system

• Dividing a binary number by 2k is equivalent to rightshifting 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.

Page 9: Lec05-CS110 Computational Engineering

Interesting facts• If a binary number is divided by 2, the least

significant bit is the reminder. The next toleast significant bit becomes the lsb (rightshifting).

• 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.

Page 10: Lec05-CS110 Computational Engineering

Decimal to Binary

• Repeatedly divide the given number by 2 tillthe 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

Page 11: Lec05-CS110 Computational Engineering

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.

Page 12: Lec05-CS110 Computational Engineering

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.

Page 13: Lec05-CS110 Computational Engineering

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 arestored. 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, whichis (11011)2. Since only 4 bits forrepresentation, we get (1011)2 which is 11.

Page 14: Lec05-CS110 Computational Engineering

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 bitand add one. Hence, named 2’s complement.

Page 15: Lec05-CS110 Computational Engineering

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

Page 16: Lec05-CS110 Computational Engineering

The Circle of Numbers000 = 0

001 = 1

010 = 2

011 = 3

111 = -1

110 = -2

101 = -3

100 = -4

Page 17: Lec05-CS110 Computational Engineering

Addition Clockwise (Eg. 1+2)000 = 0

001 = 1

010 = 2

011 = 3

111 = -1

110 = -2

101 = -3

100 = -4

Page 18: Lec05-CS110 Computational Engineering

Addition Clockwise (Eg. -3+2)000 = 0

001 = 1

010 = 2

011 = 3

111 = -1

110 = -2

101 = -3

100 = -4

Page 19: Lec05-CS110 Computational Engineering

Addition Clockwise (Eg. -2+3)000 = 0

001 = 1

010 = 2

011 = 3

111 = -1

110 = -2

101 = -3

100 = -4

Page 20: Lec05-CS110 Computational Engineering

Addition Clockwise (Eg. 2+3)000 = 0

001 = 1

010 = 2

011 = 3

111 = -1

110 = -2

101 = -3

100 = -4Overflow!! Adding two +ve numbers result in a -ve number

3-bits range:

-4 to +3 only.

Page 21: Lec05-CS110 Computational Engineering

Subtraction Anti Clockwise000 = 0

001 = 1

010 = 2

011 = 3

111 = -1

110 = -2

101 = -3

100 = -41 - 3; start at 1 and move anticlockwise 3 steps

Page 22: Lec05-CS110 Computational Engineering

Subtraction Anti Clockwise000 = 0

001 = 1

010 = 2

011 = 3

111 = -1

110 = -2

101 = -3

100 = -41 - 3; start at 1 and move clockwise 5 steps. -3 is related to 5!!

Page 23: Lec05-CS110 Computational Engineering

Subtraction Anti Clockwise000 = 0

001 = 1

010 = 2

011 = 3

111 = -1

110 = -2

101 = -3

100 = -45 is two’s complement of 3

Page 24: Lec05-CS110 Computational Engineering

Subtraction Anti Clockwise000 = 0

001 = 1

010 = 2

011 = 3

111 = -1

110 = -2

101 = -3

100 = -4Subtraction is addition!!!!, A - B = A + Two’s complement(B)

Page 25: Lec05-CS110 Computational Engineering

Subtraction Anti Clockwise000 = 0

001 = 1

010 = 2

011 = 3

111 = -1

110 = -2

101 = -3

100 = -4True for other numbers - Sub by 2 = add by 6

Page 26: Lec05-CS110 Computational Engineering

Subtraction Anti Clockwise000 = 0

001 = 1

010 = 2

011 = 3

111 = -1

110 = -2

101 = -3

100 = -4-2 -3 is -2 + 5 = 3; underflow. With 3 bits -4 to 3 only.

Page 27: Lec05-CS110 Computational Engineering

Conclusions

• Given that the two input numbers arerepresentable using “n” bits– Overflow occurs only when adding two +ve

numbers– Underflow occurs only when adding two

negative numbers.

Page 28: Lec05-CS110 Computational Engineering

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 representedperform the following– 3 - 2; 5 - 6; 4 + 3; -4 -3; 2 + 7;– All numbers are in decimal above. Indicate

Underflow and overflows

Page 29: Lec05-CS110 Computational Engineering

Answers

Page 30: Lec05-CS110 Computational Engineering

Creative Question - 3

• You are given a BLACK BOX - nothing isknown what is inside it - but it does thefollowing for you– Give a sequence of n real numbers and an integer

k, the box will output “yes” or “no” indicatingwhether there is a subset of the numbers whosesum 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.

Page 31: Lec05-CS110 Computational Engineering

Example

(1.3,.7,2.5,1.7) and k = 3

Yes

The problem is using the above box exactly four times you

Should find out the subset that adds to 3, namely (1.3,1.7)

Page 32: Lec05-CS110 Computational Engineering

Thank You


Top Related