Characters, Strings, and Floats CS 350: Computer Organization & Assembler Language Programming 9/6: pp.8,9; 9/28: Activity Q.6 A. Why? We need to represent textual characters in addition to numbers. Floating-point numbers separate the magnitude of a number from its significance. B. Outcomes At the end of today, you should: Know how textual characters are represented. Know why we have floating-point numbers and how they are represented. C. Characters and Strings A character is represented by a bit string. ASCII is one scheme for characters; it uses 8 bits per character. The ASCII representations of The digits 0 9 are hex 30 39 (decimal 48 57). The letters A Z are hex 41 5A (decimal 65 90) The letters a z are hex 61 7A (decimal 97 122) The space character is hex 20 (decimal 32) ASCII C strings are null-terminated: They include an extra 8 bit of zeros. Example 1: "A9z" is represented as the 4-character sequence 'A', '9', 'z', '\0'. (The character '\0' represents 8 zero bits.) As a sequence of hex digits, this is 41 39 7A 00. As a sequence of natural numbers 0 and < 256, we have 65 57 122 0. Unicode is a newer scheme that extends ASCII to use 16 bits per character. Includes symbols from other languages and from math, logic, and other fields. Strings are sequences of characters (null-terminated or length-specified). D. Non-Whole Numbers In decimal, non-whole numbers are represented in various ways Example 2: 3.75 and 3 ¾ and 3 ⁷⁵/₁₀₀ all represent the same number When using a decimal point, positions to the right of the decimal point indicate increasingly negative powers of 10: 10-1, 10-2,. Example 3: 3.75 = 3 10⁰ + 7 10-1 + 5 10-2 Dividing by 10 n shifts the decimal point n digits to the left. Example 4: 0.75 = 75 / 100, so 3.75 = 3 ⁷⁵/₁₀₀ = 3 ¾ In binary, the positions to the right of the binary point indicate negative powers of 2. CS 350: Comp Org & Asm Lang Pgmg 1 James Sasaki, 2017
Example 5: 1.011₂ = 1 2⁰ + 0 2-1 + 1 2-2 + 1 2-3 = 1 + ¹/₄ + ¹/₈ = 1 ³/₈ = 1.375₁₀ Dividing by 2 n shifts the binary point n bits left; multiplying by 2 n shifts right. Example 6: 1.011₂ = (1011/1000)₂ = (¹¹/₈)₁₀ = 1³/₈ To convert a decimal.fraction to binary, you can treat the parts to the left and the right of the binary point separately. We already know how to deal with the part to the left. For the part to the right of the binary point, repeatedly subtract negative powers of 2. Example 7: 1.375 = 1 + 0.375 = 1 + 0 0.5 + 0.375 = 1 + 0 0.5 + 1.25 + 0.125 = 1 + 0 0.5 + 1.25 + 1 0.125 = 1.011₂ A rational number has the form numerator/denominator. It s the ratio of two integers. There are a couple of techniques for converting a decimal rational number to binary. Technique 1: Convert the rational to decimal.fraction form first. Example 8: 1³/₈ = 1.375 = = 1.011₂ Technique 2: If the denominator is a power of 2, say 2 n, then you can convert the numerator to binary and shift the binary point n bits left. Example 9: 1³/₈ = (8+3)/8 = ¹¹/₈ = (1011₂)/2³ = 1.011₂ E. Scientific Notation One standard way to represent real numbers is scientific notation, as in 6.02 10 23 or ±1.1001₂ 2-4. In normalized scientific notation, we keep one (non-zero) digit to the left of the radix point (so 6.02 10 23, not 60.2 10 22 or 0.602 10 24 ). The overall sign, of course, says whether the number is positive or negative (its direction from the origin). The exponent of a scientific notation number specifies its basic magnitude (its distance from the origin). Exponents > 1 are for values greater than the radix (or less than the negative of the radix). Exponents < 1 are for values between 0 and radix (or -radix and 0). The significand or mantissa of a scientific notation number is the digit dot fraction part. To specify a number precisely, we need some number of significant digits (the significand has a required length). Example 10: For 1.011₂ 2-48, we need 4 significant bits. For 1.0110₂ 2-48 or 1.0111₂ 2-48, we need 5 significant bits. F. Floating-Point Numbers In hardware, we use floating-point numbers to represent real numbers using scientific notation where the significand and exponent have fixed maximum sizes. So a floating point number might only be an approximation to an actual real number. CS 350: Comp Org & Asm Lang Pgmg 2 James Sasaki, 2017
Nowadays, people generally use an IEEE representation for floating-point numbers. IEEE = Institute of Electrical & Electronics Engineers. For the IEEE representation, we break up a floating-point number into a sign bit S, an exponent field E, and a fraction field F. If the sign S is 1, the number is negative; if it is 0, the number is non-negative. The IEEE representation differs from normalized scientific notation in two ways: The IEEE fraction part omits the leading 1 dot. It's understood to be there but it's not written. The IEEE exponent is written using a binary offset. Floating-point numbers come in various sizes. For a 32-bit IEEE floating-point number, the exponent is 8 bits long and the fraction is 23 bits long. To get the scientific notation significand, we prepend a 1 dot to the 23-bit IEEE fraction, so we have 24 significant bits of precision. If we read the IEEE exponent field E as an unsigned integer (where 1 E < 255), then the scientific notation exponent is E 127. So we can represent exponents -126 and 127. (The cases E = 0 and 255 are treated specially; see below.) So the 32-bit string S E F represents (-1) S 1.F 2 E 127 where 1.F means 1. prepended to F. Example 12: Let N be the 32 bits 1100 0101 1011 0100 0000 0000 0000 0000. The sign field S is 1, the exponent field E is 1000 1011 = 139₁₀, and the 23-bit fraction F is 011010 (18) where the notation 0 (18) means 18 zero bits. The scientific notation exponent is E 127 = 139 127 = 12. The scientific notation fraction is 1.F, which is 1.011010 (18). So N represents -1.01101 2 12 (or -1.011010 (18) 2 12 if you want to write the trailing zeros). Example 13: In the other direction, if we are asked for the IEEE representation of -1.01101 2 12, we use 1 for the negative sign, drop the 1. and use 011010 (18) for the fraction, and add 127 to the exponent 12 to get 139 which is 1000 1011₂. This gives 1 1000 1011 011010 (18) as the IEEE representation. The IEEE representation has some special cases. If E = 0 and F = 0 (23) we have +0 or -0 depending on the overall sign. If E = 0 and F 0 (23) we have N = (-1) S 0.F 2 126. These are unnormalized scientific notation numbers, since they begin with 0; they re used for numbers that are extremely close to zero. E = 255 is used for +,, and NaN (Not a Number). G. Representation Problems With Floating-Point Numbers Our representation of floating-point numbers may fail in three ways. Two of them are magnitude problems; the third involves the number of significant bits. Overflow: The exponent is too large (i.e, too positive). For 32-bit floating-point numbers, we can have E = 254 (so the actual exponent is 127). Writing 1 (23) to mean twenty-three 1 bits, the furthest we can get from zero is ±1.1 (23) 2 127. Underflow: The exponent is too small (i.e., too negative). CS 350: Comp Org & Asm Lang Pgmg 3 James Sasaki, 2017
For 32-bit floating-point numbers, we can have E = 1 (so the scientific notation exponent is 126), so ± 1.0 (23) 2 126 is as close to zero as we can get using the standard case for IEEE floating-point numbers. Unnormalized numbers (those that begin 0. ) do let us get closer to zero. To get these numbers, we use E = 0 (and a nonzero significand). We get a scientific notation exponent of -126, so the smallest number we can represent has the IEEE representation S 0000 0000 0 (22) 1. In scientific notation, we get ±0.0 (22) 1 2 126, which equals ± 2-149. Loss of Precision: Since we only have so many bits for the fraction, we can only write numerals with a limited number of significant bits. Since we are writing values in scientific notation, magnitude and significance are different. Example 14: 1.01, 10.1, 101., 1010., 10100., and.101,.0101,.00101, all require 3 significant bits because we can write them using the form 1.01 2 N. Limiting the number of significant bits creates a limit to how close two floating-point numbers can be. Example 15: with 32-bit IEEE floating-point numbers, we can t represent any number strictly between 1.0 (22) 0 and 1.0 (22) 1 because we would need > 24 significant bits. (By the way, that these two numbers only differ by 2-23, so they re very close.) Example 16: 2 23 and 2 23 +1 are adjacent (we can t represent any number between them). We have 2 23 = 1.0 (23) 2 23 and 2 23 + 1 = 1.0 (22) 1 2 23. Note these values differ by 1, so they re much further apart than the ones in the previous example. H. Arithmetic Problems With Floating-Point Numbers Because we can only represent certain real numbers with floating-point numbers, arithmetic operations can produce inaccurate results. Overflow: Adding or multiplying two large numbers can yield a result with an exponent that s too large. Example 17: 2 127 2 should = 2 128, which isn t representable using 32-bit floating-point numbers. Underflow: Dividing two numbers can yield an exponent that s too small. Example 18: 2 126 /2 should equal 2 127, which isn t representable using 32-bit floating-point numbers. Loss of Significance: When we do arithmetic with two floating-point numbers, the result might require too many significant bits. Examples of Loss of Significance For examples of how we can lose significant digits, let s look at 5-bit floating-point numbers of the form b.bbbb 2 N (where each b stands for a bit) Example 19: If we add 1.0000 and 0.0001, the result is 1.0001 and no loss of significance has occurred. Note we really had 1.0000 2⁰ and 0.0001 2⁰, so the exponents matched. In general, we may want to add two numbers where the exponents don t match. To make the exponents match, let s raise the smaller exponent to match the larger one. To do this, we shift its bits rightward, introducing 0s on the left and dropping bits on the right. If we drop a 1 bit, we re losing a significant digit. (Shifting out bits that are 0 seems less of a problem.) CS 350: Comp Org & Asm Lang Pgmg 4 James Sasaki, 2017
Example 20: Take 1.0000 2¹ + 1.0000 2-2 First shift 1.0000 2-2 = 0.1000 2-1 = 0.0100 2⁰ = 0.0010 2¹ Note we lost the two rightmost 0 bits. Now add 1.0000 2¹ + 0.0010 2¹ = 1.0010 2¹ Example 21: 1.0000 2¹ + 1.0011 2-2. This time we shift 1.0011 2-2 to 0.0010 2¹, but we lose the two rightmost 1 bits to do it. Then we add 1.0000 2¹ + 0.0010 2¹ = 1.0010 2¹ Note in general, floating-point addition isn t associative because we may lose significance when adding numbers with dissimilar exponents. Similar truncation problems occur with other arithmetic operations. Because each operation can introduce an error, it s tricky to write algorithms that do calculations using floating-point numbers that keep the maximal number of significant digits. CS 350: Comp Org & Asm Lang Pgmg 5 James Sasaki, 2017
Illinois Institute of Technology Activity 4 Characters, Strings, and Floats CS 350: Computer Organization & Assembler Language Programming A. Why? We need to represent textual characters in addition to numbers. We use floating-point numbers to represent non-whole numbers (numbers not evenly divisible by 1). B. Outcomes After this activity, you should Be able to describe the difference between characters and their ASCII representations. Be able to represent floating-point numbers in binary and using the IEEE representation. C. Problems 1. Let '0', '1',, '9' be the ASCII representations of the digits 0 9. (In C, you can get these numbers using (int) '0', etc.) (a) What are the values of '0', '1',, '9'? Which (if any) of the following are true? (b) '2'+'3' = '5' (c) '2'+3 = '5' (d) 2+'3'= '5' (e) 2+3 = '5'. 2. Let N be an integer with 0 N 9. What are the integer values of the following: (a) '0'+ N (b) '0'+'N' (c) 'A'+ N (d) 'a'+ N (e) Which of (a) through (d) represent printable characters? What are those characters? 3. Let X represent one of the capital letters A Z. What are the integer values of: (a) X -'A' (b) X -'A'+'a'? (c) Which of (a) and (b) represent printable characters? What are those characters? 4. What decimal number does 10.011₂ represent? (There are multiple ways to write the answer.) 5. Let X = 6.4375₁₀. (a) What is the binary representation of X? (b) What is its scientific notation representation? (I.e., 1.something 2 raised to some power.) (c) What is its 32-bit IEEE representation? 6. [9/28] Let Y = 19/8 = 10011₂ / 2³ = 10.011₂. (a) What is the scientific notation representation of Y? (b) What is the 32-bit IEEE representation of Y? (c) What is the hex representation of the result from (6b)? Hint: You need 8 hex digits to represent the 32 bits. (d) [9/28] Now do the reverse of steps a c on hex FD5C0000. First rewrite it as a bitstring, then as the three fields of a 32-bit IEEE floating point number, then as a binary number in scientific notation, then as a binary number with a binary point but no power of 2, and finally as a fraction of two decimal numbers. 7. On average, the Moon is D = 238,000 miles from the Earth. (Note: 2 17 < D < 2 18.) CS 350: Comp Org & Asm Lang Pgmg 6 James Sasaki, 2017
Illinois Institute of Technology Activity 4 (a) If we represent D using the IEEE 32-bit format, what is the largest error you could expect when trying to represent distances in miles? (b) If you were launching a lunar probe, would you want to use 32-bit floating-point numbers in your navigation system? 8. Let s assume that we re doing addition using 5 significant bits and that if we need to lose precision, we truncate the offending bits drop the rightmost bits. [9/6] (a) If we add 1.1111 + 0.1110 using 5 significant digits, does truncation (of nonzero bits) occur before the addition? After the addition? (b) Repeat, for 1.1001 +.11101 (by.11101 I mean 1.1101 2 ¹). * (c) Repeat, for 1.1101 +.01100. 9. In general, is floating-point addition associative? * If we have n significant bits and n bits to the right of the binary point, we can put the binary point inside the bitstring. CS 350: Comp Org & Asm Lang Pgmg 7 James Sasaki, 2017
Illinois Institute of Technology Activity 4 Activity 4 Solution 1a. '0' = 48, '1'= 49, '9'= 57. 1b. '2'+'3' = 50 + 51 = 101 is 53 = '5' 1c. '2'+3 = 50 + 3 = 53 is = '5' 1d. 2+'3' = 2 + 53 = 55 is = '5' 1e. 2+3 = 5 is 53 = '5'. 2. Given N is an integer, 0 N 9, we have 2a. '0'+ N = 48 + N = the ASCII representation of N 2b. '0'+'N' = 48 + 78 = 126 (which happens not to = '~' ). Note: You're not expected to know 'N' = 78 or '~' = 126 for the tests. 2c. 'A'+ N is the representation of the N + 1st capital letter of the alphabet, (If N = 0 then 'A'+ N = 'A', if N = 1, then 'A'+ N = 'B', etc.) 2d. 'a'+ N is the representation of the N + 1st lowercase letter of the alphabet. (If N = 0 then 'a'+ N = 'a', if N = 1, then 'a'+ N = 'b', etc.) 2e. (See answers above.) 3a. If X is the nth letter of the uppercase alphabet (1 n 26) then X = 'A' + n 1, so X 'A' = n 1. (If X = 'A', then X 'A' = 0; if X = 'B', then X 'A' = 1; ; if X = 'Z', then X 'A' = 25.) 3b. (X -'A') +'a' = n 1 + 'a'. This sum is the lowercase version of the uppercase X. 4. 10.011₂ = 2 +.011₂ = 2 + (11₂/100₂) = 2 ³/₈. Also, 2 +.011₂ = 2 +.25 +.125 = 2.375 (which = 2 ³/₈, of course). 5a. 6.4375₁₀ = 6 + 0.25 +.125 +.0625 = 110.0111₂. 5b. 110.0111₂ = 1.100111₂ 2² 5c. We need a sign of 0 (for 0), an exponent field of 127 + actual exponent = 127 + 2 = 129 = 1000 0001, and a fraction of 1.100111 less the leading 1 bit. The result is 0 1000 0001 100111 0 (17) where 0 (17) means 17 zeros. 6a. 10.011₂ = 1.0011₂ 2¹. 6b. We need a sign of 0, exponent field = 127 + 1 = 128 = 1000 0000, and a fraction field of 0011 plus 19 zeros: 0 1000 0000 0011 0 (19). 6c. The hex representation of this is 0100 0000 0001 1000 0 (16) = 4018 0000₁₆. 6d. Hex FD5C0000 = bitstring 1111 1101 0101 1100 0000 0000 0000 0000 = 1 1111 1010 101110^(18) Repackaging into 1-bit, 8-bit, and 23-bit fields = Sign = 1, exponent field = 1111 1010₂ = 124₁₀, fraction field = 101110 = negative 1.101110 2 to power (124-127) Add 1. to fraction, offset exponent by 127 = -1.101110 2 ³ In binary scientific notation = -0.001101110 In binary (no power of 2) = -110111₂ / 2⁸ Binary integer divided by power of 2 = -55 / 256 Decimal fraction CS 350: Comp Org & Asm Lang Pgmg 8 James Sasaki, 2017
Illinois Institute of Technology Activity 4 7a. Since 2 17 < D < 2 18, we need 18 significant bits to represent D. Since the 32-bit representation has 24 significant bits, we have 6 bits left to specify parts of a mile. The maximum error would be half of (1 mile / 2⁶) = about 41 feet. 7b. Could be problematic: If we issue a command like Fire the retro-rockets when you re X miles away from the Earth, we might fire the rockets 41 feet too high (or too low). 8a. We don t need to drop any bits to align the binary points of 1.1111 and 0.1110 (so no truncation), but we have to convert the result 10.1101 10.110 because we only have 5 significant digits, so we truncate the rightmost bit, a 1. [9/6] 8b. 1.1001 +.11101 = 1.1001 2⁰ + 1.1101 2-1 = 1.1001 2⁰ + 0.1110 2⁰ (we lost a 1 bit) = 10.0111 2⁰ = 1.0011 2¹ (no truncation). 8c. [9/6]: First we take 1.1101 +.01100 and align the binary points. We want 1.1101 2⁰ + 0.01100 2⁰, but we have to drop the rightmost bit of 0.01100 (i.e., a 0) to get 5 bits, so we have 1.1101 + 0.0110. Adding the two values together, we want 10.0011 as the sum but again we must drop the rightmost bit to get 5 bits. This time the rightmost bit is a 1, so we lose information and get 10.001, (= 1.0001 2¹ in scientific notation). 9. No, in general floating-point addition (and arithmetic in general) isn t associative. (Given a choice, it s better to add two numbers of the same magnitude than two numbers with wildly different magnitudes.) CS 350: Comp Org & Asm Lang Pgmg 9 James Sasaki, 2017