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

Slides:



Advertisements
Similar presentations
Assembly Language for Intel-Based Computers, 4 th Edition Chapter 1: Basic Concepts (c) Pearson Education, All rights reserved. You may modify and.
Advertisements

1 Chapter 2 The Digital World. 2 Digital Data Representation.
Lecture - 2 Number systems and computer data formats
1 Review Chpt 2 and 3 Five components of a computer Input Output Memory Arithmetic & Logic Unit (ALU) Control Two types of information Data and instruction.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design First Edition by Tony Gaddis.
TK 2633 Microprocessor & Interfacing Lecture 3: Introduction to 8085 Assembly Language Programming (2) 1 Prepared By: Associate Prof. Dr Masri Ayob.
Room: E-3-31 Phone: Dr Masri Ayob TK 2633 Microprocessor & Interfacing Lecture 1: Introduction to 8085 Assembly Language.
Understanding Arrays and Pointers Object-Oriented Programming Using C++ Second Edition 3.
Data types and variables
Chapter 2 Data Types, Declarations, and Displays
C++ for Engineers and Scientists Third Edition
JavaScript, Third Edition
Programming Logic and Design Fourth Edition, Comprehensive
Data Representation in Computers
Bit Operations C is well suited to system programming because it contains operators that can manipulate data at the bit level –Example: The Internet requires.
A bit can have one of two values: 0 or 1. The C language provides four operators that can be used to perform bitwise operations on the individual bits.
Data Representation (in computer system) Computer Fundamental CIM2460 Bavy LI.
VB .NET Programming Fundamentals
CODING SYSTEMS CODING SYSTEMS CODING SYSTEMS. CHARACTERS CHARACTERS digits: 0 – 9 (numeric characters) letters: alphabetic characters punctuation marks:
1 Chapter Two Using Data. 2 Objectives Learn about variable types and how to declare variables Learn how to display variable values Learn about the integral.
Objectives You should be able to describe: Data Types
Programmable Logic Controllers
Lecture 6 Topics Character codes Error Detection and Correction
Computers Organization & Assembly Language
1 Overloading Operators Object-Oriented Programming Using C++ Second Edition 8.
CSU0014 Assembly Languages Homepage: Textbook: Kip R. Irvine, Assembly Language for Intel-Based Computers,
Summer 2014 Chapter 1: Basic Concepts. Irvine, Kip R. Assembly Language for Intel-Based Computers 6/e, Chapter Overview Welcome to Assembly Language.
Assembly Language for x86 Processors 7th Edition
Input & Output: Console
Array Processing Simple Program Design Third Edition A Step-by-Step Approach 7.
Chapter 2 Computer Hardware
Lec 3: Data Representation Computer Organization & Assembly Language Programming.
Cis303a_chapt03-2a.ppt Range Overflow Fixed length of bits to hold numeric data Can hold a maximum positive number (unsigned) X X X X X X X X X X X X X.
Introduction to Computer Design CMPT 150 Section: D Ch. 1 Digital Computers and Information CMPT 150, Chapter 1, Tariq Nuruddin, Fall 06, SFU 1.
1 INFORMATION IN DIGITAL DEVICES. 2 Digital Devices Most computers today are composed of digital devices. –Process electrical signals. –Can only have.
Logic Design Dr. Yosry A. Azzam. Binary systems Chapter 1.
1 Pertemuan 2 Network Math. Discussion Topics Binary presentation of data Bits and bytes Base 10 number system Base 2 number system Converting decimal.
CH2 – Using Data. Constant Something which cannot be changed Data Type Format and size of a data item Intrinsic Data Types Pg. 47 – Table 2-1 Basic ones.
Chapter 19 Number Systems. Irvine, Kip R. Assembly Language for Intel-Based Computers, Translating Languages English: Display the sum of A times.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals. Procedural Constructs.
Agenda ASCII char N int Character class char N String Bitwise operators homework.
EEL 3801C EEL 3801 Part I Computing Basics. EEL 3801C Data Representation Digital computers are binary in nature. They operate only on 0’s and 1’s. Everything.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals. Procedural Constructs -
Data Representation, Number Systems and Base Conversions
Data Representation. What is data? Data is information that has been translated into a form that is more convenient to process As information take different.
Characters and Strings
Information Coding Schemes Group Member : Yvonne Tiffany Jurifah bt Junaidi Clara Jane George.
Windows Programming Lecture 06. Data Types Classification Data types are classified in two categories that is, – those data types which stores decimal.
OPERATORS IN C CHAPTER 3. Expressions can be built up from literals, variables and operators. The operators define how the variables and literals in the.
CHAPTER 3 BINARY NUMBER SYSTEM. Computers are electronic machines which operate using binary logic. These devices use two different values to represent.
Number Systems. The position of each digit in a weighted number system is assigned a weight based on the base or radix of the system. The radix of decimal.
Lecture Coding Schemes. Representing Data English language uses 26 symbols to represent an idea Different sets of bit patterns have been designed to represent.
© 2004 Pearson Addison-Wesley. All rights reserved August 27, 2007 Primitive Data Types ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
Nat 4/5 Computing Science Data Representation Lesson 3: Storing Text
Programmable Logic Controller
Chapter 3 - Binary Numbering System
Programming and Data Structure
CSCI 198: Lecture 4: Data Representation
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
Chapter 3 Data Storage.
CSCI 161: Lecture 4: Data Representation
Chapter 2 Bits, Data Types & Operations Integer Representation
Ch2: Data Representation
Numbering System TODAY AND TOMORROW 11th Edition
How Computers Store Data
Chapter 3 DataStorage Foundations of Computer Science ã Cengage Learning.
Using C++ Arithmetic Operators and Control Structures
Chapter 3 - Binary Numbering System
CHAPTER 69 NUMBER SYSTEMS AND CODES
Presentation transcript:

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

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

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

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

Representation of a Few Decimal Numbers 13

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

Representation of a Few Binary Numbers 13

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

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

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

Driver UsingInts and DriverUsingBits Classes 13

Using Individual Bits to Store Data 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

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

Output of Driver2 Program 13

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

Representation of Employee with Three Selected Attributes 13

The Employee Class 13

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

main() Program Using the Employee Class 13

Output of a Typical Run of the Employee Program 13

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

Using the Logical Bitwise AND Operator with a Mask 13

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

Using the Logical Bitwise AND Operator with a Mask 13

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

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

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 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

Using the Logical Bitwise OR Operator 13

Using the Logical Bitwise OR Operator 13

Using the Logical Bitwise OR Operator Figure 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

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

Output of a Typical Execution of Employee3 Program 13

Shifting Bits You can use the bitwise left shift operator ( >) to shift bits to the right For example, Figure 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

Shifting Bits The program in Figure 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

Shifting Bits 13

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 shows a simple example of recursion When the main() program in Figure 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

Shifting Bits 13

Shifting Bits Figure 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

Output of Recursion2 Program 13

Nonrecursive Program that Computes a Sum 13

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

Using a Recursive Function to Sort a List 13

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

Using a Recursive Function to Sort a List 13

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

Output of a Typical Run of the DrawingRecursion Program 13

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

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

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

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