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

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

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

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

Programming with Visual C++: Concepts and Projects

Chapter 3A: Integral Data (Concepts)

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

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

usesProgramming with Visual C++: Concepts and Projects 2

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

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 3

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

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 4

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

The Binary Number System (continued)

Programming with Visual C++: Concepts and Projects 5

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

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 6

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

The Binary Number System (continued)

Programming with Visual C++: Concepts and Projects 7

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

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 8

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

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 9

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

Integral Data Types (continued)

Programming with Visual C++: Concepts and Projects 10

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

Integral Data Types (continued)

Programming with Visual C++: Concepts and Projects 11

• 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

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

Integral Data Types (continued)

Programming with Visual C++: Concepts and Projects 12

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

Integral Data Types (continued)

Programming with Visual C++: Concepts and Projects 13

• 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

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

Integral Data Types (continued)

Programming with Visual C++: Concepts and Projects 14

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

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 15

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

Data Type Conversion (continued)

• Explicit type conversion methods– Unique to Visual C++• ToString()

• Convert methods

Programming with Visual C++: Concepts and Projects 16

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

Data Type Conversion (continued)

– Standard C++ • Typecasting

– (datatype) variable_name

Programming with Visual C++: Concepts and Projects 17

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

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 18

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

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 19

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

Data Type Conversion (conversion)

Programming with Visual C++: Concepts and Projects 20

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

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 21

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

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 22

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

Integer Arithmetic (continued)

Programming with Visual C++: Concepts and Projects 23

• 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

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

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 24

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

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 25

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

The Mod Operator (%) (continued)

Programming with Visual C++: Concepts and Projects 26

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

The Mod Operator (%) (continued)

Programming with Visual C++: Concepts and Projects 27

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

The Mod Operator (%) (continued)

Programming with Visual C++: Concepts and Projects 28

• The remainder of 35 % 25 is 10

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

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 29

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

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 30