Vous êtes sur la page 1sur 38

1/9/2013

ENGR 1200U Introduction to Programming Lecture 2 Data Representation & Storage Dr. Eyhab Al-Masri

1992-2012 by Pearson Education, Inc. & John Wiley & Sons Some portions are adopted from C++ for Everyone by Horstmann

https://piazza.com/uoit.ca/spring2013/engr1200u

ENGR 1200U Winter 2013 - UOIT

1/9/2013

Who is the inventor of the Analytical Engine? What is Programming? What are the main components inside a computer? What are the main software categories?
What does ALU stands for? Its purpose? List application software sub-categories (i.e. types)

Why machine language is important? Can a high-level program run without its machine language?

ENGR 1200U Winter 2013 - UOIT

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 seveneight digit numbers never arises!

ENGR 1200U Winter 2013 - UOIT

1/9/2013

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

ENGR 1200U Winter 2013 - UOIT

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

ENGR 1200U Winter 2013 - UOIT

1/9/2013

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

ENGR 1200U Winter 2013 - UOIT

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 three-digit integers)

ENGR 1200U Winter 2013 - UOIT

1/9/2013

Example

Distribution law (distribute pieces as you separate or break it into parts) a x (b c) = a x b a x c


for a = 5, b = 210, c = 195 Left side: 5 x 15 75 right side: overflow

A x b causes an overflow: 5 x 210 1050

distributive law does not hold (overflow in the infinite arithmetic of three-digit integers)
ENGR 1200U Winter 2013 - UOIT

Conclusion:
Are computers suitable for doing arithmetic?

Yes

ENGR 1200U Winter 2013 - UOIT

1/9/2013

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

ENGR 1200U Winter 2013 - UOIT

All types of information inside a computer can be represented using binary system
Numbers Letters of alphabet Punctuation marks Microprocessor instructions Graphics/Video Sound

ENGR 1200U Winter 2013 - UOIT

1/9/2013

Arithmetic used by computers differs in some ways from the arithmetic used by people Two main differences

Most important difference is that computers perform operations on numbers whose precision is finite and fixed Most computers use the binary rather than the decimal system for representing numbers

ENGR 1200U Winter 2013 - UOIT

binary digit is a single numeral in a binary number Example: 1 0 0 1 0

Each 0 and 1 in this example is a binary digit

Term binary digit is commonly called a bit Eight bits grouped together form a byte

ENGR 1200U Winter 2013 - UOIT

1/9/2013

Binary numbers can be stored in memory as sequence of bits Example

The right most bit of a byte represents the ones position The next bit represents the twos position The nest bit represents the fours position The left most bit represents 2n-1

ENGR 1200U Winter 2013 - UOIT

Binary numbers can be stored in memory as a sequence of bits, called a word The number of words available in memory is referred to as the memory space, or address space Address Sixteen Bit Word
000 0000101011011101 1010001011010100 1011010010100101 0101001101010101 0101000111001110 1100110000111010 0100011101001001 0101110001001000

Address Space 8 Word Size 16 Double Word 32 Quad Word 64

001 010 011 010 010 110 111

ENGR 1200U Winter 2013 - UOIT

1/9/2013

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

ENGR 1200U Winter 2013 - UOIT

radix k number system requires k different symbols to represent the digits 0 to k -1

Choice of 10 as the base for exponentiation (called the radix) is made because we are using decimal, or base 10 numbers When dealing with computers, it is frequently convenient to use radices other than 10 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 Most important radices are 2 (binary) 8 (octal) 16 (hexadecimal)

ENGR 1200U Winter 2013 - UOIT

1/9/2013

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


0123456789ABCDEF

ENGR 1200U Winter 2013 - UOIT

Decimal number 2001 expressed in binary, octal, and hexadecimal

ENGR 1200U Winter 2013 - UOIT

10

1/9/2013

Decimal numbers and their binary, octal, and hexadecimal equivalents

ENGR 1200U Winter 2013 - UOIT

Decimal numbers and their binary, octal, and hexadecimal equivalents

ENGR 1200U Winter 2013 - UOIT

11

1/9/2013

Fundamental aspect of number systems is that the rules governing are the same in any system
Counting Arithmetic operations Conversions from one base to another

The rules for performing an operation in one number system should be applied to another number system

i.e. addition rules in decimal should be able to apply them in binary

ENGR 1200U Winter 2013 - UOIT

Any number to the 0 (zero) power is 1


40 =1 50 = 1 1,8000 = 1

Any number to the first power is the number itself


