1 / 99

Basic Data Types

Basic Data Types. Basic Data Types. Chapter 2. What you MUST know before we start:. Basic Data Types. (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.

elainee
Download Presentation

Basic Data Types

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Basic Data Types Basic Data Types Chapter 2

  2. What you MUST know before we start: Basic Data Types (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

  3. Adding in Binary Basic Data Types There are only four (4) possible combinations: 0 + 0 0 0 + 1 1 1 + 0 1 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 2any more than there is the symbol 10 in decimal

  4. Adding in Binary Basic Data Types 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

  5. 1 0 1 + 1 0 1 1 1 1 1 1 1 1 + 1 0 1 0 0 1 1 1 1 + 1 0 1 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 Some additional Examples of adding in Binary: Basic Data Types 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!

  6. Characters vs. Numeric Values 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??

  7. Yielding: Characters vs. Numeric Values 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. Good Golly, Miss Molly

  8. Characters vs. Numeric Values 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

  9. Characters vs. Numeric Values 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.

  10. Numeric Values 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 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

  11. Numeric Values Basic Data Types The same sequencing pattern is used to represent numeric values Decimal Binary Decimal Binary Decimal Binary 0 0 10 1010 20 10100 1 1 11 1011 21 10101 2 10 12 1100 22 10110 11 3 13 1101 23 10111 4 100 14 1110 24 11000 5 101 1111 25 11001 15 6 110 16 10000 26 11010 7 111 17 10001 27 11011 8 1000 10010 28 11100 18 9 1001 19 10011 29 11101

  12. 95 95 Minutes Total 60 5752 5700 52 52 Seconds 1 1 Hour Total 60 95 60 35 35 Minutes Numeric Conversions Is there any way of converting from decimal to binary?? Basic Data Types 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: So 5,752 Seconds = 1 hour, 35 minutes, and 52 seconds

  13. Number Hours Total Seconds Number Seconds Secs. Per Minute Total Minutes Number Minutes Numeric Conversions Basic Data Types 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 Minutes 35 Minutes 95 Minutes * 60 Seconds 5,700 Seconds + 52 Seconds 5,752 Seconds

  14. 47 Total Feet 12 575 564 11 No. Inches 15 No. Yards 3 47 45 2 No. Feet Numeric Conversions Basic Data Types Example #2: Convert 575 In. to Yds, Ft, and In. Because there 12 Inches in a Foot: Because there 3 Feet in a Yard: 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

  15. What does this have to do with converting from decimal to binary?? Basic Data Types 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

  16. 5 2 11 10 1 2 2 5 4 1 1 2 2 2 0 0 2 1 0 1 Numeric Conversions Take, for example, the number 1110 (which we know is 10112) Basic Data Types • 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 1 0 1 12 = 11 (Check the Table)

  17. 2 28 2 1 2 3 2 7 2 14 Example 2: Convert 2810 to binary (= 111002): (The notation used varies slightly to save space) Basic Data Types Quotient Remainder (If Even = 0; If Odd = 1) 0 Collecting Remainders from last to first: 0 The Binary Value is: 1 1 1 1 0 02 1 1 The New Quotient is Zero: Quit

  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 Basic Data Types Collecting from Last to First: 44/2 = 22 44 % 2 = 0 22/2 = 11 22 % 2 = 0 1 0 1 1 0 02 11/2 = 5 11 % 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

  19. How do I know that 1011002 is really 4410?? It is not on my table. Basic Data Types 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.

  20. Consider the number 3,46710 Basic Data Types 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

  21. HOW??? Basic Data Types 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

  22. Remember how we laid out our decimal number (3,46710): Exponent Position:3 2 1 0 Basic Data Types 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 * 24 + 0 * 23 + 1 * 22 + 1 * 21 + 1 * 20 Rewritten as: 1 * 16 + 0 * 8 + 1 * 4 + 1 * 2 + 1 * 1 Valued at: 16 + 0 + 4 + 2 + 1 = 23

  23. In Binary, our calculations are actually simplified since: 1 * Any Number = That Number Basic Data Types AND 0 * Any Number = 0 Therefore: Exponent Position: 4 3 2 1 0 Number: 1 0 1 1 1 Written as: 24 + 22 + 21 + 20 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.)

  24. 2 107 1 2 53 1 2 26 0 2 13 1 2 6 0 2 3 1 2 1 1 Another Example: Convert 10710 to Binary and Check it: Basic Data Types The binary 10710 of is: 1 1 0 1 0 1 12 Check: Position: 6 5 4 3 2 1 0 Number: 1 1 0 1 0 1 1 Value: 26 + 25 + 23 + 21 + 20 = 64 + 32 + 8 + 2 + 1 = 107 Since the new quotient is 0: STOP & Collect

  25. Note that some binary numbers are easy and don’t conversion: Basic Data Types E.g., 710 = 1112 810 = 10002 102310 = 11111111112 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

  26. 8 1 8 11 8 92 Note that the methods we used to convert from decimal to binary, and back again, work for any base. Basic Data Types If, for example we wished to convert 9210 to Octal (base 8): Remainder 9210 = 1 3 4 8 4 Check: Position: 2 1 0 3 Number: 1 3 4 Value: 1* 82 + 3 * 81 + 4 * 80 1 = 1* 64 + 3 * 8 + 4 * 1 = 64 + 24 + 4 0 = 92 Since the new quotient is 0, collect from last to first

  27. Why would I care about octal (base 8) ?? Basic Data Types 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 Octal Binary 0 000 4 100 5 1 001 101 2 010 6 110 3 011 7 111

  28. 1011100 = 1 011 100 1 3 4 (Check against Table) Why is that easy? Basic Data Types Consider the binary number 10111002 (which is 9210 and 1348 - see the previous example) Notice that there is a direct transference from binary to octal and octal to binary.

  29. 2 457 2 228 2 114 2 57 2 28 2 14 2 7 2 3 2 1 Example 2: Consider the number 45710 Basic Data Types 1 1 1 1 0 0 1 0 0 12 = 45710 0 In Octal ?? 0 111 001 001 1 7 1 1 = 7118 0 Sure?? 0 7118 = 7 * 82 + 1 * 81 + 1 * 80 1 = 7 * 64 + 1 * 8 + 1 * 1 1 = 448 + 8 + 1 1 = 457 Stop and Collect

  30. Digit Binary Digit Binary 0 0000 8 1000 1 0001 9 1001 2 0010 10 1010 3 0011 11 1011 4 0100 12 1100 5 0101 13 1101 6 0110 14 1110 7 0111 15 1111 What about Hexadecimal (Base 16) ?? Basically, Hex is used for the same reason: It is easy to convert Basic Data Types Since 24 = 16 pieces of information, we can represent all the digits between 0 and 15:

  31. Digit Binary Digit Binary 0 0000 8 1000 1 0001 9 1001 2 0010 A 1010 3 0011 B 1011 4 0100 C 1100 5 0101 D 1101 6 0110 E 1110 7 0111 F 1111 But 10, 11, 12, 13, 14, and 15 are NOT digits. They are Combinations of digits. Basic Data Types True. We need to substitute the symbols: A = 10 B = 11 C = 12 D = 13 E = 14 F = 15 The Conversion Table Should be:

  32. Once again, consider the binary number 10111002 = 9210 = 1348 - see the prior example Basic Data Types What is the Hexadecimal Value ?? 101 1100 = 5C16 5 C Sure??? 5C16 = 5 * 161 + C * 160 = 5 * 161 + 12 * 160 = 5 * 16 + 12 * 1 = 80 + 12 = 92

  33. 2 486 2 243 2 121 2 60 2 30 2 15 2 7 2 3 2 1 Example 2: Consider the number 48610 Basic Data Types 0 1 1 1 1 0 0 1 1 02 = 48610 1 In Hexadecimal ?? 1 1 1110 0110 0 1 E 6 = 1E616 Sure?? 0 1E616 = 1 * 162 + E * 161 + 6 * 160 1 = 1 * 162 + 14 * 161 + 6 * 160 1 = 1 * 256 + 14 * 16 + 6 * 1 = 256+ 224 + 6 1 = 486 1 Stop and Collect

  34. 8 47 8 5 16 2 16 47 What about Converting from Octal to Hexadecimal?? The easiest way is to use binary numbers: Basic Data Types 578 = 5 7 1 0 1 1 1 1 2 F = 2F16 Sure??? 1011112 = 25 + 23 + 22 + 21 + 20 = 32 + 8 + 4 + 2 + 1 = 4710 Octal Hexadecimal 2F 7 15 = F 5 2 57

  35. And Now …. Back to Characters. So far, we know the following: Basic Data Types • 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

  36. In C, when we make the declaration: chara; Basic Data Types We are: • Requesting that 1-byte of RAM be allocated • (by using the reserved word char) • Indicating that whenever we use the variablea, 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: chara = ‘A’; We would also be requesting: • That the numeric value 65 (10000012) be stored at location a

  37. We could have also made the initialization: Basic Data Types chara=65; Which would have exactly the same effect as: chara=‘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

  38. How would the value 65 or ‘A’ actually be stored ??? Basic Data Types 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 (On 8-bits) 0 1 0 0 0 0 0 1 Or: • Which is how we would set the micro-switches at location a in RAM

  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)?? Basic Data Types 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)

  40. CAVEAT STUDENTI !!!! Basic Data Types 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 ???

  41. The point is, that on PCs, until recently: Basic Data Types • 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 ???

  42. All of the slides are based on a 16-bit integer Basic Data Types 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!)

  43. 2 27 2 1 2 3 2 6 2 217 2 13 2 108 2 54 Let me give you a quick example: Basic Data Types • 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: 1 11011001 0 (By the way, did I mention that calculators will NOT be allowed in the first Quiz ???) 0 1 1 0 1 And then collect the remainders in reverse order 1

  44. 11011001 00000000 11011001 00000000 00000000 00000000 Mapping it into memory is straightforward: Basic Data Types • 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: • 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!)

  45. “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” So, what are we going to do, Obi Wan?? We are all going to take a Vow: Basic Data Types (Repeat after me)

  46. OK – So now we understand that Integers on the PC are always stored on 32-bits and not 16! Basic Data Types 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 !!

  47. 00110000 00011100 00000000 00000000 11111111 11111111 Given 16-bits (2-bytes) we could store 216 = 65,536 pieces of information, or the integers 0 through 65,535. Basic Data Types How would this be stored?? The number 0 would be stored as: 2 locations in memory The number 65,535 would be stored as: 2 locations in memory A number such as 12,316 would be stored as: 2 locations in memory

  48. 00000000 00000000 00000000 00000000 11111111 11111111 11111111 11111111 00000000 00000000 00110000 00011100 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 Basic Data Types The number 0 would be stored as: 4 locations in memory The number 4,294,967,295 would be stored as: 4 locations in memory A number such as 12,316 would be stored as: 4 locations in memory

  49. 00110000 00011100 00000011 00011100 In C/C++ we would use the command: Basic Data Types unsigned intnumber; To reserve 16-bits (2-bytes) of RAM at location number unsigned intnumber= 12316; Would also store the value In location number in RAM Or we could enter the command: number = 796; To store the value at location number in RAM AFTER the initial declaration

  50. Why is the integer variable numberdeclared as unsigned??? Basic Data Types 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 non-negative???

More Related