Presentation is loading. Please wait.

Presentation is loading. Please wait.

Page 1 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types Chapter 2 Basic Data Types.

Similar presentations


Presentation on theme: "Page 1 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types Chapter 2 Basic Data Types."— Presentation transcript:

1

2 Page 1 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types Chapter 2 Basic Data Types

3 Page 2 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types What you MUST know before we start: 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 (Remember: The topics in this course build on each other)

4 Page 3 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types Adding in Binary There are only four (4) possible combinations: 0 +0 0 +1 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

5 Page 4 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types 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

6 Page 5 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types Some additional Examples of adding in Binary: 1 0 1 + 1 0 1 1 1 1 1 1 + 1 0 1 0 1 1 1 1 1 1 + 1 0 1 0 0 1 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 1 0 1 0 1 1 1 1 0 Adding in Binary 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!

7 Page 6 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types 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?? Characters vs. Numeric Values

8 Page 7 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types 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 Characters vs. Numeric Values

9 Page 8 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types 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: 51 The character ‘4’ is actually the numeric value: 52 The character ‘g’ is actually the numeric value: 103 Check your ASCII Tables Characters vs. Numeric Values

10 Page 9 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types 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. Characters vs. Numeric Values

11 Page 10 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types 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 001101 010110 011111 And, with 4-bits, there are 16 possible combinations: 0000 010010001100 0001010110011101 0010011010101110 0011011111001111 Numeric Values

12 Page 11 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types The same sequencing pattern is used to represent numeric values DecimalBinary 11 2 3 4 5 6 7 8 9 00 DecimalBinaryDecimalBinary 10 11 100 101 110 111 1000 1001 1010102010100 1110112110101 1211002210110 1311012310111 1411102411000 15 11112511001 16100002611010 17100012711011 18 100102811100 19100112911101 Numeric Values

13 Page 12 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types 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 95 Minutes Total 60 5752 5700 52 52 Seconds 1 1 Hour Total 60 95 60 35 35 Minutes So 5,752 Seconds = 1 hour, 35 minutes, and 52 seconds Numeric Conversions

14 Page 13 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types Our Check would be to perform the opposite operations: * 60 Seconds 5,700 Seconds + 52 Seconds 95 Minutes 5,752 Seconds 1 Hour * 60 = 60 MinutesNumber Hours Number Minutes Total Minutes Secs. Per Minute Number Seconds Total Seconds 35 Minutes In the last case, we were (sort-of; not really) working in base 60 (since 1 hr = 50 mins. And 1 Min. = 60 Seconds) Numeric Conversions

15 Page 14 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types Because there 12 Inches in a Foot: 47 Total Feet 12 575 564 11 No. Inches Because there 3 Feet in a Yard: 15 No. Yards 3 47 45 2 No. Feet And so there are 15 yards, 2 feet, and 11 inches in 575 inches. Check: 3 * 15 = 45 + 2 47 * 12 = 564 + 11 = 575 Inches Example #2: Convert 575 In. to Yds, Ft, and In. Numeric Conversions

16 Page 15 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types What does this have to do with converting from decimal to binary?? 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 Instead of dividing by 60

17 Page 16 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types Take, for example, the number 11 10 (which we know is 1011 2 ) 5 2 11 10 1 2 2 5 4 1 2 0 2 1 0 1 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 1011212 = 11 (Check the Table) Numeric Conversions

18 Page 17 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types 2 28 (The notation used varies slightly to save space) Quotient Remainder (If Even = 0; If Odd = 1) 0 2 14 0 2 7 1 2 3 1 2 1 1 The New Quotient is Zero: Quit Collecting Remainders from last to first: The Binary Value is: 11100202 Example 2: Convert 28 10 to binary (= 11100 2 ):

