0 Bewertungen0% fanden dieses Dokument nützlich (0 Abstimmungen)

1 Ansichten112 SeitenSep 09, 2016

Lec01-ENGR1200-KH.pdf

© © All Rights Reserved

PDF, TXT oder online auf Scribd lesen

© All Rights Reserved

Als PDF, TXT **herunterladen** oder online auf Scribd lesen

0 Bewertungen0% fanden dieses Dokument nützlich (0 Abstimmungen)

1 Ansichten112 SeitenLec01-ENGR1200-KH.pdf

© All Rights Reserved

Als PDF, TXT **herunterladen** oder online auf Scribd lesen

Sie sind auf Seite 1von 112

Lecture 01

1

Copyright 2014 Pearson Education, Inc. Computer Security: Principles and Practice, 3/e, by William Stallings and Lawrie Brown

Objectives

Introduce computing and engineering problem solving, including:

A brief history

A discussion of Numbering Systems

A five-step problem-solving methodology

Historical Perspective

What is a computer?

Historical Perspective

(1815-1852, below)

wrote the first

computer program.

Charles Babbage,

(1792-1871, above)

designed the

Analytical Engine

(left) to process

decimal numbers.

Historical Perspective

Charles Babbage, Esq. 1792-1871

English mathematician

Analytical Engine:

Performs only simple mathematical operations

Designed to process base ten numbers.

Consisted of four parts:

Storage unit (memory)

Processing unit (computation unit)

Input device (punched card reader)

Output device (punched and printed output)

5

Historical Perspective

Analytical Engine:

Luigi F. Menabrea, French engineer and mathematician, described

Babbages vision of a machine capable of solving any problem using:

Inputs

Outputs

Published a paper Sketch of the Analytical Engine Invented by Charges

Babbage, Esq

Historical Perspective

Augusta Ada Byron 1815-1852

Wrote the English translation of Menabreas Sketch of the Analytical

Engine

Envisioned the multidisciplinary potential of the Analytical Engine

Wrote detailed instructions for performing numerical computations using

the Analytical Engine

Historical Perspective

Digital Computers

ABC (Atanasoff Berry Computer)

Developed at Iowa State University between 1939 and 1942 by John

Atanasoff and Clifford Berry.

Weighed 700 pounds.

Executed one instruction every 15 seconds.

ENIAC (Electronic Numerical Integrator And Calculator)

Developed by research team lead by John Mauchly and J. Presper

Eckert during the early 1940s

Consisted of 18,000 vacuum tubes and 1500 relays

Weight: 30 tons, 6000 switches, many jumper cables

Consumed: 140 kilowatts of power

Executed hundreds of instructions every second

8

Historical Perspective

ENIAC vs. Intel Pentium 4

Todays

processors

execute trillions

of operations per

second and

weigh ounces.

ENIAC executes hundreds of operations per second (30 tons)

9

Advances of the digital computer has facilitated many significant engineering

achievements over the past five decades

Achievements illustrate how engineering has improved our lives and

expanded possibilities of the future

Some of the major achievements

Exploration of space

1996: Mars Global Surveyor: a spacecraft developed by NASA

for collecting scientific data while in orbit

Application Satellites

Used to provide weather information, relay communication

signals, maps, and environmental updates. Example: GPS

Computer Simulations

Allow for experimentation that may not be possible (due to size,

speed, dangers to health and safety, etc)

10

What Is Programming?

Many people use computers for everyday tasks such as electronic banking

or writing a term paper

Computers are good for such tasks

C++ is a powerful computer programming language thats appropriate for

technically oriented people to use in building substantial information

systems

In use today are more than a billion general-purpose computers and

billions more cell phones, smartphones and handheld devices (such as

tablet computers)

The number of mobile Internet users will reach approximately 150 million

by 2015

The smartphone applications market is exceeded $40 billion, which is

creating significant opportunities for programming mobile applications

11

What Is Programming?

Computers can carry out a wide range of tasks because they execute

different programs, each of which directs the computer to work on a

specific task

The computer itself is a machine that stores data (numbers, words,

pictures), interacts with devices (the monitor, the sound system, the

printer), and executes programs

A computer program tells a computer, in minute detail, the sequence of

steps that are needed to fulfill a task

A computing system is a complete working system that includes:

Hardware: The physical computer and peripheral devices are

collectively called the hardware

