1 / 30

Programming with Visual C++: Concepts and Projects

Learn about the binary representation of integers, converting data types, and integer division in Visual C++. Explore the similarities between integer and character data types.

jworley
Télécharger la présentation

Programming with Visual C++: Concepts and Projects

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. Programming with Visual C++: Concepts and Projects Chapter 3A: Integral Data (Concepts)

  2. Objectives In this chapter, you will: Learn about the binary representation of integers Discover similarities between integer and character data Learn about methods for converting data from one type to another Become familiar with integer division and its uses Programming with Visual C++: Concepts and Projects

  3. Objectives (continued) Learn to use the mod operator (%) Use the mod operator (%) and integer division to convert a character to its binary representation Programming with Visual C++: Concepts and Projects

  4. The Binary Number System We are used to counting in base-10 (the decimal number system) For integers in base 10 each placeholder represents the number of groups (0-9) of some power of 10 Programming with Visual C++: Concepts and Projects

  5. The Binary Number System (continued) Programming with Visual C++: Concepts and Projects

  6. The Binary Number System (continued) Computers use base-2 (the binary number system) For integers in base-2, each placeholder represents the number of groups (0-1) of some power of 2 Programming with Visual C++: Concepts and Projects

  7. The Binary Number System (continued) Programming with Visual C++: Concepts and Projects

  8. Integral Data Types • Integral data types are represented as binary integers • Boolean • example 0 = false, 1 = true • Character • Requires one byte for ASCII characters • example: 01000001 = ‘A’ • Integer • Requires 4 bytes for int data • example: 01000001 = 65 Programming with Visual C++: Concepts and Projects

  9. Integral Data Types (continued) • Different types require different amounts of storage • Boolean and character data may be assigned to an integer variable • Data should not be assigned to a type that requires less storage • Example: Assigning an integer to a char • The result is loss of information Programming with Visual C++: Concepts and Projects

  10. Integral Data Types (continued) Programming with Visual C++: Concepts and Projects

  11. Integral Data Types (continued) Programming with Visual C++: Concepts and Projects A single character (‘A’) may be assigned to an integer variable directly This is because the char data type is a subset of the int data type

  12. Integral Data Types (continued) Programming with Visual C++: Concepts and Projects

  13. Integral Data Types (continued) Programming with Visual C++: Concepts and Projects Although character data can be assigned to an integer variable, the reverse is not true A standard 32-bit integer would not necessarily fit into the 8-bits required to represent a single ASCII character Data loss can occur if this is allowed

  14. Integral Data Types (continued) Programming with Visual C++: Concepts and Projects

  15. Data Type Conversion Data often has to be converted from one type to another For example, the ToString() method was used in previous projects to transform an integer or double into a string of text An explicit type conversion is a statement that calls a method (like ToString() ) to convert data from one type to another Programming with Visual C++: Concepts and Projects

  16. Data Type Conversion (continued) • Explicit type conversion methods • Unique to Visual C++ • ToString() • Convert methods Programming with Visual C++: Concepts and Projects

  17. Data Type Conversion (continued) • Standard C++ • Typecasting • (datatype) variable_name Programming with Visual C++: Concepts and Projects

  18. Data Type Conversion (continued) • Implicit type conversion happens automatically • Involves data type promotion like and integer (int) being promoted to a double Programming with Visual C++: Concepts and Projects

  19. Data Type Conversion (continued) Data types may be arranged in order of number of bytes of storage they require Data of type requiring less storage can be assigned to variables with data types requiring more (promotion) It is unsafe to assign data to variable of a type that requires less storage (demotion) Programming with Visual C++: Concepts and Projects

  20. Data Type Conversion (conversion) Programming with Visual C++: Concepts and Projects

  21. Integer Arithmetic • Division operator (/) has several forms • Real number division • Integer division • The result of division is always a real number unless both operands are of integer data types (integer division) • The result of integer division • Is always an integer • Any remainder is dropped (truncated) • Example: 7 / 4 is the integer 1, not the real number 1.75 Programming with Visual C++: Concepts and Projects

  22. Integer Arithmetic (continued) Unless you know the data type of each variable in Example 3-6 you cannot know the result If change is an integer then change / 25 uses integer division If quarters is an integer then change must be as well Programming with Visual C++: Concepts and Projects

  23. Integer Arithmetic (continued) Programming with Visual C++: Concepts and Projects Integer division occurs when sum (478) is divided by 10. The result is 47 (NOT 47.8) The integer 47 is assigned to a double (average) as 47.0

  24. Integer Arithmetic (continued) • To avoid integer division when it is not appropriate • Convert either integer operand to a float or double before the division takes place • Convert methods will work for this • C typecasting will also work Programming with Visual C++: Concepts and Projects

  25. The Mod Operator (%) The mod operator stands for modulus It has nothing to do with percentages It yields the integer remainder from integer division Examples: 7 % 4 is 3, 6 % 6 is 0 Programming with Visual C++: Concepts and Projects

  26. The Mod Operator (%) (continued) Programming with Visual C++: Concepts and Projects

  27. The Mod Operator (%) (continued) Programming with Visual C++: Concepts and Projects

  28. The Mod Operator (%) (continued) Programming with Visual C++: Concepts and Projects The remainder of 35 % 25 is 10

  29. Summary • Integral types (bool, char and int) are all represented in the same way as binary numbers • Data of one data type may be converted to another data type in several ways • Explicit type conversion • Implicit type conversion • Data type promotion is safe • Data type demotion is unsafe Programming with Visual C++: Concepts and Projects

  30. Summary (continued) • Integer division is a special type of division • Both operands must be integers • The result is always an integer • Remainders are truncated • Type conversion can be used to override integer division • The mod operator (%) is used to capture the remainder from integer division Programming with Visual C++: Concepts and Projects

More Related