Presentation is loading. Please wait.

Presentation is loading. Please wait.

Faculty of Computer Science © 2006 CMPUT 229 Representing Information Numbers, Numbers, and Numbers.

Similar presentations


Presentation on theme: "Faculty of Computer Science © 2006 CMPUT 229 Representing Information Numbers, Numbers, and Numbers."— Presentation transcript:

1 Faculty of Computer Science © 2006 CMPUT 229 Representing Information Numbers, Numbers, and Numbers

2 © 2006 Department of Computing Science CMPUT 229 Slide’s source Yale N. Patt and Sanjay J. Patel, Introduction to Computing Systems: From bits & gates to C & Beyond, McGrawHill Press, 2001, Chapter2.

3 © 2006 Department of Computing Science CMPUT 229 Positional Number System 329923 3 2 9 9 2 3 9 2 3 9 2 3 329329 923923

4 © 2006 Department of Computing Science CMPUT 229 Positional Number System 329923 3  10 2 + 2  10 1 + 9  10 0 9  10 2 + 2  10 1 + 3  10 0 3  100 + 2  10 + 9  19  100 + 2  10 + 3  1 300 + 20 + 9 900 + 20 + 3 923 329

5 © 2006 Department of Computing Science CMPUT 229 Positional Number System 329 13 923 13 3  13 2 + 2  13 1 + 9  13 0 9  13 2 + 2  13 1 + 3  13 0 3  169 10 + 2  13 10 + 9  1 10 9  169 10 + 2  13 10 + 3  1 10 507 10 + 26 10 + 9 10 1521 10 + 26 10 + 3 10 The same positional system works with different basis: 1550 10 542 10

6 © 2006 Department of Computing Science CMPUT 229 Binary System 110 2 923 16 1  2 2 + 1  2 1 + 0  2 0 9  16 2 + 2  16 1 + 3  16 0 1  4 10 + 1  2 10 + 0  1 10 9  256 10 + 2  16 10 + 3  1 10 4 10 + 2 10 + 0 10 2304 10 + 32 10 + 3 10 In computers we are mostly interested on bases 2, 8, and 16. 2339 10 6 10

7 © 2006 Department of Computing Science CMPUT 229 Signed Integers –Problem: given 2 k distinct patterns of bits, each pattern with k bits, assign integers to the patterns in such a way that: The numbers are spread in an interval around zero without gaps. Roughly half of the patterns represent positive numbers, and half represent negative numbers. When using standard binary addition, given an integer n, the following property should hold: pattern(n+1) = pattern(n) + pattern(1)

8 © 2006 Department of Computing Science CMPUT 229 Sign-Magnitude Representation In a sign-magnitude representation we use the first bit of the pattern to indicate if it is a positive or a negative number.

9 © 2006 Department of Computing Science CMPUT 229 Sign-Magnitude Represetation What do we do with the pattern 1000?

10 © 2006 Department of Computing Science CMPUT 229 Sign-Magnitude Representation Having two patterns to represent 0 is wasteful. The sign-magnitude representation has the advantage that it is easy to read the value from the pattern. But does it have the binary arithmetic property? For instance, what is the result of pattern(-1) + pattern(1)? 1001 pattern(-1) + 0001 pattern(1) ?? PattPatel, pp. 20

11 © 2006 Department of Computing Science CMPUT 229 Sign-Magnitude Representation Having two patterns to represent 0 is wasteful. The sign-magnitude representation has the advantage that it is easy to read the value from the pattern. But does it have the arithmetic property? For instance, what is the result of pattern(-1) + pattern(1)? 1001 pattern(-1) + 0001 pattern(1) 1010 = ?? PattPatel, pp. 20

12 © 2006 Department of Computing Science CMPUT 229 Sign-Magnitude Representation Having two patterns to represent 0 is wasteful. The sign-magnitude representation has the advantage that it is easy to read the value from the pattern. But does it have the arithmetic property? For instance, what is the result of pattern(-1) + pattern(1)? 1001 pattern(-1) + 0001 pattern(1) 1010 = pattern(-2) PattPatel, pp. 20

13 © 2006 Department of Computing Science CMPUT 229 1’s-Complement Representation A negative number is represented by “flipping” all the bits of a positive number. We still have two patterns for 0. It is still easy to read a value from a given pattern. How about the arithmetic property? Suggestion: try the following -1 + 1 = ?? -0 + 1 = ?? 0 + 1 = ?? PattPatel, pp. 20

14 © 2006 Department of Computing Science CMPUT 229 2’s-Complement Representation A single pattern for 0. 1111 pattern(-1) + 0001 pattern(1) 0000 = pattern(0) It holds the arithmetic property. But the reading of a negative pattern is not trivial. PattPatel, pp. 20