Software: The programs the computer executes are called the

software

Programming: is the act of designing and implementing computer

programs

12

Computer Hardware

von Neumann Computing Model:

In 1946, John von Neumann proposed a computing model that is still used

today in the design of most digital computers

Hardware refers to the physical

parts off the computing system that

have mass (i.e. they can actually be

touched):

Computer

Display

Mouse

Printer

The von Neumann Computing Model

13

Computer Hardware

von Neumann Computing Model:

Input device(s)

Output device(s)

Memory Unit

CPU (Central Processing

Unit) consisting of:

Control Unit

ALU (Arithmetic Logic Unit)

14

Computer Hardware

15

Computer Hardware

The CPU (central processing unit)

Heart of the computer

Executes one operation at a time

Performs program control and data processing

Carries out arithmetic operations such as addition, subtraction,

multiplication, and division

Fetches data from external memory or devices and stores data back

All data must travel through the CPU whenever it is moved from one

location to another

16

Computer Hardware

Arithmetic Logic Unit (ALU)

Performs arithmetic and logic operations

ALU is a fundamental block for CPU of a computer

All CPUs and Graphical Processing Units (GPUs) all have complex

ALUs

Accumulator: used for temporary storage of values and results

Memory

The computer stores data and programs in memory

Primary memory - memory chips

Random access memory (RAM) (read-write memory)

Read-only memory (ROM)

Disk drives

DVDs

17

Computer Hardware

Control Unit

Controls the transfer and processing of data

Retrieves and interprets instructions stored in memory

Accepts data from an input device (i.e. keyboard or mouse),

Sends data to a specified output device (i.e. printer or display)

Stores data in the memory unit

Peripheral Devices

The computer transmits information (called output) to the user

through a display screen, speakers, and printers

The user can enter information (called input) for the computer by

using a keyboard or a pointing device such as a mouse

18

Computer Software

Refers to programs that reside and execute electronically on the hardware.

Compilers: Translate source code

Operating systems: Provide the HCI (Human Computer Interface)

Application programs: Provide problem solutions, written by using a

computer language

19

Computer Software

Operating systems:

Operating Systems typically comes with the computer hardware when

purchased

OS provides an interface between the user and the hardware (allows

user to select and execute software)

OS contain a group of programs called utilities that allow the user to

perform functions such as printing files, copying files, etc

OS simplify the interface with the OS by using icons and menus

(Examples: Windows)

20

Computer Software

Application Software:

Common Applications

Purpose: Perform common operations

Examples: word processors, text editors, spreadsheet

Database management software

Purpose: Efficiently store and retrieve larges amounts of data

Examples: MySQL, Oracle, Microsoft SQL

Computer Aided Design (CAD)

Purpose: Define objects and manipulate them graphically

Examples: AutoCAD, Architectural Desktop, Civil 3D

Mathematical Computation Software

Purpose: Powerful mathematical commands & generate graphs

Examples: Matlab, Mathematica, Maple

21

Computer Software

Computer Languages:

Computer languages can be described in terms of levels

Programmers write instructions in various programming languages,

some directly understandable by computers and others requiring

intermediate translation steps.

These may be divided into three general types:

Machine languages

Assembly languages

High-level languages

22

Computer Software

Computer Languages:

Low-level languages (or machine languages)

Most primitive and are tied closely to the design of the hardware

(instructions are encoded)

Generally consist of strings of numbers (ultimately reduced to 1s

and 0s)

Machine dependenta particular machine language can be used

on only one type of computer

Machine language differs from one machine to another (i.e. Sun

versus HP computer)

23

Computer Software

Computer Languages:

Assembly language

Instructions are written in symbolic statements instead of binary

Also unique to a specific computer design

Usually have limited set of statements

Makes task of writing programs very tedious

Translator programs called assemblers convert assemblylanguage programs to machine language

24

Computer Software

Computer Languages:

High-level language

Have English-like commands and instructions

Examples: C, C++, Java, VB.NET, C#, etc

Independent of the processor type and hardware

Writing programs in high-level language is much easier than

machine or assembly

Contain large number of commands and an extensive set of

syntax (or grammar) rules

25

Building a Program

Computers only understand machine language

High-level languages like C++ must be translated to machine language for

execution

Compiler

A special computer program that translates higher-level programs into

machine instructions for a particular processor

Key Terms

Source Program: printable/Readable Program file