19 Page 18 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types Example 3: Convert 44 10 to binary: 44/2 = 22 The notation used differs to correspond to c syntax: / Integer Division: The result is the quotient % Modulus Arithmetic: The Result is the Remainder 44 % 2 = 0 22/2 = 1122 % 2 = 0 11/2 = 511 % 2 = 1 5/2 = 2 5 % 2 = 1 2/2 = 1 2 % 2 = 0 1/2 = 0 1 % 2 = 1 Since the New Quotient is 0: STOP Collecting from Last to First: 101100202

20 Page 19 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types How do I know that 101100 2 is really 44 10 ?? 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.

21 Page 20 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types Consider the number 3,467 10 We could have written it as: 3,000 + 400 + 60 + 7 OR 3 * 1000 + 4 * 100 + 6 * 10 + 7 * 1 OR 3 * 10 3 + 4 * 10 2 + 6 * 10 1 + 7 * 10 0 This last notation gives us some clue to how we might go about converting from binary to decimal

22 Page 21 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types HOW??? Any Number, in any base, can be written in the same manner For example, the binary number 10111 2 (which we know is 23 10 ) could be rewritten as: 1 * 2 4 + 0 * 2 3 + 1 * 2 2 + 1 * 2 1 + 1 * 2 0 = 1 * 16 + 0 * 8 + 1 * 4 + 1 * 2 + 1 * 1 = 16 + 0 + 4 + 2 + 1 = 23

23 Page 22 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types Remember how we laid out our decimal number (3,467 10 ): Number: 3 4 6 7 Exponent Position: 3 2 1 0 Written as: 3 * 10 3 + 4 * 10 2 + 6 * 10 1 + 7 * 10 0 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 (10111 2 ): Number: 1 0 1 1 1 Exponent Position: 4 3 2 1 0 Written as: 1 * 2 4 + 0 * 2 3 + 1 * 2 2 + 1 * 2 1 + 1 * 2 0 Rewritten as: 1 * 16 + 0 * 8 + 1 * 4 + 1 * 2 + 1 * 1 Valued at: 16 + 0 + 4 + 2 + 1 = 23

24 Page 23 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types In Binary, our calculations are actually simplified since: 1 * Any Number = That Number 0 * Any Number = 0 AND Therefore: Number: 1 0 1 1 1 Exponent Position: 4 3 2 1 0 Written as: 2 4 + 2 2 + 2 1 + 2 0 Rewritten as: 16 + 4 + 2 + 1 Valued at: 23 (There are other ways of converting from binary to Decimal. Check the on-line Tutorial Page for a different one.)

25 Page 24 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types Another Example: Convert 107 10 to Binary and Check it: 2 107 1 2 53 1 2 26 0 2 13 1 2 6 0 2 3 1 2 1 1Since the new quotient is 0: STOP & Collect The binary 107 10 of is:1101011212 Check: Number: 1 1 0 1 0 1 1 Position: 6 5 4 3 2 1 0 Value: 2 6 + 2 5 + 2 3 + 2 1 + 2 0 = 64 + 32 + 8 + 2 + 1 = 107

26 Page 25 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types Note that some binary numbers are easy and don’t conversion: 7 10 = 111 2 E.g., 8 10 = 1000 2 1023 10 = 1111111111 2 1024 10 = 10000000000 2 How Do we Know that??? Given 3 bits, we know that 2 3 = 8, meaning that we can represent the numbers from 0 to 7  7 10 MUST equal 111 2  8 10 MUST be one more than 111 2 or 1000 2 Given 10 bits, we know that 2 10 = 1,024, meaning that we can represent the numbers from 0 to 1,023  1023 10 MUST equal 1111111111 2  1024 10 MUST be one more than or 10000000000 2

27 Page 26 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types 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 92 10 to Octal (base 8): 8 924 Remainder 8 113 8 11 0 Since the new quotient is 0, collect from last to first 92 10 =134 8 Check: Number: 1 3 4 Position: 2 1 0 Value: 1* 8 2 + 3 * 8 1 + 4 * 8 0 = 1* 64 + 3 * 8 + 4 * 1 = 64 + 24 + 4 = 92

