2 Number Systems z Decimal z Binary z
2. Number Systems z Decimal z Binary z Addition z Headecimal z. Two’s complement z ASCII characters CMPUT 229 - Computer Organization and Architecture I 1
Positional Number System 329 3 2 9 923 9 2 3 9 9 2 2 3 3 923 329 CMPUT 229 - Computer Organization and Architecture I 2
Decimal numbers 329 923 3 102 + 2 101 + 9 100 9 102 + 2 101 + 3 100 + 2 10 + 9 1 300 + 20 + 9 9 100 + 2 10 + 3 1 900 + 20 + 3 329 923 CMPUT 229 - Computer Organization and Architecture I 3
Positional Number System The same positional system works with different basis: 32913 92313 3 132 + 2 131 + 9 130 9 132 + 2 131 + 3 130 3 169 + 2 13 + 9 1 50710 + 2610 + 910 9 169 + 2 13 + 3 1 152110 + 2610 + 310 54210 155010 CMPUT 229 - Computer Organization and Architecture I 4
Binary System In computers we are mostly interested on bases 2, 8, and 16. 1102 92316 1 22 + 1 21 + 0 20 9 162 + 2 161 + 3 160 1 4 + 1 2 + 0 1 410 + 210 + 010 9 256 + 2 16 + 3 1 230410 + 3210 + 310 610 233910 CMPUT 229 - Computer Organization and Architecture I 5
Signed Integers y. Problem: given 2 k distinct patterns of bits, each pattern with k bits, assign integers to the patterns in such a way that: x. The numbers are spread in an interval around zero without gaps. x. Roughly half of the patterns represent positive numbers, and half represent negative numbers. x. When using standard binary addition, given an integer n, the following property should hold: pattern(n+1) = pattern(n) + pattern(1) CMPUT 229 - Computer Organization and Architecture I 6
Signed Magnitude In a signed magnitude representation we use the first bit of the pattern to indicate if it is a positive or a negative number. CMPUT 229 - Computer Organization and Architecture I 7
Signed Magnitude What do we do with the pattern 1000? CMPUT 229 - Computer Organization and Architecture I 8
Signed Magnitude Having two patterns to represent 0 is wasteful. The signed 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 ? ? pattern(-1) pattern(1) CMPUT 229 - Computer Organization and Architecture I Patt. Patel pp. 9 20
Signed Magnitude Having two patterns to represent 0 is wasteful. The signed 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 - Computer Organization and Architecture I 10 20 Patt. Patel pp.
Signed Magnitude Having two patterns to represent 0 is wasteful. The signed 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 - Computer Organization and Architecture I 11 20 Patt. Patel pp.
1’s Complement 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 folllowing -1 + 1 = ? ? -0 + 1 = ? ? CMPUT 229 - Computer 0 + 1 = ? ? Organization and Architecture I 12 20 Patt. Patel pp.
2’s Complement z a representation for negative numbers y. The leftmost bit is used to indicate +/x. Positive number starts with 0, negative 1 z. A negative number is obtained by y. Convert the corresponding positive decimal number to a binary y toggle all bits ( all 1 0 and all 0 1) y. Add 1 to the binary obtained in the previous step CMPUT 229 - Computer Organization and Architecture I 13
2’s Complement 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 - Computer Organization and Architecture I 14 20 Patt. Patel pp.
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 [-27; +(27 -1)] =[-128; +127] interval. CMPUT 229 - Computer Organization and Architecture I 15 23 Patt. Patel pp.
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: if (negative = true) then CMPUT 229 - Computer Organization and Architecture I 16 24 Patt. Patel pp.
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: CMPUT 229 - Computer Organization and Architecture I 17 24 Patt. Patel pp.
Decimal to Binary Convertion We will start with an example. What is the binary representation of 10510? Our problem is to find the values of each ai Because 105 is odd, we know that a 0 = 1 Thus we can subtract 1 from both sides to obtain: CMPUT 229 - Computer Organization and Architecture I 18 24 Patt. Patel pp.
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 CMPUT 229 - Computer Organization and Architecture I 19 24 Patt. Patel pp.
Decimal to Binary Convertion (cont. ) a 4 = 0 a 5 = 1 a 6 = 1 Thus we got: a 6 = 1 a 5 = 1 a 4 = 0 a 3 = 1 a 2 = 0 a 1 = 0 a 0 = 1 10510 = 011010012 CMPUT 229 - Computer Organization and Architecture I 20 25 Patt. Patel pp.
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 - Computer Organization and Architecture I 21
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 - Computer Organization and Architecture I 22
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 Thus: -10510 =100101112 CMPUT 229 - Computer Organization and Architecture I Patt. Patel pp. 2322 -23
Hexadecimal Numbers (base 16) By convention, the characters 0 x are printed in front of an hexadecimal number to indicate base 16. If the number 0 x. 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 0 x. FACE is a negative number, and we have to flip all the bits. CMPUT 229 - Computer Organization and Architecture I Patt. Patel pp. 2426 -27
Hexadecimal Numbers (base 16) It is best to write down the binary representation of the number first: 0 x. FACE = 1111 1010 1100 1110 Now we flip all the bits and add 1: 0000 0101 0011 0001 + 0000 0001 0000 0101 0010 = 0 x 0532 Then we convert 0 x 0532 from base 16 to base 10: 0 x 0532 = 0 163 + 5 162 + 3 161 + 2 160 = 0 + 5 256 + 3 16 + 2 1 = 1280 + 48 + 2 = 133010 = -133010 CMPUT 0 x. FACE 229 - Computer Organization and Architecture I Patt. Patel pp. 2526 -27
Binary Arithmetic Decimal Binary 19 + 3 22 010011 + 000011 010110 Binary Decimal 14 - 9 5 910 = 0010012 -910 = 1101112 CMPUT 229 - Computer Organization and Architecture I 001110 + 110111 000101 26 25 Patt. Patel pp.
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 = +1510. When the result is too large for the representation we say that the result has OVERFLOWed the capacity of the representation? CMPUT 229 - Computer Organization and Architecture I 27 27 Patt. Patel pp.
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. CMPUT 229 - Computer Organization and Architecture I 28 28 Patt. Patel pp.
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? CMPUT 229 - Computer Organization and Architecture I 29 28 Patt. Patel pp.
Sign-extension What is the 8 -bit representation of +510? 0000 0101 What is the 16 -bit representation of +510? 0000 0101 What is the 8 -bit representation of -510? 1111 1011 What is the 8 -bit representation of -510? 1111 1011 CMPUT 229 - Computer Organization and Architecture I 30 27 Patt. Patel pp.
Sign-extension What is the 8 -bit representation of +510? 0000 0101 What is the 16 -bit representation of +510? 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 1011 CMPUT 229 - Computer Organization and Architecture I 31 27 Patt. Patel pp.
Storing characters z. ASCII (American Standard Code for Information Interchange) y One byte z. UTF-8 (8 -bit Unicode Transformation Format) y One to four bytes y Coincides with ASCII y Be able to represent any languages CMPUT 229 - Computer Organization and Architecture I 32
Endianess In the previous example, there are two ways to store +1910 at the address 0 x 1000 and -10510 at the address 0 x 10001004: Address 0 x 10001001 0 x 10001002 0 x 10001003 0 x 10001004 0 x 10001005 0 x 10001006 0 x 10001007 Value 0 x 00 0 x 13 0 x. FF 0 x 97 Address 0 x 10001001 0 x 10001002 0 x 10001003 0 x 10001004 0 x 10001005 0 x 10001006 0 x 10001007 Value 0 x 13 0 x 00 0 x 97 0 x. FF What is the difference? CMPUT 229 - Computer Organization and Architecture I 33
Little-End and Big-End +1910 = 0000 0000 0001 0011 Big end of +1910 Little end of +1910 (hexadecimal) +1910 = 0 x 0000 0013 -10510 = 1111 1111 1001 0111 Big end of +1910 (binary) Little end of +1910 = 0 x. FFFF FF 97 CMPUT 229 - Computer Organization and Architecture I (hexadecimal) 34
Endianess The question is: which end of the integer do we store first in memory? Big end of +1910 Address 0 x 10001001 0 x 10001002 0 x 10001003 0 x 10001004 0 x 10001005 0 x 10001006 0 x 10001007 Value 0 x 00 0 x 13 0 x. FF 0 x 97 Big Endian Byte Order Address 0 x 10001001 0 x 10001002 0 x 10001003 0 x 10001004 0 x 10001005 0 x 10001006 0 x 10001007 Little end of +1910 Value 0 x 13 0 x 00 0 x 97 0 x. FF Little Endian Byte Order DECstations and Intel 80 x 86 are little-endians. Sun SPARC and CMPUT 229 Macintosh - Computer are big-endians. Organization and Architecture I 35
Floating Number z How to represent a very large number? z How about a number of the form y 2. 5006 x 10 -8 z. Floating number y. IEEE standard CMPUT 229 - Computer Organization and Architecture I 36
IEEE 754 Floating Number S exponent 1 N = -1 8 S fraction (precision) 23 x 1. Fraction x 2 exponent – 127 CMPUT 229 - Computer Organization and Architecture I 37
Example -6 5 8 CMPUT 229 - Computer Organization and Architecture I 38
- Slides: 38