Object Program: nonprintable machine readable file

Executable Program: nonprintable executable code

26

Building a Program

Errors in Programs:

Syntax/Parse Errors

Mistakes with the language

Always reported by the compiler

Linking Errors

Missing pieces prevent the final assembly of an executable

program

Run-time Errors

Occur when program is executing

May or may not be reported

27

Building a Program

Logic Errors

Can be difficult to find

Debugging can be time consuming

Better tools for finding bugs

It is important to carefully check the output of your programs for

errors

Even programs that appear to work correctly may have bugs!

Debugging

Process of eliminating logic errors (i.e. bugs) from programs

User-friendly programming environments such as Microsoft Visual

C++ integrate the compiler with

text processors and code editors

special tools to help find bugs in programs (debugger)

testing tools

and much more

28

The Programming Environment

You will need to know how to log in (if needed), and, how to start your

C++ development environment

An IDE (integrated development environment) is where you will most

likely work

Use an editor to type your C++ programs into the IDE

Your program is called a source program (file)

29

The Programming Environment

You will need to learn how to compile and run your program in the IDE

on behind the

scenes in the IDE

that you dont

normally see

30

The Programming Environment

You will need to know your computers file system: files, folders, and

extensions

C++ program files typically have the extension: .cpp (or .cc or .cxx or

even .C)

You should be organized by creating folders for organizing your files

The following IDEs are available:

Microsoft Visual Studio (C++)

NetBeans

Eclipse

Dev C++

CodeLite

31

The Programming Environment

The compiler translates C++ programs into machine code

The linker combines machine code with library code into an executable

program

32

The Edit-Compile-Run Loop

33

34

While performing arithmetic, one usually gives little thought on the question:

How many decimal digits it takes to represent a number?

Physicists can calculate that there are 1078 electrons in the universe without

being bothered by the fact that it requires 79 decimal digits to write that

number out in full

Example

Calculating value of a function to six/seven/eight significant digits (using

pencil and paper)

The problem of the paper not being wide enough for seven-eight digit

numbers never arises!

35

Finite-Precision Numbers

With computers, its quite different!

On most computers, amount of memory available for storing a number is

fixed at the time that the computer is designed

With a certain amount of effort, programmer can represent numbers two,

or three or even many times larger than this fixed amount

Challenge: fixed amount of memory

Finite nature of the computer forces us to deal only with numbers that can

be represented in a fixed number of digits finite-precision numbers

Example: set of positive integers representable by three decimal digits

(no decimal or sign)

Set has exactly 1000 members: 000, 001, 002, , 999

Since computers have finite memories (then it must perform arithmetic on

finite-precision numbers), the results of certain calculations will be just

plain wrong (from a mathematical perspective)

Algebra of finite-precision numbers is different from normal algebra

36

Finite-Precision Numbers

Associative Law Example

Associative law (can group numbers in any way without changing the

answer)

a + (b - c) = (a + b) c

for a = 700, b = 400, c = 300

Left side: 800

right side: overflow

associative law does not hold (overflow in the infinite arithmetic of threedigit integers)

37

Finite-Precision Numbers

Distributive Law Example

Distribution law (distribute pieces as you separate or break it into parts)

a (b c) = a b a c

for a = 5, b = 210, c = 195

Left side: 5 x 15=75

right side: overflow

a b causes an overflow: 5 x 210 = 1050

distributive law does not hold (overflow in the infinite arithmetic of threedigit integers)

Conclusion: Are computers suitable for doing arithmetic? Yes

38

Why binary

Original computers were mainly designed to perform calculations (i.e.

calculator)

Designers used electronic components available at the time

Designers soon realized they could use a simple coding system (the binary

system) to represent numbers

All information inside a computer can be represented using binary system:

Numbers

00110101001001001010101111101110

Letters of alphabet

10101011111011100011010100100100

Microprocessor instructions

11000110110101011111001001001010

Graphics/Video, Sound,

Arithmetic used by computers differs in some ways from the arithmetic

used by people:

Computers perform operations on finite precision numbers

Computers use the binary system, not decimal, for representing

numbers

39

Binary System:

Digital computers store information as a sequence of bits (binary digits).

The value or state of a bit at any given time can be 0 or 1 (off or on).

Data is stored as a sequence of bytes.

A nibble is a sequence of 4 bits.

A byte is a sequence of 8 bits.

Example

