Presentation is loading. Please wait.

Presentation is loading. Please wait.

Advanced Topics Object-Oriented Programming Using C++ Second Edition 13.

Similar presentations


Presentation on theme: "Advanced Topics Object-Oriented Programming Using C++ Second Edition 13."— Presentation transcript:

1 Advanced Topics Object-Oriented Programming Using C++ Second Edition 13

2 Objectives In this chapter, you will learn: About the binary system Why computers use the binary system How to use individual bits to store data How to combine bit fields to hold values larger than 1 How to convert a group of bit fields to an integer value 13

3 Objectives In this chapter, you will learn: How to use the logical bitwise AND operator with a mask How to use the logical bitwise OR operator How and why to shift bits About recursion How to use a recursive function to sort a list 13

4 Understanding the Binary System Every piece of data stored on every computer in the world is stored as a series of 0s and 1s This system of storage, called the binary system, has been used since the earliest computers were created because it is the cheapest way to store data The numbers you are accustomed to using are organized in the decimal system The decimal system is so named because the prefix “dec” means 10 and the decimal system uses 10 digits: 0 through 9 13

5 Representation of a Few Decimal Numbers 13

6 Understanding the Binary System Constructing values in the binary system works the same way as in the decimal system, except that you have only two symbols to use, so each successive column represents a value only two times greater than the column to its right You can construct any binary number’s decimal value by adding the column values of those columns that contain a 1 13

7 Representation of a Few Binary Numbers 13

8 Understanding Why Computers Use the Binary System Every computer system uses a code to represent values, although different systems use different codes Many personal computers use a system called ASCII (American Standard Code for Information Interchange), in which eight binary digits are used to represent a character Many mainframe computer systems use a separate code named EBCDIC (Extended Binary Coded Decimal Interchange Code), in which, for example, the character A is 11000001 13

9 Understanding Why Computers Use the Binary System Some C++ compilers also support Unicode, which is a 16-bit coding scheme Computer professionals use the term bit as shorthand when they refer to a single binary digit Eight binary digits are called a byte, which on many systems is the amount of storage required to store a single character such as an alphabetic letter or punctuation mark 13

10 Using Individual Bits to Store Data You have several options for storing the values 0 and 1 within fields in a class you create For example: –You can store 0 or 1 in a double field –You can store 0 or 1 in an integer field –You can store 0 or 1 in a single bit field When you examine the output in Figure 13-3, you see that the DriverUsingInts object requires 20 bytes of storage—four bytes for each of its five integer fields 13

11 Driver UsingInts and DriverUsingBits Classes 13

12 Using Individual Bits to Store Data 13

13 Using Individual Bits to Store Data In the steps shown on pages 494 to 496 of the textbook, you create a Driver class with two bit fields, and declare some objects that hold values in the fields 13

14 Combining Bit Fields to Hold Values Larger than 1 When you want to store a 0 or 1, you can use a single bit If you want to store larger values, you need more bits Table 13-2 shows that you need at least two bits of storage to represent the value 2 or 3 In the next set of steps referred to on pages 496 and 497 of the textbook, assume that you want to include a field within the Driver class you created earlier, and that you allow only three or fewer vehicles per Driver 13

15 Output of Driver2 Program 13

16 Converting a Group of Bit Fields to an Integer Value When you store 0s and 1s in the bit fields for an Employee object, you can think of them as individual fields or as a single unit 13

17 Representation of Employee with Three Selected Attributes 13

18 The Employee Class 13

19 Converting a Group of Bit Fields to an Integer Value Figure 13-9 contains a main() program that uses the Employee class shown in Figure 13-8 An array of Employee objects is declared and, in a for loop, each Employee is assigned values from the keyboard 13

20 main() Program Using the Employee Class 13

21 Output of a Typical Run of the Employee Program 13

22 Using the Logical Bitwise AND Operator with a Mask Comparing an object’s bit fields to a decimal value is efficient when you want to find objects with a specific stored pattern C++ provides you with a special set of operators, called bitwise operators, that you can use in situations like the one just described Bitwise operators let you manipulate individual bits of integer or character values One such operator is the logical bitwise AND operator, which is written as a single ampersand (&) 13

23 Using the Logical Bitwise AND Operator with a Mask 13

24 Using the Logical Bitwise AND Operator with a Mask A mask is a value whose only purpose is to filter values from other variables Figure 13-3 shows three possible Employee objects and the result that ensues when you mask their bit values with an insuranceMask set to 00001110 13