28 Page 27 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types 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 0 Binary 000 1001 2010 3 011 OctalBinary 4100 5 101 6110 7111

29 Page 28 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types Why is that easy? Consider the binary number 1011100 2 (which is 92 10 and 134 8 - see the previous example) 1011100 = 1 011 100 1 3 4 (Check against Table) Notice that there is a direct transference from binary to octal and octal to binary.

30 Page 29 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types Example 2: Consider the number 457 10 2 457 2 228 2 114 2 57 2 28 2 14 2 7 2 3 2 1 1 Stop and Collect 0 0 1 0 0 1 1 1 111001001212 = 457 10 In Octal ?? 111 7 001 1 1= 711 8 Sure?? 711 8 = 7 * 8 2 + 1 * 8 1 + 1 * 8080 = 7 * 64 + 1 * 8 + 1 * 1 = 448 + 8 + 1 = 457

31 Page 30 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types What about Hexadecimal (Base 16) ?? Basically, Hex is used for the same reason: It is easy to convert Since 2 4 = 16 pieces of information, we can represent all the digits between 0 and 15: Digit Binary Digit Binary 0 0000 81000 1 0001 91001 2 0010101010 3 0011111011 40100121100 50101131101 60110141110 7 0111151111

32 Page 31 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types 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 Binary Digit Binary 0 000081000 1 000191001 2 0010A1010 3 0011B1011 40100C1100 50101D1101 60110E1110 7 0111F1111

33 Page 32 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types Once again, consider the binary number 1011100 2 = 92 10 = 134 8 - see the prior example What is the Hexadecimal Value ?? 1011100 5C Sure??? 5C 16 = 5 * 16 1 + C * 16 0 = 5C 16 = 5 * 16 1 + 12 * 16 0 = 5 * 16 + 12 * 1 = 80 + 12 = 92

34 Page 33 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types Example 2: Consider the number 486 10 2 486 2 243 2 121 2 60 2 30 2 15 2 7 2 3 2 1 0 Stop and Collect 1 1 0 0 1 1 1 1 111100110202 = 486 10 In Hexadecimal ?? 1 1 1110 E 0110 6= 1E6 16 Sure?? 1E6 16 = 1 * 16 2 + E * 16 1 + 6 * 16 0 = 1 * 16 2 + 14 * 16 1 + 6 * 16 0 = 256 + 224 + 6 = 486 = 1 * 256 + 14 * 16 + 6 * 1

35 Page 34 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types What about Converting from Octal to Hexadecimal?? The easiest way is to use binary numbers: 57 8 = 5 7 1 0 11 1 1 2F= 2F 16 Sure??? 101111 2 = 25 25 + 23 23 + 22 22 + 2 1 + 2 0 = 32 + 8 + 4 + 2 + 1 = 47 10 8 477 8 55 Octal 57 Hexadecimal 16 47 16 2 15 = F 2 2F

36 Page 35 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types 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) Numbers 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

37 Page 36 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types In C, when we make the declaration: char a; We are: Requesting that 1-byte of RAM be allocated char (by using the reserved word char ) aIndicating that whenever we use the variable a, we are actually referring to a specific location (Address) in memory (RAM) If we were to make the declaration: char a = ; char a = ‘A’ ; We would also be requesting: That the numeric value 65 (1000001 2 ) be stored at location a (The address will be determined at run-time)

38 Page 37 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types We could have also made the initialization: char = 65; char a = 65; Which would have exactly the same effect as: 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 char = ‘A’; char a = ‘A’;

39 Page 38 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types 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’ = 65 10 = 1000001 = 01000001 (On 8-bits) Or: 01000001 Which is how we would set the micro-switches at location a in RAM

40 Page 39 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types But, if we request only 1-byte of storage doesn’t that mean that we can only store the numbers 0 through 255 (since 2 8 = 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)

41 Page 40 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types CAVEAT STUDENTI !!!! 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 ???

42 Page 41 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types 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 ???