The right most bit of a byte represents the ones position

The next bit represents the twos position

The next bit represents the fours position

The left most bit represents 2n-1

40

Binary System:

The number of words available in memory is referred to as the memory

space, or address space

Address

Sixteen Bit Word

Memory Diagram:

000

0000101011011101

Address Space 8

001

1010001011010100

Word Size

16

010

1011010010100101

Double Word 32

011

0101001101010101

Quad Word 64

010

0101000111001110

010

1100110000111010

110

0100011101001001

111

0101110001001000

Right most bit (RMB) is referred to as the least significant bit (LSB).

Left most bit (LMB) is referred to as the most significant bit (MSB).

Value stored at address 000 is 00001010110111012 = 278110

what does it represent?

41

Numbering Systems:

Ordinary decimal number consists of a string of decimal digits and,

possibly, a decimal point

The general form and its usual representation are shown below

In general: radix k number system requires k different symbols to

represent the digits 0 to k -1

because we are using decimal, or base 10 numbers

Zero is always the first digit of any base

When 1 is added to the largest digit, a sum of zero and a carry of one

results

42

Numbering Systems:

When dealing with computers, it is frequently convenient to use radices

other than 10

Most important radices are

2 (binary)

8 (octal)

16 (hexadecimal)

Binary numbers are built-up from two binary digits

01

Octal numbers are built-up from the eight octal digits

01234567

Hexadecimal numbers are built-up from 16 digits

0 12 34 56 78 9ABC D E F

43

Numbering Systems:

Decimal number 2001 expressed in binary, octal, and hexadecimal

44

Numbering Systems:

Decimal numbers and their binary, octal, and hexadecimal equivalents

45

Numbering Systems:

Base ten number system

Ten decimal digits (0,1,2,3,4,5,6,7,8,9)

Each digit multiplies a power of ten

Example: 24510 = 2*102 + 4*101 + 5*100

Two binary digits (0,1)

Each digit multiplies a power of two

Example:

= 1*16 + 0*8 + 1*4 + 1*2 + 0*1

= 16 + 0 + 4 + 2 + 0

= 2210

46

Numbering Systems:

Base eight number system

Eight octal digits (0,1,2,3,4,5,6,7)

Each digit multiplies a power of eight

Example:

= 2*64 + 4*8 + 5*1

= 128 + 32 + 5

= 1658

47

Numbering Systems:

Base sixteen number system

Sixteen hex digits (0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F)

Each digit multiplies a power of sixteen

Example:

= 2*256 + F*16 + B*1

= 512 + 240 + 11

= 76310

48

Numbering Systems:

Practice with Number Systems

1002 = ?8

37168 = ?2

1101001112 = ?10

3A1B16 = ?2

49

Conversion From One Radix to Another:

Each octal digit can be represented as a group of 3-bit binary number

Each hexadecimal digit can be represented as a group of 4-bit binary

number

50

Conversion From Binary to Decimal:

Binary numbers can be converted to decimal by summing up the powers

of 2 corresponding to the 1 bits in the number

1 0 1 1 0 24 + 22 + 21 = 16 + 4 + 2 = 22

Repeated Subtraction

Repeated Division

Both methods produce the same result and you should use whichever

method you are most comfortable with

51

Conversion From Decimal to Binary: Repeated Subtraction

Largest power of 2 smaller than the number is subtracted from the number

STEP 1: Write down all of the binary place values in order until you get to the

first binary place value that is GREATER THAN the decimal number you are

trying to convert. Example: 485

512

256

128

64

32

16

8

4

2

1

STEP 2: Mark out the largest place value (this will tell us how many place

values we will need)

512

256

128

64

32

16

8

4

2

STEP 3: Subtract the largest place value from the decimal number. Then, place a

1 under the place value

STEP 4: Repeat step 3 until all of the place values are processed: 485 256 = 229

256

128

64

32

16

8

4

2

1

52

1

Conversion From Decimal to Binary: Repeated Subtraction

256

128

1

229 128 = 101

256

128

1

1

101 64 = 37

256

128

1

1

37 32 = 5

256

128

1

1

5 16 = X

256

128

1

1

..

256

1

128

1

64

32

16

64

32

16

64

1

32

16

64

1

32

1

16

64

1

32

1

16

0

64

1

32

1

16

0

8

0

4

1

2

0

1

1

53

Conversion From Decimal to Binary:

