Page 1 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types Chapter 2 Basic Data Types
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)
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: 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
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
Page 5 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types Some additional Examples of adding in Binary: 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!
Page 6 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types From the ASCII Tables, we know: is the sequence of bits needed to represent ‘3’ is the sequence of bits needed to represent ‘4’ Therefore: Must be the sequence of bits needed to represent ‘7’ According to the ASCII Tables is the sequence of bits needed to represent the character ‘g’. What Gives?? Characters vs. Numeric Values
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
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
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
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 : And, with 4-bits, there are 16 possible combinations: Numeric Values
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 DecimalBinaryDecimalBinary Numeric Values
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: Minutes Total Seconds 1 1 Hour Total Minutes So 5,752 Seconds = 1 hour, 35 minutes, and 52 seconds Numeric Conversions
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
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 No. Inches Because there 3 Feet in a Yard: 15 No. Yards No. Feet And so there are 15 yards, 2 feet, and 11 inches in 575 inches. Check: 3 * 15 = * 12 = = 575 Inches Example #2: Convert 575 In. to Yds, Ft, and In. Numeric Conversions
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
Page 16 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types Take, for example, the number (which we know is ) 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 = 11 (Check the Table) Numeric Conversions
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) The New Quotient is Zero: Quit Collecting Remainders from last to first: The Binary Value is: Example 2: Convert to binary (= ):
Page 18 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types Example 3: Convert 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:
Page 19 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types How do I know that is really ?? 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.
Page 20 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types Consider the number 3, We could have written it as: 3, OR 3 * * * * 1 OR 3 * * * * 10 0 This last notation gives us some clue to how we might go about converting from binary to decimal
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 (which we know is ) could be rewritten as: 1 * * * * * 2 0 = 1 * * * * * 1 = = 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, ): Number: Exponent Position: Written as: 3 * * * * 10 0 Rewritten as: 3 * * * * 1 Valued at: 3, = 3,467 The same holds true for our binary number ( ): Number: Exponent Position: Written as: 1 * * * * * 2 0 Rewritten as: 1 * * * * * 1 Valued at: = 23
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: Exponent Position: Written as: Rewritten as: Valued at: 23 (There are other ways of converting from binary to Decimal. Check the on-line Tutorial Page for a different one.)
Page 24 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types Another Example: Convert to Binary and Check it: Since the new quotient is 0: STOP & Collect The binary of is: Check: Number: Position: Value: = = 107
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 = E.g., 8 10 = = = 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 8 10 MUST be one more than or Given 10 bits, we know that 2 10 = 1,024, meaning that we can represent the numbers from 0 to 1,023 MUST equal MUST be one more than or
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 to Octal (base 8): Remainder Since the new quotient is 0, collect from last to first =134 8 Check: Number: Position: Value: 1* * * 8 0 = 1* * * 1 = = 92
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 OctalBinary
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 (which is and see the previous example) = (Check against Table) Notice that there is a direct transference from binary to octal and octal to binary.
Page 29 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types Example 2: Consider the number Stop and Collect = In Octal ?? = Sure?? = 7 * * * 8080 = 7 * * * 1 = = 457
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
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 A B C D E F1111
Page 32 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types Once again, consider the binary number = = see the prior example What is the Hexadecimal Value ?? C Sure??? 5C 16 = 5 * C * 16 0 = 5C 16 = 5 * * 16 0 = 5 * * 1 = = 92
Page 33 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types Example 2: Consider the number Stop and Collect = In Hexadecimal ?? E = 1E6 16 Sure?? 1E6 16 = 1 * E * * 16 0 = 1 * * * 16 0 = = 486 = 1 * * * 1
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 = F= 2F 16 Sure??? = = = Octal 57 Hexadecimal = F 2 2F
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
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 ( ) be stored at location a (The address will be determined at run-time)
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’;
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’ = = = (On 8-bits) Or: Which is how we would set the micro-switches at location a in RAM
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)
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 ???
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 ???
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!)
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: And then collect the remainders in reverse order (By the way, did I mention that calculators will NOT be allowed in the first Quiz ???)
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 (Its obvious that my slides are already too crowded!)
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”
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 !!
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: locations in memory 2 locations in memory 2 locations in memory
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
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 In location number in RAM To store the valueat location number in RAM AFTER the initial declaration
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???
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 : The remaining (15) bits will be interpreted as non-negative If we place a 1 (one) in the first (left-most) bit : The remaining (15) bits will be interpreted as negative
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: to OR -32,768 to +32,767 But of course in reality: to – 1 = -2,147,483,648 to 2,147,483,647
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 Would store the value at location signednumber in RAM IF the variable had been declared
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: at location signednumber in RAM IFF the variable signednumber had previously been declared and initialize it as:
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: Stores the value at location signednumber in RAM You know, You’re really starting to annoy me !! signednumber At location signednumber
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.
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 = ; 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 ???
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: And the number -8: We should get 0 (zero). Right?
Page 59 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types Let’s Try it: Which Translates to: Plus -8 Equals -16 ??? What Gives???
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: Would be: The process is called Complimenting Now, if we add the two numbers (8 + -8) together:
Page 61 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types 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.
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: 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.
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: = (on 16-bits) 7 = (on 16-bits) -7 = (one’s compliment) (We can not have a non-negative zero)
Page 64 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types Now adding 8 + (-7): 8 = = Which of course evaluates to 0 (zero) (we know the answer is 1) Therefore, we need to add 1 to get the correct answer
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: = = = Then: 7 = = Since the result is negative, we must compliment Which evaluates to -1 (The correct answer)
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 Number 8: Compliment: Add 1:
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: :8: + - 8: The Overflow is dropped The Value is 0 (Zero) 0
Page 68 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types Let’s Take Another Example: Add: = =
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: = ’s Compliment: Add 1:+ 1 Is this really how the value -79 will be stored in RAM ??? YES !!! 2’s Compliment:
Page 70 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types NOW we can add the values : 92 = = Carry-Over is Dropped Since the sign is positive, evaluate the remaining bits: = = = 13 Which we know is correct
Page 71 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types One Final Example: Add: We Already know that = But to get -92 we must again compliment +92 and add 1: = 1’s Compliment: Add 1: Which is how we would store -92 in RAM (16-bits) 2’s Compliment:
Page 72 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types NOW we can add the values : -92 = = The result is negative, Therefore we must Compliment = -( ) = -(8 + 4) = -12 BUT The correct answer -13 ????
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 ’s Compliment: Add 1: Which evaluates to: = -( ) = -( ) = -13 (The CORRECT answer)
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: Let’s store the number -79 as a two’s compliment value on 32-bits: 79 = Add 1: To get 2’s Compliment
Page 75 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types Now, adding the numbers -79 and 92 on 32-bits: -79 = = Drop = 0 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 2020 = = = 13 Alright !! Can we stop with the 32-bit illustrations ???
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 = – 19 = – 236 = 764 ??? So What ???
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 Again, the carry-over is dropped 98 – Or The carry-over is dropped 492 – Or The carry-over is dropped ??? Which is easier: Addition or Subtraction ???
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) to 2 15 – 1 = 32,768 to 32,767 unsigned short 0 to 2 16 – 1 = 0 to 65,535 int (and signed int) to 2 15 – 1 = 32,768 to 32, to 2 31 – 1 = -2,147,483,648 to - 2,147,483, 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) to 2 31 – 1 = -2,147,483,648 to - 2,147,483, to 2 31 – 1 = -2,147,483,648 to - 2,147,483, 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
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, The number could be rewritten as: 4, = * 10 = * 10 1 = E+1 = * 100 = * 10 2 = E+2 = * 1000 = * 10 3 = E+3 = * = * 10 4 = E+4 What Difference does it make??
Page 80 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types If we consider the last notation: E+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
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
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 = (Meaning the numbers 0 … 16383)
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: Which is SMALLER BUT Requires MORE Digits to represent Could We Represent the decimal with 13-bits ??? NO. To Represent the decimal we need: n = log(I) log(2) = = = 17 bits Mantissa The more the number of bits allocated to the Mantissa, The greater the Level of Precision
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 There are 4-digits in With 17- bits we can represent numbers to = There are 6-digits in the number 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 ?? (also a 6-digit Number)
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 OR -8 to +7 5-bits Allocated: -2 4 to OR -16 to +15 So ???
Page 86 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types Which is the larger Number ??.124E+7 = (The largest characteristic available with 4-bits) OR.124E+15 = (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
Page 87 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types Which is the SMALLER Number ??.124E-8 = (The largest negative characteristic available with 4-bits) OR.124E-16 = (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.
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
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 … => Characteristic Bits 14 Characteristic Range = -8,192 to 8, … => = -4,096 to 4, … => = -2,048 to 2, … => = -1,024 to 1, … => = -512 to … => = -256 to … => = -128 to … => = -64 to … => = -32 to … => 0.. 1, = -16 to … => 0.. 2, = -8 to … => 0.. 4, = -4 to … => 0.. 8, = -2 to … => , = -1 to 0 What’s the Solution ???
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.
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) = -32,786 to 32, ,535 (4 decimals) = -16,384 to 16, ,071 (5 decimals) = to 8, ,143 (5 decimals) = -4,096 to 4, ,288 (5 decimals) = -2,048 to 2, ,048,576 (6 decimals) = -1,024 to 1, ,097,152 (6 decimals) = -512 to ,194,304 (6 decimals) = -256 to ,388,608 (6 decimals) = -128 to ,777,216 (7 decimals) = -64 to ,554,432 (7 decimals) = -32 to ,108,864 (7 decimals) = -16 to ,217,728 (8 decimals) = -8 to ,435,456 (8 decimals) = -4 to 3
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
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 ???.( ) E (26 (26 -1) = 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 = And In Between ??? ALL values: B BB BUT only to 7 (or 8) decimals of precision.
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
Page 95 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types Here is the C++ Program I wrote:
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 ----
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
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 signed char 1-byte-128 to unsigned char 1-byte0 to 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 ….
Page 99 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types