43 Page 42 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types 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!)

44 Page 43 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types 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 2171 2 1080 2 540 2 271 2 131 2 60 2 31 2 11 And then collect the remainders in reverse order 11011001 (By the way, did I mention that calculators will NOT be allowed in the first Quiz ???)

45 Page 44 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types Mapping it into memory is straightforward: If we were dealing with 16-bit integers, the mapping would be: (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: IDENTICALThe 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. 1101100100000000 1101100100000000 (Its obvious that my slides are already too crowded!)

46 Page 45 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types 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 illustra- tion, we will pretend that they are still stored on 16- bits”

47 Page 46 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types 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 16- bit integer … STOP!! I’m already confused enough !!

48 Page 47 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types Given 16-bits (2-bytes) we could store 2 16 = 65,536 pieces of information, or the integers 0 through 65,535. How would this be stored?? The number 0 would be stored as: The number 65,535 would be stored as: A number such as 12,316 would be stored as: 00110000 00011100 00000000 11111111 2 locations in memory 2 locations in memory 2 locations in memory

49 Page 48 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types Given 32-bits (4-bytes) we could store 2 32 = 4,294,967,296 pieces of information, or the integers 0 through 4,294,967,295 The number 0 would be stored as: The number 4,294,967,295 would be stored as: A number such as 12,316 would be stored as: 4 locations in memory 4 locations in memory 4 locations in memory 00000000 00000000 11111111 11111111 00000000 00000000 00110000 00011100

50 Page 49 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types In C/C++ we would use the command: unsigned int number; To reserve 16-bits (2-bytes) of RAM at location number unsigned int = 12316; unsigned int number = 12316; Or we could enter the command: = 796; number = 796; Would also store the value 00110000 00011100 In location number in RAM 00000011 00011100 To store the valueat location number in RAM AFTER the initial declaration

51 Page 50 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types Why is the integer variable number declared as unsigned ??? number 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 by defaultsigned 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 non- negative???

52 Page 51 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types 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

53 Page 52 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types But if we use one of our 16-bits for the sign, won’t that reduce the magnitude of the numbers we can represent??? Given 15-bits, we have 2 15 = 32,768 pieces of information Yes: Because we wish to include 0 (zero) in our set of integers The range of integers we can represent is: - 2 15 to +2 15 - 1 OR -32,768 to +32,767 But of course in reality: - 2 31 to +2 31 – 1 = -2,147,483,648 to 2,147,483,647

54 Page 53 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types The declaration: int ; int signednumber; Would reserve 16-bits (2-bytes) of RAM at location signednumber int signednumber = 5789; And the command: signednumber = 796; Or the declaration: signed int signed int signednumber; Would store the value at location signednumber in RAM 0 0010110 01101011 Would store the value at location signednumber in RAM IF the variable had been declared 0 0000011 00011100

55 Page 54 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types int signednumber = 5789; The command: signednumber = 796; Would reserve 32-bits (4- bytes) of storage at location signednumber Would store the value Lest we forget that we have been pretending, let’s repeat the previous example using the REAL (32- bit) representation: 0 0000000 00000000 0000011 00011100 0 0000000 00000000 0010110 01101011 at location signednumber in RAM IFF the variable signednumber had previously been declared and initialize it as:

56 Page 55 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types 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 ; short signednumber; Would reserve 16-bits (2-bytes) of RAM at location signednumber short signednumber = 5789; And the command: signednumber = 796; Or the declaration: signed short signed short signednumber; Would store the value: 0 0010110 01101011 Stores the value 0 0000011 00011100 at location signednumber in RAM You know, You’re really starting to annoy me !! signednumber At location signednumber

57 Page 56 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types 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. Entering the declaration: char acharacter = -107; The declaration: unsigned char acharacter = 87; May be entered without any complications More on this later. is legal, although unexpected things may happen.

58 Page 57 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types There is a data type called long: 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). long lnumber = 1073741824; Well tell me, Oh Master of Confusion, what happens if we DO need larger Integers ??? 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 ???

