Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programming with Visual C++: Concepts and Projects Chapter 3A: Integral Data (Concepts)

Similar presentations


Presentation on theme: "Programming with Visual C++: Concepts and Projects Chapter 3A: Integral Data (Concepts)"— 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 Projects2

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 Projects3

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 Projects4

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

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 Projects6

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

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 Projects8

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 Projects9

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

11 Integral Data Types (continued) Programming with Visual C++: Concepts and Projects11 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 Projects12

13 Integral Data Types (continued) Programming with Visual C++: Concepts and Projects13 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 Projects14

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 Projects15

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

17 Data Type Conversion (continued) – Standard C++ Typecasting – (datatype) variable_name Programming with Visual C++: Concepts and Projects17

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 Projects18

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 Projects19

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

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 Projects21

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 Projects22

23 Integer Arithmetic (continued) Programming with Visual C++: Concepts and Projects23 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 Projects24

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 Projects25

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

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

28 The Mod Operator (%) (continued) Programming with Visual C++: Concepts and Projects28 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 Projects29

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 Projects30


Download ppt "Programming with Visual C++: Concepts and Projects Chapter 3A: Integral Data (Concepts)"

Similar presentations


Ads by Google