Presentation is loading. Please wait.

Presentation is loading. Please wait.

VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 3A Integral Data (Concepts)

Similar presentations


Presentation on theme: "VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 3A Integral Data (Concepts)"— Presentation transcript:

1 VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 3A Integral Data (Concepts)

2 Objectives Visual C++ Programming 2  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

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

4 The Binary Number System Visual C++ Programming 4  We are used to counting in base-10 (the decimal number system)  For integers in base 10 each placeholder represents a number of groups (0-9) of some power of 10  Computers use base-2 (the binary number system)  For integers in base-2 each placeholder represents a number of groups (0-1) of some power of 2

5 Visual C++ Programming 5

6 6

7 Integral Data Types Visual C++ Programming 7  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

8 Integral Data Types (continued) Visual C++ Programming 8  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

9 Visual C++ Programming 9

10 10

11 Visual C++ Programming 11

12 Visual C++ Programming 12

13 Data Type Conversion Visual C++ Programming 13 Explicit type conversion Unique to Visual C++ ToString() Convert methods Standard C++ Typecasting Implicit type conversion Happens automatically Involves data type promotion

14 Visual C++ Programming 14

15 Visual C++ Programming 15

16 Visual C++ Programming 16

17 Visual C++ Programming 17

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

19 Visual C++ Programming 19

20 Integer Arithmetic Visual C++ Programming 20  The 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

21 Visual C++ Programming 21

22 Visual C++ Programming 22

23 Integer Arithmetic (continued) Visual C++ Programming 23  To avoid integer division  Converting either integer operand to a float or double before the division takes place  Use Convert methods, or typecasting

24 Visual C++ Programming 24

25 The Mod Operator (%) Visual C++ Programming 25  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

26 Visual C++ Programming 26

27 Visual C++ Programming 27

28 Visual C++ Programming 28

29 Summary Visual C++ Programming 29 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

30 Summary (continued) Visual C++ Programming 30  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


Download ppt "VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 3A Integral Data (Concepts)"

Similar presentations


Ads by Google