59 Page 58 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types 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: 0000000000000001000 And the number -8: 100000000000001000 We should get 0 (zero). Right?

60 Page 59 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types Let’s Try it: 0000000000000001000 8 + -8 100000000000001000 1000000000010000 1 Which Translates to: 16- 8 Plus -8 Equals -16 ??? What Gives???

61 Page 60 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types In Binary, the negative of a number is the opposite of that number. The opposite of the number 8: 0000000000000001000 Would be: 1111111111110111 The process is called Complimenting Now, if we add the two numbers (8 + -8) together:

62 Page 61 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types 0000000000000001000 8 1111111111110111 + -8 1111111111111111 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.

63 Page 62 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types 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: 1111111111111111 000000000000000 Number: Compliment: 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.

64 Page 63 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types 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 Consider the following example: 8 - 7 8 = 0000000000001000(on 16-bits) 7 = 0000000000000111(on 16-bits) -7 = 1111111111111000(one’s compliment) (We can not have a non-negative zero)

65 Page 64 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types Now adding 8 + (-7): 8 = 0000000000001000 -7 = 1111111111111000 0000 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 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

66 Page 65 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types If, however, the result is negative, we need not add 1 Consider the following example: 7 - 8 7 = 00000000000001118 = 0000000000001000 -8 = 1111111111110111 Then: 7 = 0000000000000111 -8 = 1111111111110111 0 1 1 1 1 1 1111111111111 Since the result is negative, we must compliment 100000000000000 Which evaluates to -1 (The correct answer)

67 Page 66 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types Must we always have a negative zero??? No - we can eliminate negative zeros by using a twos compliment procedure called twos compliment twos compliment With twos compliment, we perform the same procedure but after complimenting, we add 1 (one) to the result. 0000000000000001000 Number 8: Compliment: 1111111111110111 Add 1: + 1 0 1 0 1 0 1 1111111111111

68 Page 67 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types Now, if we add the two values together: 0000000000000001000 8:8: + - 8: 1111111111111000 0000000000000000 1111111111111 The Overflow is dropped The Value is 0 (Zero) 0

69 Page 68 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types Let’s Take Another Example: Add: 92 + -79 92 10 =2 920 2 460 2 231 2 111 2 51 2 0 2 11 1011100 79 10 =2 791 2 391 2 191 2 91 2 40 2 0 2 11 1001111

70 Page 69 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types Since we wish to store the number -79 as a two’s compliment value (on 16-bits), we must compliment AND then Add 1: 0000000000010001111 79 = 1111111110110000 1’s Compliment: 1111111110110001 Add 1:+ 1 Is this really how the value -79 will be stored in RAM ??? YES !!! 2’s Compliment:

71 Page 70 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft 1111111101110001 Basic Data Types NOW we can add the values 92 + -79: 92 = 0000000000010101100 + -79 = 10110 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 Carry-Over is Dropped Since the sign is positive, evaluate the remaining bits: = 2 3 + 2 2 + 2 0 = 8 + 4 + 1 = 13 Which we know is correct

72 Page 71 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types One Final Example: Add: - 92 + 79 We Already know that 79 10 = 0000000000010001111 But to get -92 we must again compliment +92 and add 1: 0000000000010101100 92 = 1’s Compliment: 1100010111111111 Add 1:+ 1 0010010111111111 11 Which is how we would store -92 in RAM (16-bits) 2’s Compliment:

73 Page 72 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types NOW we can add the values -92 + 79: -92 = 1111111110100100 + 79 = 11001 1 1 1 1111111111 0000000000010001111 The result is negative, Therefore we must Compliment 001100000000000 = -(2 3 + 2 2 ) = -(8 + 4) = -12 BUT The correct answer -13 ????