Repeated Division for integers only

Dividing the number by 2

The quotient is written directly beneath the original number and the

remainder, 0 or 1, is written next to the quotient

The quotient is then considered and the process is repeated until the

number 0 has been reached

54

Conversion From Decimal to Binary:

Repeated Division for integers only

STEP 1: Divide the decimal by 2 in a regular long division until you

have a final remainder

STEP 2: Use the remainder as the LEAST SIGNIFICANT DIGIT of

the binary number

STEP 3: Divide the quotient from the first division until you have final

remainder

STEP 4: Use the remainder as the next digit of the binary number

STEP 5: Repeat steps 3 and 4 as many times as necessary until you

have a quotient that can not be divided by 2

STEP 6: Use the remainder (one you cant divide by 2) as the MOST

SIGNIFICANT DIGIT

55

Conversion From Decimal to Binary:

Repeated Division for integers only

Performing the same example of 485

485 / 2

242 / 2

121 / 2

60 / 2

30 / 2

15 / 2

7 /2

3 /2

1 /2

= 242

= 121

= 60

= 30

= 15

=7

=3

=1

=X

rem 1

rem 0

rem 1

rem 0

rem 0

rem 1

rem 1

rem 1

rem 1

111100101

56

Practice Exercises

Textbook: Page 20:

Practice exercises 1-10

Textbook: Page 22:

Practice exercises 1-10

Textbook: Page 32

Practice exercises 10-13

57

58

Octal Numbers

Octal number system is a base 8 number system

There are 8 symbols: 0 1 2 3 4 5 6 7

Each place value in an octal number is a power of 8

Each group of 3 binary digits can be represented by a single octal digit

Conversion From Binary Numbers to Octal:

Start with the LEAST SIGNIFICANT digit, mark out the digits in

groups of 3

Convert each group of three digits to its octal digit (or symbol)

Example

1 0 1 1 1 0 1 1 1

5

6

7

left can have

anywhere from 1 to 3

binary digit group

59

Octal Numbers

Conversion From Octal Numbers to Binary:

Converting octal numbers to binary is just the reverse operation of

converting binary to octal

Simply convert each octal digit to its three-bit binary pattern. The

resulting set of 1s and 0s is the binary equivalent of the octal number

Example

5

6

7

1 0 1 1 1 0 1 1 1

60

Octal Numbers

Conversion From Decimal Numbers to Octal: two methods:

Do a decimal-to-binary conversion, and then a binary-to-octal

conversion

Do a direct conversion using the repeated division method

Since this is a conversion to octal, 8 is the divisor each time

Example: Performing the same example of 485

485 / 8

= 60

rem 5

Least Significant Digit

60 / 8

=7

rem 4

7 /8

=X

rem 7

Most Significant Digit

=745

Since we are dividing by 8 in the repeated division, you may end up

with remainders of anywhere from 0 to 7

61

Octal Numbers

Example:

Given octal number 7501, what is the equivalent decimal value?

83

512

82

64

81

8

80

1

62

Octal Numbers

Conversion From Octal Numbers to Decimal:

Multiply each digit of the octal number by its place value and add the

results

Example: 7501

7 x 512

5 x 64

0x 8

1x 1

= 3584

= 320

=

0

= + 1

3905

63

Hexadecimal Numbers

Prefix hexa stands for 6 and the prefix deci stands for 10

Hexadecimal number system is a base 16 number system

There are 16 symbols: 0 1 2 3 4 5 6 7 8 9 A B C D E F

Each place value in a hexadecimal number is a power of 16

Each group of 4 binary digits can be represented by a single

hexadecimal digit

64

Hexadecimal Numbers

Conversion From Binary Numbers to Hexadecimal:

Start with the LEAST SIGNIFICANT digit, mark out the digits in

groups of 4

Convert each group of four digits to is hexadecimal digit (or symbol)

Example

The last group

1 0 1 1 1 0 1 1 1

on the left can

1

7

7

have anywhere

from 1 to 4

binary digit

group

65

Hexadecimal Numbers

Conversion From Hexadecimal Numbers to Binary:

Converting hexadecimal numbers to binary is just the reverse operation

of converting binary to hexadecimal

Simply convert each hexadecimal digit to its four-bit binary pattern.

The resulting set of 1s and 0s is the binary equivalent of the

hexadecimal number