25 Using the Logical Bitwise AND Operator with a Mask 13

26 Using the Logical Bitwise AND Operator with a Mask In the program in Figure 13-14, using the mask improves program efficiency; you can select records of Employees who take all three types of insurance by making a single logical comparison 13

27 Using the Logical Bitwise AND Operator with a Mask Figure 13-15 shows a typical run of the program 13

28 Using the Logical Bitwise OR Operator The logical bitwise OR operator (|) compares the bits of its two operands, and produces a result in which each bit is a 1 if either of the operand’s corresponding bits is a 1 Figure 13-16 shows an example that uses the logical bitwise OR operator One way to use the logical bitwise OR is to turn on specific bits within an object 13

29 Using the Logical Bitwise OR Operator 13

30 Using the Logical Bitwise OR Operator 13

31 Using the Logical Bitwise OR Operator Figure 13-18 shows a function you can add to the Employee class to assign an unsigned integer’s bit values to an Employee’s bit fields 13

32 Using the Logical Bitwise OR Operator The program in Figure 13-19 establishes a newBenefitMask with a value of 66 13

33 Output of a Typical Execution of Employee3 Program 13

34 Shifting Bits You can use the bitwise left shift operator ( >) to shift bits to the right For example, Figure 13-21 shows the bit representation of a byte, and the results after shifting one bit to the left, equivalent of using the expression result = value <<1; 13

35 Shifting Bits The program in Figure 13-22 shows two ways of multiplying 5 by 2 to the third power In the first part of the program, a variable named num is set to 5, and a loop is executed to multiply 2 by 2 by 2 to determine 2 to the third power before multiplying by 5 and displaying the answer In the second part of the program, num is reset to 5 and the bitwise left shift operator performs the same task in one statement 13

36 Shifting Bits 13

37 Understanding Recursion A function that calls itself a recursive function Many programming languages do not allow a function to call itself, but C++ programmers can use recursion to produce some interesting effects Figure 13-24 shows a simple example of recursion When the main() program in Figure 13-24 calls the infinity() function, the stack receives the address to which the program should return within main(), but this program never returns to main() 13

38 Shifting Bits 13

39 Shifting Bits Figure 13-26 shows a program that calls a recursive function which computes the sum of every integer, from 1 up to and including the function’s argument value 13

40 Output of Recursion2 Program 13

41 Nonrecursive Program that Computes a Sum 13

42 Using a Recursive Function to Sort a List It is possible to quickly sort a list using a recursive sorting function To use this sorting method, you employ a “divide and conquer” technique in which you select a point within a list that represents a middle position, and then divide the list into two sublists Then you swap the positions of pairs of values until all the values in the first sublist are less than the value in the middle position, and all the values in the second sublist are more than the value in the middle position Subsequently, each sublist is divided in half and rearranged so all the low values are in one sublist and all the high values are in the other 13

43 Using a Recursive Function to Sort a List 13

44 Using a Recursive Function to Sort a List Figure 13-31 shows the splitList() function The sort() function in Figure 13-32 is the recursive function It accepts a list of numbers and a start and stop position 13

45 Using a Recursive Function to Sort a List 13

46 Using a Recursive Function to Sort a List In the steps outlined on pages 520 and 521 of the textbook, you create a recursive drawing function so you can experiment with the way recursion works 13

47 Output of a Typical Run of the DrawingRecursion Program 13

48 Summary Every piece of data stored on a computer uses the binary system The only digits used are 0 and 1 Computers use the binary system because two-way switches are the cheapest form of storage; most systems store eight bits in a byte You can use a single bit for a field that holds a 0 or 1 13

49 Summary When you want to store a small value that requires more storage space than a bit, but not as much as a byte or an integer, you increase the number following the colon in the declaration of the bit field When you store 0s and 1s in the bit fields for an object, you can think of them as individual fields, or you can think of them as a single unit and assign the binary numbering system column values to each column 13

50 Summary C++ provides you with bitwise operators that let you manipulate individual bits of integer or character values The logical bitwise OR operator (|) compares the bits of its two operands and produces a result in which each bit is a 1 if either of the operand’s corresponding bits is a 1 13

51 Summary You can use the bitwise left shift operator ( >) to shift bits to the right A function that calls itself is a recursive function It is possible to quickly sort a list using a recursive sorting function 13


Download ppt "Advanced Topics Object-Oriented Programming Using C++ Second Edition 13."

Similar presentations


Ads by Google