61 = 6 871 = 87 18281=1828

ENGR 1200U Winter 2013 - UOIT

12

1/9/2013

Prefix deci stands for tenth Decimal number system is a based 10 number system
k = 10 k-1 = 9

There are 10 different symbols (called digits)


0123456789

Each place value in a decimal number is a power of 10


ENGR 1200U Winter 2013 - UOIT

103 1000 1

102 100 8

101 10 2

100 1 8

ENGR 1200U Winter 2013 - UOIT

13

1/9/2013

1 x 1000 = 8 x 100 = 2 x 10 8x 1 = =

1000 800 20

8 1828

ENGR 1200U Winter 2013 - UOIT

Prefix bi stands for two Binary number system is a base 2 number system
There are 2 symbols
01

Each place value in a binary number is a power of 2

ENGR 1200U Winter 2013 - UOIT

14

1/9/2013

23 8 1

22 4 1

21 2 0

20 1 1

ENGR 1200U Winter 2013 - UOIT

1x 8 1x 4 0x 2 0x 1

= = = =

8 4 0

0 12

ENGR 1200U Winter 2013 - UOIT

15

1/9/2013

27 26 25 24 23 128 64 32 16 8 1 0 1 1 0

22 21 20 4 2 1 1 0 1

ENGR 1200U Winter 2013 - UOIT

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

ENGR 1200U Winter 2013 - UOIT

16

1/9/2013

ENGR 1200U Winter 2013 - UOIT

Binary numbers can be converted to decimal in two ways


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 Binary number is written vertically (one bit per line) with the leftmost bit on the bottom
Bottom line is called line 1, line above it is called line 2, etc Decimal number will be built up in parallel column next to the binary number

ENGR 1200U Winter 2013 - UOIT

17

1/9/2013

Decimal numbers can be converted into binary in two different ways


Repeated Subtraction Repeated Division

Both methods produce the same result and you should use whichever method you are most comfortable with

ENGR 1200U Winter 2013 - UOIT