74 Page 73 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types 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 0000000000000001100 1’s Compliment: Add 1:+ 1 1011000000000000 Which evaluates to: = -(2 3 + 2 2 + 2 0 ) = -(8 + 4 + 1) = -13 (The CORRECT answer)

75 Page 74 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types Once again, lest we forget, let’s redo the (next-to) last example using the REAL (32-Bit) representation: 1’s Compliment: 00001101111111111111111111111111 Let’s store the number -79 as a two’s compliment value on 32-bits: 79 = 11110100000000000000000000000000 Add 1: + 1 10001101111111111111111111111111 To get 2’s Compliment

76 Page 75 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types 10110000000000000000000000000000 Now, adding the numbers -79 and 92 on 32-bits: -79 = 000110111111111111111111111111111100010100001000000000000100000000 92 = 1111111111111111111111111111 Drop = 0 * 2 31 + 0 * 2 30 + 0 * 2 29 + 0 * 2 28 + 0 * 2 27 + 0 * 2 26 + 0 * 2 25 + 0 * 2 24 + 0 * 2 23 + 0 * 2 22 + 0 * 2 21 + 0 * 2 20 + 0 * 2 19 + 0 * 2 18 + 0 * 2 17 + 0 * 2 16 + 0 * 2 15 + 0 * 2 14 + 0 * 2 13 + 0 * 2 12 + 0 * 2 11 + 0 * 2 10 + 0 * 29 29 + 0 * 28 28 + 0 * 27 27 + 0 * 26 26 + 0 * 25 25 + 0 * 24 24 + 1 * 23 23 + 1 * 22 22 + 0 * 21 21 + 1 * 2020 = 23 23 + 22 22 + 21 21 + 2 0 = 8 + 4 + 1 = 13 Alright !! Can we stop with the 32-bit illustrations ???

77 Page 76 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types In Decimal (for example): Note that complimenting can be applied to any base 7Is the compliment of3 19Is the compliment of81 764Is the compliment of236 ??? How So ??? 10 – 3 = 7 100 – 19 = 81 1000 – 236 = 764 ??? So What ???

78 Page 77 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types Instead of subtracting, we can add the compliment and get the same result: 9 – 7 2 Or 1 9 + 3 2 Again, the carry-over is dropped 98 – 81 17 Or 11 98 + 19 17 The carry-over is dropped 492 – 236 256 Or 11 492 + 764 256 The carry-over is dropped ??? Which is easier: Addition or Subtraction ???

79 Page 78 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types Before we move on to our next data type, let’s summarize: Data Type char (and signed char) Old PC Value -2 7 to 2 7 – 1 = -128 to 127 New PC Value -128 to 127 Mainframe Value -128 to 127 unsigned char 0 to 2 8 – 1 = 0 to 255 short (and signed short) -2 15 to 2 15 – 1 = 32,768 to 32,767 unsigned short 0 to 2 16 – 1 = 0 to 65,535 int (and signed int) -2 15 to 2 15 – 1 = 32,768 to 32,767 -2 31 to 2 31 – 1 = -2,147,483,648 to - 2,147,483,647 -2 31 to 2 31 – 1 = -2,147,483,648 to - 2,147,483,647 unsigned int 0 to 2 16 – 1 = 0 to 65,535 0 to 2 32 – 1 = 0 to 4,294,967,295 0 to 2 32 – 1 = 0 to 4,294,967,295 long (and signed long) -2 31 to 2 31 – 1 = -2,147,483,648 to - 2,147,483,647 -2 31 to 2 31 – 1 = -2,147,483,648 to - 2,147,483,647 -2 63 to 2 63 – 1 = -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 unsigned long0 to 2 32 – 1 = 0 to -4,294,967,295 0 to 2 32 – 1 = 0 to -4,294,967,295 0 to 2 64 – 1 = 0 to 18,446,744,073,709,551,615