Example: Given hexadecimal number 2EC3, what is the equivalent

decimal value?

163

162

161

160

4096

256

16

3

66

Hexadecimal Numbers

Conversion From Hexadecimal Numbers to Decimal:

Multiply each digit of the hexadecimal number by its place value and

add the results

Example: AB87

10 x 4096

11 x 256

8 x 16

7 x 1

= 40960

= 2816

=

128

= + 7

43911

67

Electronic Prefixes

There are a set of terms used in electronics which are used to represent

different powers of ten

There is also a set of terms used to represent large whole numbers and a set

of terms used to represent small fractional numbers

megabyte (MB)=220= 1,048,576 bytes

kilobyte (KB)=210=1,024 bytes

68

Data Types and Storage

When data is represented in memory, it is represented as a sequence of bits

This sequence may represent:

An instruction,

A numeric value,

A character,

A portion of an image, digital signal, or some other type of data

69

Data Types and Storage: Code for Letters and Symbols:

Computers use a standard binary code to represent letters of the alphabet,

numerals, punctuation marks, and other special characters

The code is referred to as American National Standards Institute (ANSI)

Based on a previous encoding scheme called (ASCII) American

Standard Code for Information Interchange

Currently, there are 256 code combinations in the ANSI format

ASCII had 128 code combinations

70

71

Data Types and Storage: Code for Letters and Symbols:

Examples of ASCII Representation:

Character

_________

Code

_____

HEX

____

0100 0010

42

0110 0010

62

&

0010 0110

26

0000 1000

[BACKSPACE]

72

Data Types and Storage:

Two basic data types:

integer

floating

point

73

Data Types and Storage: Integer

Integer data is often stored in memory using 4 bytes, or 32 bits

Left most bit is reserved for the sign of the number

Remaining 31 bits represent the magnitude of the number

Representation of data affects the efficiency of arithmetic and logic

operations

For efficiency, negative integers are often represented in their 2s

complement form

The 2s complement of an integer is formed by negating all of the bits

and adding one

74

Data Types and Storage: Integer

Ones Complement

Has a sign bit with 0 used for plus and 1 for minus.

Switch all 0s to 1s and 1s to 0s

Binary #

10110011

1s complement

01001100

75

Data Types and Storage: Integer

Twos Complement

Has a sign bit that is 0 for plus and 1 for minus.

To negate a number, it is done in a two step process:

Each 1 is replaced by a 0 and each 0 is replaced by a 1 (just as in the

ones complement)

1 is added to the result.

Binary addition is the same as decimal addition except that a carry

is generated if the sum is greater than 1 rather than greater than 9

Example:

0 0 0 0 0 1 1 0 (+6)

1 1 1 1 1 0 0 1 (-6 in ones complement)

1 1 1 1 1 0 1 0 (-6 in twos complement)

-2n-1 to 2n-1 -1

76

Data Types and Storage: Floating Point

Floating point types represent real numbers, such as 12.25, that include a

decimal point

Digits to the right of the decimal point form the fractional part of the

number

Digits to the left of the decimal point form the integral part of the

number

Example: Convert 12.2510 to binary

First convert the integer part: 1210=11002

Then repeatedly multiply the fractional part by 2:

.25*2=0.5 C0

.50*2=1.0 C1

Therefore: 12.2510 =1100.012

77

Example 1:

What does the decimal value of 124 stand for?

102

1

124

101 100

2

4

x100 + x10 + x1

base

rightmost place

answer

124

22

1

110

x4

21

1

+ x2 +

20

0

base

rightmost place

x1

answer

6

78

Example 1:

What is the decimal value of the octal value 176?

176

82

81

80

base

rightmost place

answer

x64 + x8 +

x1

126

B29

162

161

160

x256 + x16 +

x1

base

rightmost place

answer

2857

79

Example 2: (Binary-to-Decimal Conversion Table)

80

Example 3a: Binary to Decimal

Convert: 1012 = ?10

Begin with rightmost bit (ones place).

The next bit is the twos place, then the fours place, and so on

22

21

20

x4 + x2 + x1

81

Example 3b: Binary to Decimal

Convert: 10102 = ?10

The next bit is the twos place, then the fours place, and so on

23

22

21

20

10

x8 + x4 + x2 + x1

82

Example 3c: Octal to Decimal

Convert: 2708 = ?10

The next bit is the eights place, then the sixty-fours place, and so