15 © 2006 Department of Computing Science CMPUT 229 Binary to Decimal Conversion Problem: Given an 8-bit 2’s complement binary number: a 7 a 6 a 5 a 4 a 3 a 2 a 1 a 0 find its corresponding decimal value. Because the binary representation has 8 bits, the decimal value must be in the [-2 7 ; +(2 7 -1)] =[-128;+127] interval. PattPatel, pp. 23

16 © 2006 Department of Computing Science CMPUT 229 if (negative = true) then Binary to Decimal Conversion a 7 a 6 a 5 a 4 a 3 a 2 a 1 a 0 Solution: negative  false if (a 7 = 1) then negative  true flip all bits; compute magnitude using: PattPatel, pp. 24

17 © 2006 Department of Computing Science CMPUT 229 Binary to Decimal Conversion (Examples) Convert the 2’s complement integer 11000111 to its decimal integer value. 1. a 7 is 1, thus we make a note that this is a negative number and invert all the bits, obtaining: 00111000 2. We compute the magnitude: 3. Now we remember that it was a negative number, thus: PattPatel, pp. 24

18 © 2006 Department of Computing Science CMPUT 229 Decimal to Binary Convertion We will start with an example. What is the binary representation of 105 10 ? Our problem is to find the values of each a i Because 105 is odd, we know that a 0 = 1 Thus we can subtract 1 from both sides to obtain: PattPatel, pp. 24

19 © 2006 Department of Computing Science CMPUT 229 Decimal to Binary Convertion (cont.) Now we can divide both sides by 2 Because 52 is even, we know that a 1 = 0 a 2 = 0 a 3 = 1 PattPatel, pp. 24

20 © 2006 Department of Computing Science CMPUT 229 Decimal to Binary Convertion (cont.) a 4 = 0a 5 = 1a 6 = 1 Thus we got: a 1 = 0a 4 = 0a 5 = 1a 6 = 1a 2 = 0a 3 = 1a 0 = 1 105 10 = 01101001 2 PattPatel, pp. 25

21 © 2006 Department of Computing Science CMPUT 229 Decimal to Binary Conversion (Another Method) We can also use repeated long division: 105/2 = 52 remainder 1 52/2 = 26 remainder 0 26/2 = 13 remainder 0 13/2 = 6 remainder 1 6/2 = 3 remainder 0 3/2 = 1 remainder 1 1/2 = 0 remainder 1

22 © 2006 Department of Computing Science CMPUT 229 Decimal to Binary Conversion (Another Method) We can also use repeated long division: 105/2 = 52 remainder 1 52/2 = 26 remainder 0 26/2 = 13 remainder 0 13/2 = 6 remainder 1 6/2 = 3 remainder 0 3/2 = 1 remainder 1 1/2 = 0 remainder 1 rightmost digit 105 10 = 01101001 2

23 © 2006 Department of Computing Science CMPUT 229 Decimal to Binary Conversion (Negative Numbers) What is the binary representation of -105 10 in 8 bits? We know from the previous slide that: +105 10 =01101001 2 To obtain the binary representation of a negative number we must flip all the bits of the positive representation and add 1: 10010110 + 00000001 10010111 Thus: -105 10 =10010111 2 PattPatel, pp. 22-23

24 © 2006 Department of Computing Science CMPUT 229 Hexadecimal Numbers (base 16) If the number $FACE represents a 2’s complement binary number, what is its decimal value? In the 68K assembler, by convention, the character $ is printed in front of an hexadecimal number to indicate base 16. First we need to look up the binary representation of F, which is 1111. Therefore $FACE is a negative number, and we have to flip all the bits. PattPatel, pp. 26-27

25 © 2006 Department of Computing Science CMPUT 229 Hexadecimal Numbers (base 16) It is best to write down the binary representation of the number first: $FACE = 1111 1010 1100 1110 Now we flip all the bits and add 1: 0000 0101 0011 0001 + 0000 0000 0000 0001 0000 0101 0011 0010 = $0532 Then we convert 0x0532 from base 16 to base 10: $0532 = 0  16 3 + 5  16 2 + 3  16 1 + 2  16 0 = 0 + 5  256 + 3  16 + 2  1 = 1280 + 48 + 2 = 1330 10 $FACE = -1330 10 PattPatel, pp. 26-27

26 © 2006 Department of Computing Science CMPUT 229 Binary Arithmetic Decimal 19 + 3 22 Binary 010011 + 000011 010110 Decimal 14 - 9 5 Binary 001110 + 110111 000101 9 10 = 001001 2 -9 10 = 110111 2 PattPatel, pp. 25