80 Page 79 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types Floating-Point (real) numbers Consider the Number:4,321.42 The number could be rewritten as: 4,321.42 = 432.142 * 10 = 432.142 * 10 1 = 432.142E+1 = 43.2142 * 100 = 43.2142 * 10 2 = 43.2142E+2 = 4.32142 * 1000 = 4.32142 * 10 3 = 4.32142E+3 =.432142 * 10000 =.432142 * 10 4 =.432142E+4 What Difference does it make??

81 Page 80 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types If we consider the last notation:.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) How are these components Stored?? When storing real (floating-point) numbers, we MUST keep track of each of the components

82 Page 81 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types The sign is easy: As with integers, we need only 1 bit. 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) How How to distribute the remaining bits is more complex. THREE 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) BOTHThe 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

83 Page 82 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types What’s the trade-off between allocating bits to the Mantissa or the Characteristic (of the exponent) ??? Consider the following Number:.8072 E+11 Where: MantissaCharacteristic The actual Number is: 80,720,000,000 First, Let’s consider the Mantissa (.8072): To represent the number 8072, we need 13-bits Why ??? Because: 2 12 = 4096 (Meaning the numbers 0 … 4095) 2 13 = 8192 (Meaning the numbers 0 … 8191) 2 14 = 16384 (Meaning the numbers 0 … 16383)

84 Page 83 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types 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 Mantissa The more the number of bits allocated to the Mantissa, The greater the Level of Precision

85 Page 84 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types 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 17- bits we can represent numbers to 2 17 - 1 = 131071. There are 6-digits in the number 131071. YES - That’s True. …. BUT …. …. 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)

86 Page 85 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types How do we interpret the Characteristic ??? 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: -2 3 to +2 3 - 1 OR -8 to +7 5-bits Allocated: -2 4 to +2 4 - 1 OR -16 to +15 So ???

87 Page 86 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types Which is the larger Number ??.124E+7 = 1240000 (The largest characteristic available with 4-bits) OR.124E+15 = 124000000000000 (The largest characteristic available with 5-bits) REMEMBER: DOUBLE Each time we add 1 additional bit, we DOUBLE the size of the number as can represent

88 Page 87 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types Which is the SMALLER Number ??.124E-8 = 0.00000000124 (The largest negative characteristic available with 4-bits) OR.124E-16 = 0.0000000000000000124 (The largest negative characteristic available with 5-bits)..... SIMILARLY..... Large OR Small Magnitude refers to how Large OR How Small the numbers which can represent are.

89 Page 88 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types 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

90 Page 89 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types ALL Let’s look at ALL possible combinations: Mantissa Bits 1 Mantissa Range 0 … 2 1 -1 => 0.. 1 Characteristic Bits 14 Characteristic Range -2 13 -2 13 - 1 = -8,192 to 8,191 2 0 … 2 2 -1 => 0.. 3 13-2 12 -2 12 - 1 = -4,096 to 4,095 3 0 … 2 3 -1 => 0.. 7 12-2 11 -2 11 - 1 = -2,048 to 2,047 4 0 … 2 4 -1 => 0.. 15 11-2 10 -2 10 - 1 = -1,024 to 1,023 5 0 … 2 5 -1 => 0.. 31 10-2 9 -2 9 - 1 = -512 to 511 6 0 … 2 6 -1 => 0.. 63 9-2 8 -2 8 - 1 = -256 to 255 7 0 … 2 7 -1 => 0.. 127 8-2 7 -2 7 - 1 = -128 to 127 8 0 … 2 8 -1 => 0.. 255 7-2 6 -2 6 - 1 = -64 to 63 9 0 … 2 9 -1 => 0.. 511 6-2 5 -2 5 - 1 = -32 to 31 10 0 … 2 10 -1 => 0.. 1,023 5-2 4 -2 4 - 1 = -16 to 15 11 0 … 2 11 -1 => 0.. 2,047 4-2 3 -2 3 - 1 = -8 to 7 12 0 … 2 12 -1 => 0.. 4,095 3-2 2 -2 2 - 1 = -4 to 3 13 0 … 2 13 -1 => 0.. 8,191 2-2 1 -2 1 - 1 = -2 to 1 14 0 … 2 14 -1 => 0.. 16,387 1-2 0 -2 0 - 1 = -1 to 0 What’s the Solution ???