Method A (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
64 32 16 8 4 2 1

512

256

128

ENGR 1200U Winter 2013 - UOIT

18

1/9/2013

Method A (Repeated Subtraction) (contd)

STEP 2 Mark out the largest place value (this will tell us how many place values we will need)

485
512 256 128 64 32 16 8 4 2 1

ENGR 1200U Winter 2013 - UOIT

Method A (Repeated Subtraction) (contd)

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

1
ENGR 1200U Winter 2013 - UOIT

19

1/9/2013

256 1 256 1 256 1 256 1

128 1 128 1 128 1 128 1

64 64 1 64 1 65 1

229 128 = 101 32 16 8 101 64 = 37 32 16 8 37 32 = 5 32 16 8 1 5 16 = X 32 16 8 1 0

4 4 4 4

2 2 2 2

1 1 1 1

256 1
ENGR 1200U Winter 2013 - UOIT

128 1

65 1

32 1

16 0

8 0

4 1

2 0

1 1

Method B (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

ENGR 1200U Winter 2013 - UOIT

20

1/9/2013

Method B (Repeated Division)

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

ENGR 1200U Winter 2013 - UOIT

Method B (Repeated Division)

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

ENGR 1200U Winter 2013 - UOIT

21

1/9/2013

Performing the same example of 485 485 / 2 242 / 2 121 / 2 60 / 2 30 / 2 15 / 2 7 /2 3 /2 1 /2


ENGR 1200U Winter 2013 - UOIT

= = = = = = = = =

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

Least Significant Digit

111100101

Most Significant Digit

Prefix oct (octo) stands for eight Octal number system is a base 8 number system
There are 8 symbols
01234567

Each digit multiples a power of 8 Each group of 3 binary digits can be represented by a single octal digit
ENGR 1200U Winter 2013 - UOIT

22

1/9/2013

Start with the LEAST SIGNIFICANT digit, mark out the digits in groups of 3 Example
1 0 1 1 1 0 1 1 1

STEP 1

ENGR 1200U Winter 2013 - UOIT

Convert each group of three digits to is octal digit (or symbol) Example
1 0 1 1 1 0 1 1 1
The last group on the left can have anywhere from 1 to 3 binary digit group

STEP 2

ENGR 1200U Winter 2013 - UOIT

23

1/9/2013

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

ENGR 1200U Winter 2013 - UOIT

ENGR 1200U Winter 2013 - UOIT

24

1/9/2013

There are two methods to choose from:


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

ENGR 1200U Winter 2013 - UOIT

Performing the same example of 485


Least Significant Digit

485 / 8 60 / 8 7 /8

= 60 =7 = 60

rem 0.625 0.625 x 8 = 5 rem 4 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

ENGR 1200U Winter 2013 - UOIT

25

1/9/2013

83 512 7

82 64 5

81 8 0

80 1 1

ENGR 1200U Winter 2013 - UOIT

Multiply each digit of the octal number by its place value and add the results Example: 7501
7 5 0 1 x 512 = x 64 = x 8 = x 1 = 3584 320 0 1 +
3905

ENGR 1200U Winter 2013 - UOIT

26

1/9/2013

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

Each digit multiples a power of 16

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

ENGR 1200U Winter 2013 - UOIT

Start with the LEAST SIGNIFICANT digit, mark out the digits in groups of 4 Example
1 0 1 1 1 0 1 1 1

STEP 1

ENGR 1200U Winter 2013 - UOIT

27

1/9/2013

Convert each group of four digits to is hexadecimal digit (or symbol) Example
1 0 1 1 1 0 1 1 1
The last group on the left can have anywhere from 1 to 4 binary digit group

STEP 2

ENGR 1200U Winter 2013 - UOIT

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

ENGR 1200U Winter 2013 - UOIT

28

1/9/2013

163 4096 2

162 161 160 256 16 1 E C 3

ENGR 1200U Winter 2013 - UOIT

Multiply each digit of the hexadecimal number by its place value and add the results Example: AB87
10 11 8 7 x 4096 = 40960 x 256 = 2816 x 16 = 128 x 1 =+ 7

43911
ENGR 1200U Winter 2013 - UOIT

29

1/9/2013

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

ENGR 1200U Winter 2013 - UOIT

Prefix ______ Kilo Mega Giga Tera


ENGR 1200U Winter 2013 - UOIT

Value ______ 1,000 1,000,000 1,000,000,000 1,000,000,000,000

Abbreviation ____________ K M G T

30

1/9/2013

Prefix ______ milli micro nano pico


ENGR 1200U Winter 2013 - UOIT

Value ______ 1/1,000 1/1,000,000 1/1,000,000,000 1/1,000,000,000,000

Abbreviation ____________ m n p

kilobyte (KB) is 1,024 bytes megabyte (MB) is 1,048,576 bytes These values are derived from the nearest binary place values 1,000 and 1,000,000

ENGR 1200U Winter 2013 - UOIT

31

1/9/2013

INFR 2810 Computer Architecture Data Types and Storage Negative Binary Numbers

ENGR 1200U Winter 2013 - UOIT

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, or digital signal, or some other type of data

ENGR 1200U Winter 2013 - UOIT

32

1/9/2013

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

ENGR 1200U Winter 2013 - UOIT

ENGR 1200U Winter 2013 - UOIT

33

1/9/2013

Character _________ B b & [BACKSPACE]


ENGR 1200U Winter 2013 - UOIT

Code _____ 0100 0010 0110 0010 0010 0110 0000 1000

HEX ____ 42 62 26 8

Two basic data types

integer

floating point

ENGR 1200U Winter 2013 - UOIT

34

1/9/2013

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

ENGR 1200U Winter 2013 - UOIT

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

ENGR 1200U Winter 2013 - UOIT

35

1/9/2013

Ones Complement

Has a sign bit with 0 used for plus and 1 for minus. To negate a number, replace each 1 by a 0 and each 0 by a 1. This holds for the sign bit as well. Ones complete is obsolete

Switch all 0s to 1s and 1s to 0s Binary # 10110011 1s complement 01001100

ENGR 1200U Winter 2013 - UOIT

Twos Complement

1. Each 1 is replaced by a 0 and each 0 is replaced by a 1 (just as in the ones complement) 2. 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

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:

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)


ENGR 1200U Winter 2013 - UOIT

36

1/9/2013

A signed number has the range


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

An unsigned number has the range


0 to 2n-1

ENGR 1200U Winter 2013 - UOIT

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

ENGR 1200U Winter 2013 - UOIT

37

1/9/2013

Convert 12.2510 to binary First convert the integer part: Then repeatedly multiply the fractional part by 2: Therefore:
.25*2=0.5 C0 .50*2=1.0 C1 1210=11002

12.2510 =1100.012

ENGR 1200U Winter 2013 - UOIT

Textbook: Page 20: Textbook: Page 22: Textbook: Page 32

Practice exercises 1-10

Practice exercises 1-10

Practice exercises 10-13

ENGR 1200U Winter 2013 - UOIT

38

Vous aimerez peut-être aussi