Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Basic Data Types Page 1 Basic Data Types Chapter 2 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types Page 2 What you MUST know before we start: (Remember: The topics in this course build on each other) • What a bit is • How a bit corresponds to computer architecture • How combinations of bits can be used to store information • How to calculate how much information a given number of bits yields • How to calculate how many bits we need to store information • What a byte is and why it is 8-bits • What ASCII is Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types Page 3 Adding in Binary There are only four (4) possible combinations: 0 0 1 1 + 0 + 1 + 0 + 1 0 1 1 10 Remember: Since binary contains the only the digits 0 & 1, the number 2 must be represented using 2 digits (1 and 0) There is no symbol 2 any more than there is the symbol 10 in decimal Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types Page 4 Adding in Binary You’re Bonkers, Mate!! Of course there is a symbol 10 in decimal !! No – The decimal (base 10) system only has the (10) symbols: 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9 • • • • The number 10 consists of the symbols 1 and 0 The number 36 consists of the symbols 3 and 6 The number 754 consists of the symbols 7, 5 and 6 The number 8,925 consists of the symbols 8, 9, 2 and 5 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types Page 5 Adding in Binary Some additional Examples of adding in Binary: 1 1 1 1 101 11 1 1 1 + 10 + 10 + 1 0 1 1 1 1 0 1 1 0 0 1 1 1 1 1 1 1 1 1 1 0 1 0 1 1 + 1 0 0 1 1 0 + 1 0 0 1 1 0 1 OK – This last one is more than you really have to know for this course – But if you look at it, it is really easy! 1 0 1 0 1 1 1 1 0 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types Page 6 Characters vs. Numeric Values From the ASCII Tables, we know: 0110011 is the sequence of bits needed to represent ‘3’ 0110100 is the sequence of bits needed to represent ‘4’ Therefore: 0110011 + 0110100 1100111 Must be the sequence of bits needed to represent ‘7’ According to the ASCII Tables 1100111 is the sequence of bits needed to represent the character ‘g’. What Gives?? Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types Page 7 Characters vs. Numeric Values ASCII is a coding to scheme used to represent ONLY CHARACTERS AND/OR SYMBOLS. The character ‘3’ is no more a number than it is in the string “TJF-3H9” (License Plate Number??). Trying to add the characters ‘3’ and ‘4’ makes no more sense than trying to add the strings : How Now Brown Cow? + To be or not to be. Yielding: Good Golly, Miss Molly Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types Page 8 Characters vs. Numeric Values Does that mean the computer really doesn’t work with numbers, only symbols?? NO - The computer works ONLY with numeric values: The character ‘3’ is actually the numeric value: The character ‘4’ is actually the numeric value: 51 52 The character ‘g’ is actually the numeric value: 103 Check your ASCII Tables Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types Page 9 Characters vs. Numeric Values Why are there differences?? Why isn’t the character ‘3’ stored the same way as the number 3??? In order to draw the distinction between Numbers and Characters. How would the numbers 3 and 4 be stored? Basically, in the same manner in which we stored them when we first started talking about bits. Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types Page 10 Numeric Values Remember when we first talked about the different combinations of ‘on’ and ‘off’ positions for a given number of light switches: With 3-bits, there are 8 possible combinations: 000 100 001 101 010 110 011 111 And, with 4-bits, there are 16 possible combinations: 0000 0100 1000 1100 0001 0101 1001 1101 0010 0110 1010 1110 0011 0111 1100 1111 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types Page 11 Numeric Values The same sequencing pattern is used to represent numeric values Decimal Binary Decimal Binary 0 1 2 3 4 0 1 10 11 100 10 11 12 13 14 1010 1011 1100 1101 1110 20 21 22 23 24 10100 10101 10110 10111 11000 5 6 7 101 110 111 15 16 17 1111 10000 10001 25 26 27 11001 11010 11011 8 9 1000 1001 18 19 10010 10011 28 29 11100 11101 Data Structures in C for Non-Computer Science Majors Decimal Binary Kirs and Pflughoeft Basic Data Types Page 12 Numeric Conversions Is there any way of converting from decimal to binary?? Yes -- The procedure is similar to one which we use all the time. Suppose you were asked to convert 5,752 seconds to Hours, Minutes, and Seconds. How would you do it???? Because there are 60 seconds to each minute: Because there are 60 minutes to each hour: 95 60 5752 5700 52 60 1 95 60 35 95 Minutes Total 52 Seconds 1 Hour Total 35 Minutes So 5,752 Seconds = 1 hour, 35 minutes, and 52 seconds Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types Page 13 Numeric Conversions In the last case, we were (sort-of; not really) working in base 60 (since 1 hr = 50 mins. And 1 Min. = 60 Seconds) Our Check would be to perform the opposite operations: 1 Hour * 60 = 60 35 95 * 60 5,700 + 52 5,752 Minutes Minutes Minutes Seconds Seconds Seconds Seconds Data Structures in C for Non-Computer Science Majors Number Hours Number Minutes Total Minutes Secs. Per Minute Number Seconds Total Seconds Kirs and Pflughoeft Basic Data Types Page 14 Numeric Conversions Example #2: Convert 575 In. to Yds, Ft, and In. Because there 12 Inches in a Foot: 47 12 575 564 11 Total Feet No. Inches 15 No. Yards 3 47 45 2 No. Feet And so there are 15 yards, 2 feet, and 11 inches in 575 inches. Because there 3 Feet in a Yard: Check: 3 * 15 = 45 + 2 47 * 12 = 564 + 11 = 575 Inches Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types Page 15 What does this have to do with converting from decimal to binary?? Instead of dividing by 60 • for hours to minutes or minutes to seconds OR by 12 • for inches to feet OR by 3 • for feet to yards We divide by 2 • for decimal to binary AND keep track of the remainders. Reconstructing the number from LAST to FIRST Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types Page 16 Numeric Conversions Take, for example, the number 1110 (which we know is 10112) • FIRST, Divide 11 by 2: • NEXT, Let the quotient become the new Dividend • CONTINUE Until the Quotient is Zero (0) • FINALLY, Collect the remainders from Last to First 5 2 11 10 1 2 2 5 4 1 1 0 1 12 Data Structures in C for Non-Computer Science Majors 1 2 2 2 0 = 11 0 2 1 0 1 (Check the Table) Kirs and Pflughoeft Basic Data Types Page 17 Example 2: Convert 2810 to binary (= 111002): (The notation used varies slightly to save space) Quotient Remainder (If Even = 0; If Odd = 1) 2 28 0 2 14 0 Collecting Remainders from last to first: The Binary Value is: 2 7 1 1 1 1 0 02 2 3 1 2 1 1 The New Quotient is Zero: Quit Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types Page 18 Example 3: Convert 4410 to binary: The notation used differs to correspond to c syntax: / Integer Division: The result is the quotient % Modulus Arithmetic: The Result is the Remainder Collecting from Last to First: 44/2 22/2 11/2 5/2 2/2 1/2 = 22 = 11 = 5 = 2 = 1 = 0 44 22 11 5 2 1 % % % % % % 2 2 2 2 2 2 = = = = = = 0 0 1 1 0 1 1 0 1 1 0 02 Since the New Quotient is 0: STOP Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types Page 19 How do I know that 1011002 is really 4410?? It is not on my table. We Need to be able to convert from Binary to Decimal as well as from Decimal to Binary. How?? Once again, following steps which are familiar to us. Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types Page 20 Consider the number 3,46710 We could have written it as: 3,000 + 400 + 60 + 7 OR 3 * 1000 + 4 * 100 + 6 * 10 + 7 * 1 OR 3 * 103 + 4 * 102 + 6 * 101 + 7 * 100 This last notation gives us some clue to how we might go about converting from binary to decimal Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types Page 21 HOW??? Any Number, in any base, can be written in the same manner For example, the binary number 101112 (which we know is 2310) could be rewritten as: 1 * 24 + 0 * 23 + 1 * 22 + 1 * 21 + 1 * 20 = 1 * 16 + 0 * 8 + 1 * 4 + 1 * 2 + 1 * 1 = 16 + 0 + 4 + 2 + 1 = 23 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types Page 22 Remember how we laid out our decimal number (3,46710): Exponent Position: 3 2 1 0 Number: 3 4 6 7 Written as: 3 * 103 + 4 * 102 + 6 * 101 + 7 * 100 Rewritten as: 3 * 1000 + 4 * 100 + 6 * 10 + 7 * 1 Valued at: 3,000 + 400 + 60 + 7 = 3,467 The same holds true for our binary number (101112): Exponent Position: 4 3 2 1 0 Number: 1 0 1 1 1 Written as: 1 * 2 4 + 0 * 2 3 + 1 * 2 2 + 1 * 2 1 + 1 * 20 Rewritten as: 1 * 16 + 0 * 8 + 1 * 4 + 1 * 2 + 1 * 1 Valued at: 16 + 0 Data Structures in C for Non-Computer Science Majors + 4 +2 + 1 = 23 Kirs and Pflughoeft Basic Data Types Page 23 In Binary, our calculations are actually simplified since: 1 * Any Number = That Number AND 0 * Any Number = 0 Therefore: Exponent Position: Number: 4 3 2 1 0 1 0 1 1 1 Written as: 24 + 22 + 21 + 20 Rewritten as: 16 + 4 + + 1 Valued at: 23 2 (There are other ways of converting from binary to Decimal. Check the on-line Tutorial Page for a different one.) Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types Page 24 Another Example: Convert 10710 to Binary and Check it: 2 107 1 2 53 1 Check: 2 26 0 Position: 6 5 Number: 1 1 Value: 26 + 25 = 64 + 32 2 13 1 2 6 0 2 3 1 2 1 1 The binary 10710 of is: 1 1 0 1 0 1 12 4 0 3 1 + 23 + 8 2 0 1 0 1 1 + 21 + 20 + 2 + 1 = 107 Since the new quotient is 0: STOP & Collect Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types Page 25 Note that some binary numbers are easy and don’t conversion: E.g., 710 = 1112 102310 = 11111111112 810 = 10002 102410 = 100000000002 How Do we Know that??? • Given 3 bits, we know that 23 = 8, meaning that we can represent the numbers from 0 to 7 710 MUST equal 1112 810 MUST be one more than 1112 or 10002 • Given 10 bits, we know that 210 = 1,024, meaning that we can represent the numbers from 0 to 1,023 102310 MUST equal 11111111112 102410 MUST be one more than or 100000000002 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types Page 26 Note that the methods we used to convert from decimal to binary, and back again, work for any base. If, for example we wished to convert 9210 to Octal (base 8): Remainder 8 92 4 8 11 3 8 1 1 0 9210 = 1 3 4 8 Check: Position: 2 1 0 Number: 1 3 4 Value: 1* 82 + 3 * 81 + 4 * 80 = 1* 64 + 3 * 8 + 4 * 1 = 64 + 24 + 4 = 92 Since the new quotient is 0, collect from last to first Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types Page 27 Why would I care about octal (base 8) ?? As it turns out, Octal AND Hexadecimal (base 16) are often used because they allow for easy conversion to and from binary. Why is Octal easy ?? Since Octal consists ONLY of the digits 0 through 7, ANY octal digit can be represented using only 3 bits: Octal Binary 0 1 2 3 Octal Binary 000 001 010 4 5 6 100 101 110 011 7 111 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types Page 28 Why is that easy? Consider the binary number 10111002 (which is 9210 and 1348 - see the previous example) 1011100 = 1 011 1 3 100 4 (Check against Table) Notice that there is a direct transference from binary to octal and octal to binary. Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types Page 29 Example 2: Consider the number 45710 2 457 1 1110010012 2 228 0 In Octal ?? 2 114 0 2 57 = 45710 111 001 001 1 7 1 1 2 28 0 Sure?? 2 14 0 2 7 1 = 2 3 1 = 448 2 1 1 = = 7118 7118 = 7 * 82 + 1 * 81 + 1 * 80 7 * 64 + 1 * 8 + 8 + 1*1 + 1 457 Stop and Collect Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types Page 30 What about Hexadecimal (Base 16) ?? Basically, Hex is used for the same reason: It is easy to convert Since 24 = 16 pieces of information, we can represent all the digits between 0 and 15: Digit Binary 0 1 2 3 4 5 6 7 0000 0001 0010 0011 0100 0101 0110 0111 Data Structures in C for Non-Computer Science Majors Digit Binary 8 9 10 11 12 13 14 15 1000 1001 1010 1011 1100 1101 1110 1111 Kirs and Pflughoeft Basic Data Types Page 31 But 10, 11, 12, 13, 14, and 15 are NOT digits. They are Combinations of digits. True. We need to substitute the symbols: A = 10 B = 11 C = 12 D = 13 E = 14 F = 15 The Conversion Table Should be: Digit 0 1 2 3 4 5 6 7 Binary 0000 0001 0010 0011 0100 0101 0110 0111 Data Structures in C for Non-Computer Science Majors Digit 8 9 A B C D E F Binary 1000 1001 1010 1011 1100 1101 1110 1111 Kirs and Pflughoeft Basic Data Types Page 32 Once again, consider the binary number 10111002 = 9210 = 1348 - see the prior example What is the Hexadecimal Value ?? 101 1100 5 C = 5C16 Sure??? 5C16 = = = = 5 * 161 + 5 * 161 + 5 * 16 + 80 + Data Structures in C for Non-Computer Science Majors C * 160 12 * 160 12 * 1 12 = 92 Kirs and Pflughoeft Basic Data Types Page 33 Example 2: 2 486 0 2 243 1 2 121 1 2 60 2 30 Consider the number 48610 1111001102 = 48610 In Hexadecimal ?? 1 1110 0110 0 1 E 6 0 Sure?? 2 15 1 2 7 1 2 3 1 2 1 1 1E616 = 1 * 162 = 1 * 162 = 1 * 256 = 256 = = 1E616 + E * 161 + 14 * 161 + 14 * 16 + 224 + 6 * 160 + 6 * 160 + 6*1 + 6 486 Stop and Collect Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types Page 34 What about Converting from Octal to Hexadecimal?? The easiest way is to use binary numbers: 578 = 5 7 1 0 1 1 1 1 2 Sure??? F = 2F16 1011112 = 25 + 23 + 22 + 21 + 20 = 32 + 8 + 4 + 2 + 1 = 4710 Octal Hexadecimal 2F 8 47 7 16 47 15 = F 8 5 5 16 2 2 57 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types Page 35 And Now …. Back to Characters. So far, we know the following: • A character is requires 8-bits (1-byte) of storage • The standard ASCII character set consists of 128 characters (7-bits needed) • The Extended ASCII consists of 256 Characters (8-bits) • Characters are, in fact, stored as Numbers: The character ‘0’ is stored as the numeric value 48 The character ‘a’ is stored as the numeric value 97 The character ‘A’ is stored as the numeric value 65 The character ‘ ‘ (space) is stored as the numeric value 32 The character ESC (escape) is stored as the numeric value 27 The character BEL (bell) is stored as the numeric value 7 The character BS (backspace) is stored as the numeric value 8 The character CR (return) is stored as the numeric value 13 The character NUL (null) is stored as the numeric value 0 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types Page 36 In C, when we make the declaration: char a; We are: • Requesting that 1-byte of RAM be allocated (by using the reserved word char) • Indicating that whenever we use the variable a, we are actually referring to a specific location (Address) in memory (RAM) (The address will be determined at run-time) If we were to make the declaration: char a = ‘A’; We would also be requesting: • That the numeric value 65 (10000012) be stored at location a Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types Page 37 We could have also made the initialization: char a = 65; Which would have exactly the same effect as: char a = ‘A’; Within the program (after initialization) we could have made the statement(s): a = ‘m’; OR a = 109; Both of which (might) store the numeric value 109 in location a Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types Page 38 How would the value 65 or ‘A’ actually be stored ??? ALL characters are stored on 8-bits • If the value does not require 8-bits, we need to add leading zeros In this case: ‘A’ = 6510 = 1000001 = 01000001 Or: (On 8-bits) 0 1 0 0 0 0 0 1 • Which is how we would set the micro-switches at location a in RAM Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types Page 39 But, if we request only 1-byte of storage doesn’t that mean that we can only store the numbers 0 through 255 (since 28 = 256 pieces of information)?? Basically, Yes. Then how can we store larger numbers?? We need to increase the number of bits. By how many?? Since RAM is accessed in groups of 8-bits (1-byte), it would make sense to add-on 8-bits and store a number on 16-bits (2-bytes) Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types Page 40 CAVEAT STUDENTI !!!! A few years ago, everything I am about to tell you was true ---- So, what – You’re going to lie to us now ??? Yes and No -- Let’s just call it “Simplification” Isn’t that what all the U.S. Presidents said when they tried to explain the war in Vietnam to the voters ??? Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types Page 41 The point is, that on PCs, until recently: • A character required 8-bits (1-byte) of storage (it still does – Although we know that Unicode will be here soon!) • An integer required double that or 16-bits (2-bytes) of storage (On Mainframes, Integers always required 32-bits of storage!) Now, MS assumes 32-bit Applications: • An integer now requires twice as much storage as it used to , or 32-bits (4-bytes). So, what does that have to do with your intense desire to lie to us ??? Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types Page 42 All of the slides are based on a 16-bit integer Oh, come !!! You can’t be that lazy !!! Change them !!! True, but if I do things will get a little sticky: • Instead of talking about integers falling in the range -32,768 to +32,767, we will now have to integers falling in the range -2,147,483,648 to +2,147,483,647 (If this makes no sense at all, don’t worry about. It will!) • MOST IMPORTANT, the diagrams will get really messy (To accurately draw what it happening will require twice as much space!) Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types Page 43 Let me give you a quick example: • I will (guaranteed) give you an integer, say 217, and ask you to show me how it is stored in RAM. • In terms of what you have to do, the first part hasn’t changed: Convert 217 to its binary equivalent: 2 217 1 11011001 2 108 0 (By the way, did I mention that 2 54 0 calculators will NOT be allowed in 2 27 1 the first Quiz ???) 2 13 1 2 6 0 2 3 1 And then collect the remainders 2 1 1 in reverse order Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types Page 44 Mapping it into memory is straightforward: • If we were dealing with 16-bit integers, the mapping would be: 00000000 11011001 (Once again, if this makes no sense, don’t worry about. It will!) • Since an integer is indeed 32-bits long, the true mapping would be: 00000000 00000000 00000000 11011001 • The Problem, of course, is that while the concepts applied to 16-bit and 32-bit integers are IDENTICAL, the ease of illustrating them is not. (Its obvious that my slides are already too crowded!) Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types Page 45 So, what are we going to do, Obi Wan?? We are all going to take a Vow: (Repeat after me) “We, the members of CIS3355, do solemnly swear, that even though we all fully understand that integers on PCs are now stored on 32-bits, for the purposes of illustration, we will pretend that they are still stored on 16bits” Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types Page 46 OK – So now we understand that Integers on the PC are always stored on 32-bits and not 16! Well, that’s not true either --You mean you’re Lying about Lying ??? You see, it depends on the software application, or in our case, the compiler • Many of the older software applications still assume a 16bit integer … STOP!! I’m already confused enough !! Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types Page 47 Given 16-bits (2-bytes) we could store 216 = 65,536 pieces of information, or the integers 0 through 65,535. How would this be stored?? The number 0 would be stored as: 00000000 00000000 2 locations in memory The number 65,535 would be stored as: 11111111 11111111 2 locations in memory A number such as 12,316 would be stored as: 00110000 00011100 Data Structures in C for Non-Computer Science Majors 2 locations in memory Kirs and Pflughoeft Basic Data Types Page 48 Given 32-bits (4-bytes) we could store 232 = 4,294,967,296 pieces of information, or the integers 0 through 4,294,967,295 The number 0 would be stored as: 4 locations in memory 00000000 00000000 00000000 00000000 The number 4,294,967,295 would be stored as: 11111111 11111111 11111111 11111111 4 locations in memory A number such as 12,316 would be stored as: 00000000 00000000 00110000 00011100 Data Structures in C for Non-Computer Science Majors 4 locations in memory Kirs and Pflughoeft Basic Data Types Page 49 In C/C++ we would use the command: unsigned int number; To reserve 16-bits (2-bytes) of RAM at location number unsigned int number = 12316; Would also store the value In location number in RAM 00110000 00011100 Or we could enter the command: number = 796; To store the value 00000011 00011100 at location number in RAM AFTER the initial declaration Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types Page 50 Why is the integer variable number declared as unsigned ??? It means that the values stored in location number are assumed to be non-negative (i.e., 0 (zero) or greater). Must ALL integers must be non-negative??? No -- in fact, by default, integers are generally signed, meaning they can be negative OR non-negative. How can we tell if an integer is negative or nonnegative??? Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types Page 51 An integer MUST be either negative OR non-negative A BINARY CONDITION!! Therefore, we need only 1-bit (of the 16-bits or 2-bytes available to us) to indicate if the integer is negative or not. If we place a 0 (zero) in the first (left-most) bit : 0 ------- -------- The remaining (15) bits will be interpreted as non-negative If we place a 1 (one) in the first (left-most) bit : 1 ------- -------- The remaining (15) bits will be interpreted as negative Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types Page 52 But if we use one of our 16-bits for the sign, won’t that reduce the magnitude of the numbers we can represent??? Yes: • Given 15-bits, we have 215 = 32,768 pieces of information • Because we wish to include 0 (zero) in our set of integers The range of integers we can represent is: - 215 to +215 - 1 OR -32,768 to +32,767 But of course in reality: - 231 to +231 – 1 = -2,147,483,648 to 2,147,483,647 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types Page 53 int signednumber; The declaration: signed int signednumber; Or the declaration: Would reserve 16-bits (2-bytes) of RAM at location signednumber int signednumber = 5789; 0 0010110 Would store the value 01101011 at location signednumber in RAM signednumber = 796; And the command: Would store the value 0 0000011 00011100 at location signednumber in RAM IF the variable had been declared Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types Page 54 Lest we forget that we have been pretending, let’s repeat the previous example using the REAL (32bit) representation: int signednumber = 5789; and initialize it as: The command: Would reserve 32-bits (4bytes) of storage at location signednumber 0 0000000 00000000 0010110 01101011 signednumber = 796; 0 0000000 00000000 Would store the value at location signednumber in RAM 0000011 00011100 IFF the variable signednumber had previously been declared Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types Page 55 OK, OK -- I get the point !! at least as far as MS/Visual C++ is concerned, there are no more 16-bit integers. Right ??? No, There is something called a Short: The declaration: short signednumber; signed short signednumber; Or the declaration: Would reserve 16-bits (2-bytes) of RAM at location signednumber short signednumber = 5789; 0 0010110 01101011 And the command: Would store the value: at location signednumber in RAM signednumber = 796; Stores the value 0 0000011 00011100 At location signednumber You know, You’re really starting to annoy me !! Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types Page 56 A quick aside about signed and unsigned integers -----As we have seen, characters are integers which are stored on 8-bits (1-byte). By default, they are SIGNED. char acharacter = -107; Entering the declaration: is legal, although unexpected things may happen. unsigned char acharacter = 87; The declaration: May be entered without any complications More on this later. Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types Page 57 Well tell me, Oh Master of Confusion, what happens if we DO need larger Integers ??? There is a data type called long: long lnumber = 1073741824; Back in the old days, when an int was 16-bits (and the value used above was illegal), a long was 32-bits (as are ints now) the value entered above was legal (as it is now for an int). HA!! I see where you are going !! Now that integers are 32-bits, I’ll bet that longs are now stored on 64-bits, Right ??? No -- They are still stored on 32-bits (4-bytes) on PCs (Although they are stored on 64-bits on Mainframes) When is the last day to drop this course ??? Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types Page 58 Adding Integers (data type int – 16-bits) As we already noted, IF the left-most bit is 0, the integer is non-negative, and IF the left-most bit is 1, the integer is negative. Therefore, if we add the number +8: 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 And the number -8: 1 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 We should get 0 (zero). Right? Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types Page 59 Let’s Try it: 1 8 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 + -8 1 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 Which Translates to: - 16 8 Plus -8 Equals -16 ??? What Gives??? Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types Page 60 In Binary, the negative of a number is the opposite of that number. The opposite of the number 8: 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 Would be: 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 The process is called Complimenting Now, if we add the two numbers (8 + -8) together: Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types Page 61 8 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 + -8 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 Notice that the left-most bit is negative, meaning the number is negative Negative??? 8 plus -8 Yields a negative number??? On some machines, that is possible. Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types Page 62 IF the left-most bit is negative, then to determine the value, the remaining 15-bits must be complimented in order to determine the true value: Number: Compliment: 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Meaning that the actual value is -0 Negative Zero??? As noted before, on some machines, this is possible. This process is called One’s Compliment. Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types Page 63 Because in one’s compliment machines we can have a -0 (negative zero): • Iff the result turns out to be non-negative (The left-most bit is ‘0’) • We must add 1 to the result (We can not have a non-negative zero) Consider the following example: 8-7 8 = 0000000000001000 7 = 0000000000000111 (on 16-bits) (on 16-bits) -7 = 1111111111111000 (one’s compliment) Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types Page 64 Now adding 8 + (-7): 1111111111111 8 = 0000000000001000 -7 = 1111111111111000 0000000000000000 Which of course evaluates to 0 (zero) (we know the answer is 1) Therefore, we need to add 1 to get the correct answer 0000000000000000 +0000000000000001 0000000000000001 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types Page 65 If, however, the result is negative, we need not add 1 Consider the following example: 7 = 0000000000000111 7-8 8 = 0000000000001000 -8 = 1111111111110111 Then: 111 7 = 0000000000000111 -8 = 1111111111110111 1111111111111110 000000000000001 Since the result is negative, we must compliment Which evaluates to -1 (The correct answer) Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types Page 66 Must we always have a negative zero??? No - we can eliminate negative zeros by using a procedure called twos compliment With twos compliment, we perform the same procedure but after complimenting, we add 1 (one) to the result. Number 8: 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 1 1 1 Compliment: 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 Add 1: + 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types Page 67 Now, if we add the two values together: 1 1 1 1 1 1 1 1 1 1 1 1 1 8: 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 + - 8: 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 The Overflow is dropped The Value is 0 (Zero) Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types Page 68 Let’s Take Another Example: 1011100 Add: 92 + -79 1001111 9210 = 2 92 0 7910 = 2 79 1 2 46 0 2 39 1 2 23 1 2 19 1 2 11 1 2 9 1 2 5 1 2 4 0 2 2 0 2 2 0 2 1 1 2 1 1 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types Page 69 Since we wish to store the number -79 as a two’s compliment value (on 16-bits), we must compliment AND then Add 1: 79 = 0 0 0 0 0 0 0 0 0 1 0 0 1 1 1 1 1’s Compliment: 1 1 1 1 1 1 1 1 1 0 1 1 0 0 0 0 Add 1: 2’s Compliment: + 1 1 1 1 1 1 1 1 1 1 0 1 1 0 0 0 1 Is this really how the value -79 will be stored in RAM ??? YES !!! Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types Page 70 NOW we can add the values 92 + -79: 1 92 = + -79 = 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 1 0 1 1 1 0 0 1 1 1 1 1 1 1 1 1 0 1 1 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 1 Carry-Over is Dropped Since the sign is positive, evaluate the remaining bits: = 23 + 22 + 20 = 8 + 4 + 1 = 13 Which we know is correct Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types Page 71 One Final Example: Add: -92 + 79 We Already know that 7910 = 0 0 0 0 0 0 0 0 0 1 0 0 1 1 1 1 But to get -92 we must again compliment +92 and add 1: 92 = 1’s Compliment: 0 0 0 0 0 0 0 0 0 1 0 1 1 1 0 0 1 1 1 1 1 1 1 1 1 1 0 1 0 0 0 1 1 Add 1: 2’s Compliment: 1 + 1 1 1 1 1 1 1 1 1 1 0 1 0 0 1 0 0 Which is how we would store -92 in RAM (16-bits) Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types Page 72 NOW we can add the values -92 + 79: 1 -92 = + 79 = 1 1 1 1 1 1 1 1 1 1 0 1 0 0 1 0 0 0 0 0 0 0 0 0 0 0 1 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 1 1 The result is negative, Therefore we must Compliment 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 = -(23 + 22) = -(8 + 4) = -12 BUT The correct answer -13 ???? Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types Page 73 We forgot one Step: Because we are storing the value using two’s compliment, we MUST again add 1 (one) to the complimented (1’s compliment) number 1’s Compliment: 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 Add 1: + 1 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 1 Which evaluates to: = -(23 + 22 + 20) = -(8 + 4 + 1) = -13 (The CORRECT answer) Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types Page 74 Once again, lest we forget, let’s redo the (next-to) last example using the REAL (32-Bit) representation: Let’s store the number -79 as a two’s compliment value on 32-bits: 79 = 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 1 1 1 1 1’s Compliment: 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 0 0 0 0 Add 1: + 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 0 0 0 1 To get 2’s Compliment Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types Page 75 Now, adding the numbers -79 and 92 on 32-bits: Drop 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 -79 = 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 0 0 0 1 92 = 0 0 0 0 01 0 0 0 0 0 0 0 0 0 0 0 01 0 0 0 0 0 0 0 0 1 0 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 1 = 0 * 231 + 0 * 230 + 0 * 229 + 0 * 228 + 0 * 227 + 0 * 226 + 0 * 225 + 0 * 224 + 0 * 223 + 0 * 222 + 0 * 221 + 0 * 220 + 0 * 219 + 0 * 218 + 0 * 217 + 0 * 216 + 0 * 215 + 0 * 214 + 0 * 213 + 0 * 212 + 0 * 211 + 0 * 210 + 0 * 29 + 0 * 28 + 0 * 27 + 0 * 26 + 0 * 25 + 0 * 24 + 1 * 23 + 1 * 22 + 0 * 21 + 1 * 20 = 23 + 22 + 21 + 20 = 8 + 4 + 1 = 13 Alright !! Can we stop with the 32-bit illustrations ??? Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types Page 76 Note that complimenting can be applied to any base • In Decimal (for example): 7 Is the compliment of 3 19 Is the compliment of 81 764 Is the compliment of 236 ??? How So ??? 10 – 3 = 7 100 – 19 = 81 1000 – 236 = 764 ??? So What ??? Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types Page 77 Instead of subtracting, we can add the compliment and get the same result: 1 9 –7 2 Or 9 +3 2 11 98 – 81 17 Or Or The carry-over is dropped 98 + 19 17 11 492 – 236 256 Again, the carry-over is dropped The carry-over is dropped 492 + 764 256 ??? Which is easier: Addition or Subtraction ??? Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types Page 78 Before we move on to our next data type, let’s summarize: Data Type Old PC Value New PC Value Mainframe Value char (and signed char) -27 to 27 – 1 = -128 to 127 -128 to 127 -128 to 127 unsigned char 0 to 28 – 1 = 0 to 255 0 to 255 0 to 255 short (and signed short) -215 to 215 – 1 = 32,768 to 32,767 32,768 to 32,767 32,768 to 32,767 unsigned short 0 to 216 – 1 = 0 to 65,535 0 to 65,535 0 to 65,535 int (and signed int) -215 to 215 – 1 = 32,768 to 32,767 -231 to 231 – 1 = -2,147,483,648 to 2,147,483,647 -231 to 231 – 1 = -2,147,483,648 to 2,147,483,647 unsigned int 0 to 216 – 1 = 0 to 65,535 0 to 232 – 1 = 0 to 4,294,967,295 0 to 232 – 1 = 0 to 4,294,967,295 long (and signed long) -231 to 231 – 1 = -2,147,483,648 to 2,147,483,647 -231 to 231 – 1 = -2,147,483,648 to 2,147,483,647 -263 to 263 – 1 = -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 unsigned long 0 to 232 – 1 = 0 to -4,294,967,295 0 to 232 – 1 = 0 to -4,294,967,295 0 to 264 – 1 = 0 to 18,446,744,073,709,551,615 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types Page 79 Floating-Point (real) numbers Consider the Number: 4,321.42 The number could be rewritten as: 4,321.42 = 432.142 * 10 = 432.142 * 101 = 432.142E+1 = 43.2142 * 100 = 43.2142 * 102 = 43.2142E+2 = 4.32142 * 1000 = 4.32142 * 103 = 4.32142E+3 = .432142 * 10000 = .432142 * 104 = .432142E+4 What Difference does it make?? Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types If we consider the last notation: Page 80 .432142E+4 Notice that there are three components: The Sign: In this case plus (implied) The Value (or Mantissa) The Exponent (or Characteristic of the exponent): The power to which we must raise our base (in this case, base 10) When storing real (floating-point) numbers, we MUST keep track of each of the components How are these components Stored?? Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types Page 81 The sign is easy: As with integers, we need only 1 bit. How to distribute the remaining bits is more complex. If we use 2-bytes (16-bits), as we did with integers, we have have 15-bits left to distribute among the remaining two components (The Mantissa and the Characteristic of the Exponent) There are THREE other considerations: • The Mantissa can only take on POSITIVE Values (The Sign-bit will indicate if the value is positive or negative) • The Characteristic of the Exponent (or Characteristic) can take on BOTH positive and negative values) • For every bit we allocate to the Mantissa, there is 1 (one) less bit available for the Characteristic Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types Page 82 What’s the trade-off between allocating bits to the Mantissa or the Characteristic (of the exponent) ??? Consider the following Number: Where: .8072 E+11 Mantissa The actual Number is: Characteristic 80,720,000,000 First, Let’s consider the Mantissa (.8072): To represent the number 8072, we need 13-bits Why ??? Because: 212 = 4096 (Meaning the numbers 0 … 4095) 213 = 8192 (Meaning the numbers 0 … 8191) 214 = 16384 (Meaning the numbers 0 … 16383) Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types Page 83 BUT, The Mantissa is a decimal (i.e., .8072) ???? TRUE. But what if the number were: .80716 Which is SMALLER BUT Requires MORE Digits to represent Could We Represent the decimal with 13-bits ??? NO. To Represent the decimal .80716 we need: n= log(I) log(2) = 4.907 0.301 = 16.302 = 17 bits The more the number of bits allocated to the Mantissa, The greater the Level of Precision Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types Page 84 IF we allocate 13-bits to the Mantissa: • we have 3 decimals of precision IF we allocate 17-bits to the Mantissa: • we have 5 decimals of precision That can’t be true! With 13-bits we can represent numbers to 8191. There are 4-digits in 8191. With 17bits we can represent numbers to 217 - 1 = 131071. There are 6-digits in the number 131071. YES - That’s True. …. BUT …. Given 13-bits, can we represent the number 9235 ?? (also a 4-digit Number) Given 17-bits, can we represent the number 131072 ?? (also a 6-digit Number) Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types Page 85 How do we interpret the Characteristic ??? The more the number of bits allocated to the Characteristic, The greater the Magnitude Assume that we have the choice of allocating EITHER 4 or 5 bits to the Characteristic. Since the characteristic can be either positive or negative, the ranges are: 4-bits Allocated: -23 to +23 - 1 OR -8 to +7 5-bits Allocated: -24 to +24 - 1 OR -16 to +15 So ??? Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types Page 86 Which is the larger Number ?? (The largest characteristic available with 4-bits) .124E+7 = 1240000 OR .124E+15 = 124000000000000 (The largest characteristic available with 5-bits) REMEMBER: Each time we add 1 additional bit, we DOUBLE the size of the number as can represent Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types Page 87 . . . . . SIMILARLY . . . . . Which is the SMALLER Number ?? .124E-8 = 0.00000000124 (The largest negative characteristic available with 4-bits) OR (The largest negative characteristic available with 5-bits) .124E-16 = 0.0000000000000000124 Magnitude refers to how Large OR How Small the numbers which can represent are. Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types Page 88 What’s More Important: Precision OR Magnitude ?? Depends on Individual Needs: • When Dealing with Measures such as PI (), Precision. • When Dealing with (e.g) Number of atoms in a jar: Magnitude How ARE the bits allocated ??? FIRST, we have to determine how many bits we should use Let’s assume that 16-bits (the same number we used for integers) is sufficient Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types Page 89 Let’s look at ALL possible combinations: Mantissa Bits 1 2 3 4 5 6 7 8 9 10 11 12 13 14 Characteristic Bits Mantissa Range 0 … 21 -1 => 0 .. 1 0 … 22 -1 => 0 .. 3 0 … 23 -1 => 0 .. 7 0 … 24 -1 => 0 .. 15 0 … 25 -1 => 0 .. 31 0 … 26 -1 => 0 .. 63 0 … 27 -1 => 0 .. 127 0 … 28 -1 => 0 .. 255 0 … 29 -1 => 0 .. 511 0 … 210 -1 => 0 .. 1,023 0 … 211 -1 => 0 .. 2,047 0 … 212 -1 => 0 .. 4,095 0 … 213-1 => 0 .. 8,191 0 … 214-1 => 0 .. 16,387 14 13 12 11 10 9 8 7 6 5 4 3 2 1 Characteristic Range -213 -213 - 1 = -8,192 to 8,191 -212 -212- 1 = -4,096 to 4,095 -211 -211- 1 = -2,048 to 2,047 -210 -210- 1 = -1,024 to 1,023 -29 -29- 1 = -512 to 511 -28 -28- 1 = -256 to 255 -27 -27- 1 = -128 to 127 -26 -26- 1 = -64 to 63 -25 -25- 1 = -32 to 31 -24 -24- 1 = -16 to 15 -23 -23- 1 = -8 to 7 -22 -22- 1 = -4 to 3 -21 -21- 1 = -2 to 1 -20 -20- 1 = -1 to 0 What’s the Solution ??? Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types Page 90 ALL of the combinations are Inadequate: What can we do ??? We Need More bits How Many More ??? It would make sense to see what would happen if we added on another 16-bits (2-bytes) How will that change things ??? We still need 1 (one) bit for the Sign, but we will now have the remaining 31-bits (4-bytes - 1-bit = 32-bits - 1 = 31-bits) to distribute between the Mantissa and the Characteristic. Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types Page 91 Let’s look at SOME combinations if 31-bits are allocated: Mantissa Characteristic Bits Bits Precision (decimals) 15 16 17 18 19 20 21 22 23 24 25 26 27 28 32,767 (4 decimalss) 65,535 (4 decimals) 131,071 (5 decimals) 262,143 (5 decimals) 524,288 (5 decimals) 1,048,576 (6 decimals) 2,097,152 (6 decimals) 4,194,304 (6 decimals) 8,388,608 (6 decimals) 16,777,216 (7 decimals) 33,554,432 (7 decimals) 67,108,864 (7 decimals) 134,217,728 (8 decimals) 268,435,456 (8 decimals) Data Structures in C for Non-Computer Science Majors 16 15 14 13 12 11 10 9 8 7 6 5 4 3 Magnitude (E-Vals) -215 -215 - 1 = -32,786 to 32,785 -214 -214- 1 = -16,384 to 16,383 -213 -213- 1 = -8.192 to 8,191 -212 -212- 1 = -4,096 to 4,095 -211 -211- 1 = -2,048 to 2,047 -210 -210- 1 = -1,024 to 1,023 -29 -29- 1 = -512 to 511 -28 -28- 1 = -256 to 255 -27 -27- 1 = -128 to 127 -26 -26- 1 = -64 to 63 -25 -25- 1 = -32 to 31 -24 -24- 1 = -16 to 15 -23 -23- 1 = -8 to 7 -22 -22- 1 = -4 to 3 Kirs and Pflughoeft Basic Data Types Page 92 What’s the best Combination ??? There is no clear-cut answer. One common allocation scheme is: 1 - bit 7 - bits Sign 24 - bits Mantissa Characteristic of the Exponent What Range of Numbers can we represent ??? Real Numbers are characterized by their LEVEL OF PRECISION, Not range This scheme allows for 7 decimals of precision Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types Page 93 OK - But what is the LARGEST (positive) number we can represent ??? .(224 - 1) E (26 -1) = .16777216 E 63 = 167,772,160,000,000,000,000,000,000,000,000,000,000,000, 000,000,000,000,000,000,000 What is the SMALLEST number we can represent ??? - .1 E -(26) = .1 E -64 = .00000000000000000000000000000000000000000000000000 00000000000000001 And In Between ??? ALL values: BUT only to 7 (or 8) decimals of precision. Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types Page 94 OK, I’ve been waiting, Oh great Swami! If Integers are now 32-bits, I’ll bet Real Numbers are 64-bits. Right ??? Nope -- Still 32-bits Can you prove all of this ??? Sure – But I’m going to have to show you a C Program (A little early in the course – bear with me, even if you don’t understand all of it – you will later) The only thing you need to know in advance is: • There is a C/C++ operator called sizeof • sizeof returns the number of bytes stored at any location in memory Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types Page 95 Here is the C++ Program I wrote: Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types Page 96 Here is Program Output: Location mycharacter Location myshort Location mydouble Location myint Location myfloat Location mylong OK, OK – Are we done yet ?? My Head Hurts !! Almost ---- Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types Page 97 What if I need greater Precision OR Magnitude ??? There are two additional real data types (in C): • data type double: using 8-bytes (64-bits) of storage • data type long double: using 16-byte (128-bits) of storage How are the bits distributed between the Mantissa and Characteristic ?? That is not easily answered: ANSI (The American National Standards Institute) requires only that the data type double (n C) have a minimum of 10 decimals of Precision. And For long doubles ?? Data Structures in C for Non-Computer Science Majors Same Requirements Kirs and Pflughoeft Basic Data Types Page 98 Summary of Basic Data Types Data Type Character Integer Real C Declaration Storage 1-byte char 1-byte signed char unsigned char 1-byte 2-bytes int (old) 4-bytes int (new) 2-bytes signed int unsigned int 2-bytes 4-bytes long signed long 4-bytes unsigned long float double long double 4-bytes 4-bytes 8-bytes 16-bytes Data Structures in C for Non-Computer Science Majors Range -128 to + 127 -128 to + 127 0 to + 255 -32,768 to 32,767 -2,147,483,648 to …. -32,768 to 32,767 0 to 65,537 -2,147,483,648 to 2,147,483,647 -2,147,483,648 to 2,147,483,647 0 to 4,294,967,295 7 decs. precision 10 decs. precision 10 decs. precision Kirs and Pflughoeft Basic Data Types Data Structures in C for Non-Computer Science Majors Page 99 Kirs and Pflughoeft