91 Page 90 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types ALL ALL of the combinations are Inadequate: 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 ??? What can we do ??? 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.

92 Page 91 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types SOME Let’s look at SOME combinations if 31-bits are allocated: Mantissa Bits 15 Precision (decimals) 32,767 (4 decimalss) Characteristic Bits 16 Magnitude (E-Vals) -2 15 -2 15 - 1 = -32,786 to 32,785 16 65,535 (4 decimals) 15-2 14 -2 14 - 1 = -16,384 to 16,383 17 131,071 (5 decimals) 14-2 13 -2 13 - 1 = -8.192 to 8,191 18 262,143 (5 decimals) 13-2 12 -2 12 - 1 = -4,096 to 4,095 19 524,288 (5 decimals) 12-2 11 -2 11 - 1 = -2,048 to 2,047 20 1,048,576 (6 decimals) 11-2 10 -2 10 - 1 = -1,024 to 1,023 21 2,097,152 (6 decimals) 10-2 9 -2 9 - 1 = -512 to 511 22 4,194,304 (6 decimals) 9-2 8 -2 8 - 1 = -256 to 255 23 8,388,608 (6 decimals) 8-2 7 -2 7 - 1 = -128 to 127 24 16,777,216 (7 decimals) 7-2 6 -2 6 - 1 = -64 to 63 25 33,554,432 (7 decimals) 6-2 5 -2 5 - 1 = -32 to 31 26 67,108,864 (7 decimals) 5-2 4 -2 4 - 1 = -16 to 15 27 134,217,728 (8 decimals) 4-2 3 -2 3 - 1 = -8 to 7 28 268,435,456 (8 decimals) 3-2 2 -2 2 - 1 = -4 to 3

93 Page 92 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types What’s the best Combination ??? There is no clear-cut answer. One common allocation scheme is: 1 - bit7 - bits24 - bits Sign Characteristic of the Exponent Mantissa What Range of Numbers can we represent ??? Real Numbers are characterized by their LEVEL OF PRECISION, PRECISION, Not range This scheme allows for 7 decimals of precision

94 Page 93 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types OK - But what is the LARGEST (positive) number we can represent ???.(2 24 - 1) E (26 (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 -(2 6 ) =.1 E -64 = -.00000000000000000000000000000000000000000000000000 00000000000000001 And In Between ??? ALL values: B BB BUT only to 7 (or 8) decimals of precision.

95 Page 94 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types 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

96 Page 95 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types Here is the C++ Program I wrote:

97 Page 96 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types Here is Program Output: Location mycharacter Location myshort Location myint Location mylong Location myfloat Location mydouble OK, OK – Are we done yet ?? My Head Hurts !! Almost ----

98 Page 97 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types 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 A merican N ational S tandards I nstitute) requires only that the data type double (n C) have a minimum of 10 decimals of Precision. And For long doubles ?? Same Requirements

99 Page 98 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types Summary of Basic Data Types Data Type Character C Declaration char Storage 1-byte Range -128 to + 127 signed char 1-byte-128 to + 127 unsigned char 1-byte0 to + 255 Integer int (old) 2-bytes-32,768 to 32,767 signed int 2-bytes-32,768 to 32,767 unsigned int 2-bytes0 to 65,537 long 4-bytes -2,147,483,648 to 2,147,483,647 signed long 4-bytes -2,147,483,648 to 2,147,483,647 unsigned long 4-bytes 0 to 4,294,967,295 Real float 4-bytes 7 decs. precision double 8-bytes 10 decs. precision long double 16-bytes 10 decs. precision int (new) 4-bytes -2,147,483,648 to ….

100 Page 99 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types


Download ppt "Page 1 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types Chapter 2 Basic Data Types."

Similar presentations


Ads by Google