on

82

81

80

184

x64+ x8 + x1

83

Example 3d: Hexadecimal to Decimal

Convert: 112A16 = ?10

The next bit is the 16s place, then the 256s place, and so on

163

162

161

160

4394

84

85

Example 4a: Decimal to Binary

Convert: 2410 = ?2

86

Example 4b: Decimal to Binary

Convert: 28.62510 = ?2

24

23

22

21

20

2-1

16

16

28.62510

2-2

0.5

0

2-3

0.125

0

= 11100.1012

87

Example 4c: Octal to Binary

Convert: 53358 = ?2

88

Example 4d: Binary to Octal

Convert: 1010110111012 = ?8

89

Example 4e: Hexadecimal to Binary

Convert: 1A8416 = ?2

90

Data Inside Computers

Computers use electricity for data processing and storage

Electricity can flow through switches

If switch is closed, electricity flows

If switch is open, electricity does not flow

Switches have only two states: ON and OFF

Binary has two digits: 0 and 1

1 for the ON state, 0 for the OFF state

91

Data Inside Computers

Computer manufacturers express capacity of memory in terms of bytes

One byte can represent many different kinds of data

This depends on how the computer creates the byte

65

0

64

ASCII A

92

Data Inside Computers

0

66

64

ASCII B

1

67

64

ASCII C

93

Data Inside Computers

Picture and Graphics Data

Digital photographs are composed of a grid of colored dots

A grid of pixels represents graphic data

Examples: pictures, frames of a movie, animations

Pixel is short for Picture Element

In simple graphics, a byte can represent a single pixel

1 byte can hold 256 different integers (0 255)

10000000

01111100

11111010

11001100

94

Data Inside Computers: Picture and Graphics Data

Bitmap (.bmp) store every pixel of image

JPEG & GIF compressed formats

Instead of storing every pixel, store a pattern of pixels

downloading them over the Web, etc

image files without altering the quality of the image

A compression ratio is commonly adjusted to make graphic file size smaller

95

Data Inside Computers: Picture and Graphics Data

96

Data Inside Computers: Picture and Graphics Data

97

Data Inside Computers: Picture and Graphics Data

98

Data Inside Computers: Picture and Graphics Data

99

Data Inside Computers: Picture and Graphics Data

100

Data Inside Computers: Picture and Graphics Data

101

Analyzing First Program C++ Program Structure

1. State the problem clearly

2. Describe the input and output information clearly

3. Work the problem by hand (or calculator) for a simple set of

data

4. Develop a solution and convert it to a computer program

5. Test the solution with a variety of data

102

Analyzing First Program C++ Program Structure

Example:

103

Analyzing First Program C++ Program Structure

1.

points in a plane

2.

I/O diagram

(x1,y1)

(x2,y2)

3.

Hand example

4.

5.

Processing

(program)

1.

2.

3.

4.

Testing

D (distance)

104

Analyzing First Program C++ Program Structure

/*-------------------------------------------------------* Program chapter1_1

* This program computes the distance between two points.

*/

#include <iostream> // Required for cout, endl.

#include <cmath>

// Required for sqrt()

using namespace std;

int main()

{

// Declare and initialize objects.

double x1(1), y1(5), x2(4), y2(7),

side1, side2, distance;

// Compute sides of a right triangle.

side1 = x2 - x1;

side2 = y2 - y1;

distance = sqrt(side1*side1 + side2*side2);

// Print distance.

cout << "The distance between the two points is "

<< distance << endl;

// Exit program.

return 0;

}

//--------------------------------------------------------

105

/*-------------------------------------------------------* Program chapter1_1

* This program computes the distance between two points.

*/

#include <iostream> // Required for cout, endl.

#include <cmath>

// Required for sqrt()

using namespace std;

int main()

{

// Declare and initialize objects.

double x1(1), y1(5), x2(4), y2(7),

Comments:

side1, side2, distance;

Document the

// Compute sides of a right triangle.

programs purpose

side1 = x2 - x1;

Help the human

side2 = y2 - y1;

reader understand the

distance = sqrt(side1*side1 + side2*side2);

program

// Print distance.

Are ignored by the

compiler

cout << "The distance between the two points is "

// comments to end-of

<< distance << endl;

line

// Exit program.

/* starts a comment

return 0;

block ending with */

}

106

//--------------------------------------------------------

107

108

109

110

111

112