27 © 2006 Department of Computing Science CMPUT 229 Overflow What happens if we try to add +9 with +11 in a 5-bit 2-complement representation? Decimal 9 + 11 20 Binary 01001 + 01011 10100 = -12 ? The result is too large to represent in 5 digits, i.e. it is larger than 01111 = +15 10. When the result is too large for the representation we say that the result has OVERFLOWed the capacity of the representation. PattPatel, pp. 27

28 © 2006 Department of Computing Science CMPUT 229 Overflow Detection What happens if we try to add +9 with +11 in a 5-bit 2-complement representation? Decimal 9 + 11 20 Binary 01001 + 01011 10100 = -12 ? We can easily detect the overflow by detecting that the addition of two positive numbers resulted in a negative result. PattPatel, pp. 28

29 © 2006 Department of Computing Science CMPUT 229 Overflow (another example) Could overflow happen when we add two negative numbers? Decimal - 12 + -6 -18 Binary 10100 + 11010 01110 = +14 ? Again we can detect overflow by detecting that we added two negative numbers and got a positive result. Could we get overflow when adding a positive and a negative number? PattPatel, pp. 28

30 © 2006 Department of Computing Science CMPUT 229 Sign-extension What is the 8-bit representation of +5 10 ? 0000 0101 What is the 16-bit representation of +5 10 ? 0000 0000 0000 0101 What is the 8-bit representation of -5 10 ? 1111 1011 What is the 8-bit representation of -5 10 ? 1111 1111 1111 1011 PattPatel, pp. 27

31 © 2006 Department of Computing Science CMPUT 229 Sign-extension What is the 8-bit representation of +5 10 ? 0000 0101 What is the 16-bit representation of +5 10 ? 0000 0000 0000 0101 What is the 8-bit representation of -5 10 ? 1111 1011 What is the 8-bit representation of -5 10 ? 1111 1111 1111 1011 To sign-extend a number to a larger representation, all we have to do is to replicate the sign bit until we obtain the new length. PattPatel, pp. 27

32 © 2006 Department of Computing Science CMPUT 229 Some Useful Numbers to Remember 2 0 = 1 10 = $0001 2 1 = 2 10 = $0002 2 2 = 4 10 = $0004 2 3 = 8 10 = $0008 2 4 = 16 10 = $0010 2 5 = 32 10 = $0020 2 6 = 64 10 = $0040 2 7 = 128 10 = $0080 2 8 = 256 10 = $0100 2 9 = 512 10 = $0200 2 10 = 1024 10 = 0100 0000 0000 = $0400 = 1K 2 20 = 2 10 × 2 10 = 1024 10 × 1024 10 = 0001 0000 0000 0000 0000 0000 = $0010 0000 = 1M 2 30 = 2 10 × 2 10 × 2 10 = 1 G 2 40 = 2 10 × 2 10 × 2 10 × 2 10 = 1 T

33 © 2006 Department of Computing Science CMPUT 229 Data and Addresses “She is 104.” “I am staying at 104.” “It costs 104.” “There is 104.” “My office is 104.” “Stop in front of 104.” “My house is 104.” In each case, how do we know what 104 is? From the context in which it is used! The same is true for data and addresses. The same number is data on one instance and address on another. “Write 100 at $0780.” “Write 100 at 100.”

34 © 2006 Department of Computing Science CMPUT 229 Representing Fractions In base 10 we know that: +0.105 10 = 105 10 /1000 10 = 105 10 /10 3 Fractions are represented in a similar fashion in base 2: +0.10101 2 = 10101 2 /100000 2 = 10101 2 /2 5 10101 2 = 21 10 and 2 5 = 32, thus 0.10101 2 = 21 10 /32 10 = 0.65625 10 Clements, pp. 152

35 © 2006 Department of Computing Science CMPUT 229 Fractions: Binary to Decimal Another way to convert a binary fraction into a decimal value is as follows: 0.10101 2 = 1  2 -1 + 0  2 -2 + 1  2 -3 + 0  2 -4 + 1  2 -5 = 1  0.5 + 0  0.25 + 1  0.125 + 0  0.0625 + 1  0.03125 = 0.5 + 0.125 + 0.03125 = 0.65625

36 © 2006 Department of Computing Science CMPUT 229 Fractions: Decimal to Binary The following method works to convert decimal factions to binary: 0.6875 10 = ?? 2 0.6875  2 = 1.3750 0.3750  2 = 0.7500 0.7500  2 = 1.5000 0.5000  2 = 1.0000 0.0000  2 Done! 0.6875 10 = 0.1011 2

37 © 2006 Department of Computing Science CMPUT 229


Download ppt "Faculty of Computer Science © 2006 CMPUT 229 Representing Information Numbers, Numbers, and Numbers."

Similar presentations


Ads by Google