Survey
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
Faculty of Computer Science Representing Information Numbers, Numbers, and Numbers CMPUT 229 © 2006 Department of Computing Science Slide’s source Yale N. Patt and Sanjay J. Patel, Introduction to Computing Systems: From bits & gates to C & Beyond, McGrawHill Press, 2001, Chapter2. CMPUT 229 © 2006 Department of Computing Science Positional Number System 329 3 2 9 923 9 2 3 9 9 2 2 3 3 923 329 CMPUT 229 © 2006 Department of Computing Science Positional Number System 329 923 3102 + 2101 + 9100 9102 + 2101 + 3100 3100 + 210 + 91 300 + 20 + 9 9100 + 210 + 31 900 + 20 + 3 329 923 CMPUT 229 © 2006 Department of Computing Science Positional Number System The same positional system works with different basis: 32913 92313 3132 + 2131 + 9130 9132 + 2131 + 3130 316910 + 21310 + 9110 50710 + 2610 + 910 916910 + 21310 + 3110 152110 + 2610 + 310 54210 155010 CMPUT 229 © 2006 Department of Computing Science Binary System In computers we are mostly interested on bases 2, 8, and 16. 1102 92316 122 + 121 + 020 1410 + 1210 + 0110 410 + 210 + 010 610 9162 + 2161 + 3160 925610 + 21610 + 3110 230410 + 3210 + 310 233910 CMPUT 229 © 2006 Department of Computing Science Signed Integers – Problem: given 2k 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) CMPUT 229 © 2006 Department of Computing Science Sign-Magnitude Representation Pattern Value Represented Sign Magnitude 0000 0 0001 1 0010 2 0011 3 0100 4 0101 5 0110 6 0111 7 1000 1001 1010 1011 1100 1101 1110 1111 In a sign-magnitude representation we use the first bit of the pattern to indicate if it is a positive or a negative number. CMPUT 229 © 2006 Department of Computing Science Sign-Magnitude Represetation Pattern Value Represented Sign Magnitude 0000 0 0001 1 0010 2 0011 3 0100 4 0101 5 0110 6 0111 7 1000 1001 -1 1010 -2 1011 -3 1100 -4 1101 -5 1110 -6 1111 -7 What do we do with the pattern 1000? CMPUT 229 © 2006 Department of Computing Science Sign-Magnitude Representation Pattern Value Represented Sign Magnitude 0000 0 0001 1 0010 2 0011 3 0100 4 0101 5 0110 6 0111 7 1000 -0 1001 -1 1010 -2 1011 -3 1100 -4 1101 -5 1110 -6 1111 -7 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 + 0001 ?? CMPUT 229 pattern(-1) pattern(1) PattPatel, pp. 20 © 2006 Department of Computing Science Sign-Magnitude Representation Pattern Value Represented Sign Magnitude 0000 0 0001 1 0010 2 0011 3 0100 4 0101 5 0110 6 0111 7 1000 -0 1001 -1 1010 -2 1011 -3 1100 -4 1101 -5 1110 -6 1111 -7 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 = ?? CMPUT 229 PattPatel, pp. 20 © 2006 Department of Computing Science Sign-Magnitude Representation Pattern Value Represented Sign Magnitude 0000 0 0001 1 0010 2 0011 3 0100 4 0101 5 0110 6 0111 7 1000 -0 1001 -1 1010 -2 1011 -3 1100 -4 1101 -5 1110 -6 1111 -7 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) CMPUT 229 PattPatel, pp. 20 © 2006 Department of Computing Science 1’s-Complement Representation Pattern 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111 Value Represented Sign Magnitude 1’s complement 0 0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 -0 -7 -1 -6 -2 -5 -3 -4 -4 -3 -5 -2 -6 -1 -7 -0 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 = ?? CMPUT 229 PattPatel, pp. 20 © 2006 Department of Computing Science 2’s-Complement Representation Pattern 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111 Value Represented Sign Magnitude 1’s complement 2’s complement 0 0 0 1 1 1 2 2 2 3 3 3 4 4 4 5 5 5 6 6 6 7 7 7 -0 -7 -8 -1 -6 -7 -2 -5 -6 -3 -4 -5 -4 -3 -4 -5 -2 -3 -6 -1 -2 -7 -0 -1 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. CMPUT 229 PattPatel, pp. 20 © 2006 Department of Computing Science Binary to Decimal Conversion Problem: Given an 8-bit 2’s complement binary number: a7 a6 a5 a4 a3 a2 a1 a0 find its corresponding decimal value. Because the binary representation has 8 bits, the decimal value must be in the [-27; +(27-1)] =[-128;+127] interval. CMPUT 229 PattPatel, pp. 23 © 2006 Department of Computing Science Binary to Decimal Conversion a7 a6 a5 a4 a3 a2 a1 a0 Solution: negative false if (a7 = 1) then negative true flip all bits; compute magnitude using: V a6 26 a5 25 a4 2 4 a3 23 a2 2 2 a1 21 a0 20 if (negative = true) then V V 1 V V CMPUT 229 PattPatel, pp. 24 © 2006 Department of Computing Science Binary to Decimal Conversion (Examples) Convert the 2’s complement integer 11000111 to its decimal integer value. 1. a7 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: V 0 26 1 25 1 2 4 1 23 0 2 2 0 21 0 20 0 64 1 32 116 1 8 0 4 0 2 0 1 0 32 16 8 0 0 0 56 3. Now we remember that it was a negative number, thus: V 56 1 57; V 57 CMPUT 229 PattPatel, pp. 24 © 2006 Department of Computing Science Decimal to Binary Convertion We will start with an example. What is the binary representation of 10510? 105 a6 26 a5 25 a4 2 4 a3 23 a2 2 2 a1 21 a0 20 Our problem is to find the values of each ai Because 105 is odd, we know that a0 = 1 Thus we can subtract 1 from both sides to obtain: 104 a6 26 a5 25 a4 2 4 a3 23 a2 2 2 a1 21 CMPUT 229 PattPatel, pp. 24 © 2006 Department of Computing Science Decimal to Binary Convertion (cont.) 104 a6 26 a5 25 a4 2 4 a3 23 a2 2 2 a1 21 Now we can divide both sides by 2 52 a6 25 a5 2 4 a4 23 a3 2 2 a2 21 a1 20 Because 52 is even, we know that a1 = 0 52 a6 25 a5 2 4 a4 23 a3 2 2 a2 21 26 a6 2 4 a5 23 a4 2 2 a3 21 a2 20 a2 = 0 26 a6 2 4 a5 23 a4 2 2 a3 21 13 a6 23 a5 2 2 a4 21 a3 20 a3 = 1 12 a6 23 a5 2 2 a4 21 CMPUT 229 PattPatel, pp. 24 © 2006 Department of Computing Science Decimal to Binary Convertion (cont.) 12 a6 23 a5 2 2 a4 21 6 a6 2 2 a5 21 a4 20 a4 = 0 6 a6 2 2 a5 21 3 a6 21 a5 20 a5 = 1 2 a6 21 1 a6 2 0 Thus we got: a6 = 1 a5 = 1 a6 = 1 a4 = 0 a3 = 1 a2 = 0 a1 = 0 a0 = 1 10510 = 011010012 CMPUT 229 PattPatel, pp. 25 © 2006 Department of Computing Science 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 CMPUT 229 © 2006 Department of Computing Science Decimal to Binary Conversion (Another Method) We can also use repeated long division: 105/2 = 52 remainder 1 rightmost digit 52/2 = 26 remainder 0 26/2 = 13 remainder 0 13/2 = 6 remainder 1 10510 = 011010012 6/2 = 3 remainder 0 3/2 = 1 remainder 1 1/2 = 0 remainder 1 CMPUT 229 © 2006 Department of Computing Science Decimal to Binary Conversion (Negative Numbers) What is the binary representation of -10510 in 8 bits? We know from the previous slide that: +10510 =011010012 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 CMPUT 229 Thus: -10510 =100101112 PattPatel, pp. 22-23 © 2006 Department of Computing Science Hexadecimal Numbers (base 16) Binary Decimal Hexadecimal 0000 0 0 0001 1 1 0010 2 2 0011 3 3 0100 4 4 0101 5 5 0110 6 6 0111 7 7 1000 8 8 1001 9 9 1010 10 A 1011 11 B 1100 12 C 1101 13 D 1110 14 E 1111 15 F In the 68K assembler, by convention, the character $ is printed in front of an hexadecimal number to indicate base 16. If the number $FACE represents a 2’s complement binary number, what is its decimal value? 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. CMPUT 229 PattPatel, pp. 26-27 © 2006 Department of Computing Science Hexadecimal Numbers (base 16) It is best to write down the binary representation of the number first: Binary Decimal Hexadecimal 0000 0 0 0001 1 1 0010 2 2 0011 3 3 0100 4 4 0101 5 5 0110 6 6 0111 7 7 1000 8 8 1001 9 9 1010 10 A 1011 11 B 1100 12 C 1101 13 D 1110 14 E 1111 15 F $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 = 0163 + 5162 + 3161 + 2160 = 0 + 5256 + 316 + 21 = 1280 + 48 + 2 = 133010 $FACE = -133010 CMPUT 229 PattPatel, pp. 26-27 © 2006 Department of Computing Science Binary Arithmetic Decimal Binary 19 + 3 22 010011 + 000011 010110 Binary Decimal 14 - 9 5 910 = 0010012 -910 = 1101112 CMPUT 229 001110 + 110111 000101 PattPatel, pp. 25 © 2006 Department of Computing Science Overflow What happens if we try to add +9 with +11 in a 5-bit 2-complement representation? Decimal Binary 9 + 11 20 01001 + 01011 10100 = -12 ? The result is too large to represent in 5 digits, i.e. it is larger than 01111 = +1510. When the result is too large for the representation we say that the result has OVERFLOWed the capacity of the representation. CMPUT 229 PattPatel, pp. 27 © 2006 Department of Computing Science Overflow Detection What happens if we try to add +9 with +11 in a 5-bit 2-complement representation? Decimal Binary 9 + 11 20 01001 + 01011 10100 = -12 ? We can easily detect the overflow by detecting that the addition of two positive numbers resulted in a negative result. CMPUT 229 PattPatel, pp. 28 © 2006 Department of Computing Science Overflow (another example) Could overflow happen when we add two negative numbers? Decimal Binary - 12 + -6 -18 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? CMPUT 229 PattPatel, pp. 28 © 2006 Department of Computing Science Sign-extension What is the 8-bit representation of +510? 0000 0101 What is the 16-bit representation of +510? 0000 0000 0000 0101 What is the 8-bit representation of -510? 1111 1011 What is the 8-bit representation of -510? 1111 1111 1111 1011 CMPUT 229 PattPatel, pp. 27 © 2006 Department of Computing Science Sign-extension What is the 8-bit representation of +510? 0000 0101 What is the 16-bit representation of +510? 0000 0000 0000 0101 What is the 8-bit representation of -510? 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. What is the 8-bit representation of -510? 1111 1111 1111 1011 CMPUT 229 PattPatel, pp. 27 © 2006 Department of Computing Science Some Useful Numbers to Remember 20 = 110 = $0001 210 = 102410 = 0100 0000 0000 = $0400 = 1K 21 = 210 = $0002 22 = 410 = $0004 23 = 810 = $0008 220 = 210 × 210 = 102410 × 102410 = 0001 0000 0000 0000 0000 0000 = $0010 0000 = 1M 24 = 1610 = $0010 25 = 3210 = $0020 26 = 6410 = $0040 230 = 210 × 210 × 210 = 1 G 27 = 12810 = $0080 28 240 = 210 × 210 × 210 × 210 = 1 T = 25610 = $0100 29 = 51210 = $0200 CMPUT 229 © 2006 Department of Computing Science Data and Addresses “She is 104.” In each case, how do we know what 104 is? “I am staying at 104.” 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. “It costs 104.” “There is 104.” “My office is 104.” “Write 100 at $0780.” “Stop in front of 104.” “Write 100 at 100.” “My house is 104.” CMPUT 229 © 2006 Department of Computing Science Representing Fractions In base 10 we know that: +0.10510 = 10510/100010 = 10510/103 Fractions are represented in a similar fashion in base 2: +0.101012 = 101012/1000002 = 101012/25 101012 = 2110 and 25 = 32, thus 0.101012 = 2110/3210 = 0.6562510 CMPUT 229 Clements, pp. 152 © 2006 Department of Computing Science Fractions: Binary to Decimal Another way to convert a binary fraction into a decimal value is as follows: 0.101012 = 12-1 + 02-2 + 12-3 + 02-4 + 12-5 = 10.5 + 00.25 + 10.125 + 00.0625 + 10.03125 = 0.5 + 0.125 + 0.03125 = 0.65625 CMPUT 229 © 2006 Department of Computing Science Fractions: Decimal to Binary The following method works to convert decimal factions to binary: 0.687510 = ??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.687510 = 0.10112 CMPUT 229 © 2006 Department of Computing Science CMPUT 229 © 2006