Lecture 2 - Data Representation & Storage.pdf

download Lecture 2 - Data Representation & Storage.pdf

of 38

Transcript of Lecture 2 - Data Representation & Storage.pdf

  • 8/14/2019 Lecture 2 - Data Representation & Storage.pdf

    1/38

    1/9/2013

    1992-2012 by Pearson Education, Inc. & John Wiley & SonsSome portions are adopted from C++ f or Everyone by Horstmann

    ENGR 1200U Introduc tion to Programming

    Lecture 2Data Representation & Storage

    Dr. Eyhab Al-Masri

    ENGR 1200UWinter 2013 - UOIT

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

  • 8/14/2019 Lecture 2 - Data Representation & Storage.pdf

    2/38

    1/9/2013

    ENGR 1200UWinter 2013 - UOIT

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

    Can a high-level program run without itsmachine language?

    ENGR 1200UWinter 2013 - UOIT

    While performing arithmetic, one usually gives littlethought on the question: How many decimal digits it takes to represent a number?

    Physicists can calculate that there are 10 78 electrons inthe universe without being bothered by the fact that itrequires 79 decimal digits to write that number out in full

    Example

    Calculating value of a function to six/seven/eightsignificant digits (using pencil and paper) The problem of the paper not being wide enough for seven-

    eight digit numbers never arises!

  • 8/14/2019 Lecture 2 - Data Representation & Storage.pdf

    3/38

    1/9/2013

    ENGR 1200UWinter 2013 - UOIT

    With computers, its quite different! On most computers, amount of memory available for

    storing a number is fixed at the time that the computeris designed

    With a certain amount of effort, programmer canrepresent numbers two, or three or even many timeslarger than this fixed amount

    Challenge: fixed amount of memory

    ENGR 1200UWinter 2013 - UOIT

    Finite nature of the computer forces us to dealonly with numbers that can be represented in afixed number of digits finite-precisionnumbers

    Example : set of positive integers representableby three decimal digits (no decimal or sign)?

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

  • 8/14/2019 Lecture 2 - Data Representation & Storage.pdf

    4/38

    1/9/2013

    ENGR 1200UWinter 2013 - UOIT

    Since computers have finite memories (then itmust perform arithmetic on finite-precisionnumbers), the results of certain calculations willbe just plain wrong (from a mathematicalperspective)

    Algebra of finite-precision numbers is differentfrom normal algebra

    ENGR 1200UWinter 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 infinitearithmetic of three-digit integers)

  • 8/14/2019 Lecture 2 - Data Representation & Storage.pdf

    5/38

    1/9/2013

    ENGR 1200UWinter 2013 - UOIT

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

    ENGR 1200UWinter 2013 - UOIT

    Conclusion:

    Are computers suitable for doing arithmetic?

    Yes

  • 8/14/2019 Lecture 2 - Data Representation & Storage.pdf

    6/38

    1/9/2013

    ENGR 1200UWinter 2013 - UOIT

    Original computers were mainly designed toperform calculations (i.e. calculator)

    Designers used electronic components available at thetime

    Designers soon realized they could use a simple codingsystem (the binary system) to represent numbers

    ENGR 1200UWinter 2013 - UOIT

    All types of information inside a computer canbe represented using binary system

    Numbers Letters of alphabet Punctuation marks Microprocessor instructions Graphics/Video Sound

  • 8/14/2019 Lecture 2 - Data Representation & Storage.pdf

    7/38

    1/9/2013

    ENGR 1200UWinter 2013 - UOIT

    Arithmetic used by computers differs in someways from the arithmetic used by people

    Two main differences Most important difference is that computers perform

    operations on numbers whose precision is finite andfixed

    Most computers use the binary rather than the decimal

    system for representing numbers

    ENGR 1200UWinter 2013 - UOIT

    binary digit is a single numeral in a binarynumber

    Example: 1 0 0 1 0 Each 0 and 1 in this example is a binary digit

    Term bi nary digi t is commonly called a it

    Eight bits grouped together form a yt

  • 8/14/2019 Lecture 2 - Data Representation & Storage.pdf

    8/38

  • 8/14/2019 Lecture 2 - Data Representation & Storage.pdf

    9/38

    1/9/2013

    ENGR 1200UWinter 2013 - UOIT

    Ordinary decimal number consists of a string ofdecimal digits and, possibly, a decimal pointThe general form and its usual representationare shown below

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

    ENGR 1200UWinter 2013 - UOIT

    Choice of 10 as the base for exponentiation (called the radix ) ismade because we are using decimal, or base 10 numbers

    When dealing with computers, it is frequently convenient to useradices 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)

  • 8/14/2019 Lecture 2 - Data Representation & Storage.pdf

    10/38

    1/9/2013

    1

    ENGR 1200UWinter 2013 - UOIT

    Binary numbers are built up from two binarydigits 0 1

    Octal numbers are built up from the eight octaldigits 0 1 2 3 4 5 6 7

    Hexadecimal numbers are built up from 16

    digits 0 1 2 3 4 5 6 7 8 9 A B C D E F

    ENGR 1200UWinter 2013 - UOIT

    Decimal number 2001 expressed in binary,octal, and hexadecimal

  • 8/14/2019 Lecture 2 - Data Representation & Storage.pdf

    11/38

    1/9/2013

    1

    ENGR 1200UWinter 2013 - UOIT

    Decimal numbers andtheir binary, octal, and

    hexadecimal equivalents

    ENGR 1200UWinter 2013 - UOIT

    Decimal numbers andtheir binary, octal, and

    hexadecimal equivalents

  • 8/14/2019 Lecture 2 - Data Representation & Storage.pdf

    12/38

    1/9/2013

    1

    ENGR 1200UWinter 2013 - UOIT

    Fundamental aspect of number systems is thatthe rules governing are the same in any system

    Counting Arithmetic operations Conversions from one base to another

    The rules for performing an operation in onenumber system should be applied to anothernumber system i.e. addition rules in decimal should be able to apply

    them in binary

    ENGR 1200UWinter 2013 - UOIT

    Any number to the 0 (zero) power is 1

    4 0 =1 5 0 = 1 1,800 0 = 1

    Any number to the first power is the numberitself

    6 1 = 6 87 1 = 87 1828 1=1828

  • 8/14/2019 Lecture 2 - Data Representation & Storage.pdf

    13/38

    1/9/2013

    1

    ENGR 1200UWinter 2013 - UOIT

    Prefix deci stands for tenthDecimal number system is a based 10 numbersystem

    k = 10 k-1 = 9

    There are 10 different symbols (called digits)

    0 1 2 3 4 5 6 7 8 9

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

    ENGR 1200UWinter 2013 - UOIT

    10 3 10 2 10 1 10 0

    1000 100 10 1

    1 8 2 8

  • 8/14/2019 Lecture 2 - Data Representation & Storage.pdf

    14/38

    1/9/2013

    1

    ENGR 1200UWinter 2013 - UOIT

    1 x 1000 = 1000

    8 x 100 = 800

    2 x 10 = 20

    8 x 1 = 8

    1828

    +

    ENGR 1200UWinter 2013 - UOIT

    Prefix bi stands for two

    Binary number system is a base 2 numbersystem

    There are 2 symbols0 1

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

  • 8/14/2019 Lecture 2 - Data Representation & Storage.pdf

    15/38

    1/9/2013

    1

    ENGR 1200UWinter 2013 - UOIT

    2 3 2 2 2 1 2 0

    8 4 2 1

    1 1 0 1

    ENGR 1200UWinter 2013 - UOIT

    1 x 8 = 8

    1 x 4 = 4

    0 x 2 = 0

    0 x 1 = 0

    12

    +

  • 8/14/2019 Lecture 2 - Data Representation & Storage.pdf

    16/38

    1/9/2013

    1

    ENGR 1200UWinter 2013 - UOIT

    2 7 2 6 2 5 2 4 2 3 2 2 2 1 2 0

    128 64 32 16 8 4 2 1

    1 0 1 1 0 1 0 1

    ENGR 1200UWinter 2013 - UOIT

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

  • 8/14/2019 Lecture 2 - Data Representation & Storage.pdf

    17/38

  • 8/14/2019 Lecture 2 - Data Representation & Storage.pdf

    18/38

    1/9/2013

    1

    ENGR 1200UWinter 2013 - UOIT

    Decimal numbers can be converted into binary intwo different ways

    Repeated Subtraction Repeated Division

    Both methods produce the same result and youshould use whichever method you are mostcomfortable with

    ENGR 1200UWinter 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 youget to the first binary place value that is GREATER THANthe decimal number you are trying to convert

    Example

    512 256 128 64 32 16 8 4 2 1

    485

  • 8/14/2019 Lecture 2 - Data Representation & Storage.pdf

    19/38

    1/9/2013

    1

    ENGR 1200UWinter 2013 - UOIT

    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 1200UWinter 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 valueSTEP 4 Repeat step 3 until all of the place values are

    processed

    485 256 = 229256 128 64 32 16 8 4 2 11

  • 8/14/2019 Lecture 2 - Data Representation & Storage.pdf

    20/38

    1/9/2013

    2

    ENGR 1200UWinter 2013 - UOIT

    229 128 = 101256 128 64 32 16 8 4 2 1

    1 1101 64 = 37

    256 128 64 32 16 8 4 2 11 1 1 37 32 = 5

    256 128 64 32 16 8 4 2 11 1 1 1

    5 16 = X256 128 65 32 16 8 4 2 11 1 1 1 0

    256 128 65 32 16 8 4 2 11 1 1 1 0 0 1 0 1

    ENGR 1200UWinter 2013 - UOIT

    Method B Repeated Division - for integers only) Dividing the number by 2

    The quotient is written directly beneath the originalnumber and the remainder, 0 or 1, is written next tothe quotient

    The quotient is then considered and the process is

    repeated until the number 0 has been reached

  • 8/14/2019 Lecture 2 - Data Representation & Storage.pdf

    21/38

    1/9/2013

    2

    ENGR 1200UWinter 2013 - UOIT

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

  • 8/14/2019 Lecture 2 - Data Representation & Storage.pdf

    22/38

    1/9/2013

    2

    ENGR 1200UWinter 2013 - UOIT

    Performing the same example of 485

    485 / 2 = 242 rem 1242 / 2 = 121 rem 0121 / 2 = 60 rem 160 / 2 = 30 rem 030 / 2 = 15 rem 015 / 2 = 7 rem 1

    7 / 2 = 3 rem 1

    3 / 2 = 1 rem 11 / 2 = X rem 1

    Least Significant Digit

    Most Significant Digit

    111100101

    ENGR 1200UWinter 2013 - UOIT

    Prefix oct (octo) stands for eight

    Octal number system is a base 8 number system

    There are 8 symbols0 1 2 3 4 5 6 7

    Each digit multiples a power of 8

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

  • 8/14/2019 Lecture 2 - Data Representation & Storage.pdf

    23/38

    1/9/2013

    2

    ENGR 1200UWinter 2013 - UOIT

    STEP 1Start with the LEAST SIGNIFICANT digit, mark outthe digits in groups of 3

    Example

    1 0 1 1 1 0 1 1 1

    ENGR 1200UWinter 2013 - UOIT

    STEP 2Convert each group of three digits to is octaldigit (or symbol)

    Example

    1 0 1 1 1 0 1 1 1

    5 6 7

    The last groupon the left can

    have anywherefrom 1 to 3

    binary digitgroup

  • 8/14/2019 Lecture 2 - Data Representation & Storage.pdf

    24/38

    1/9/2013

    2

    ENGR 1200UWinter 2013 - UOIT

    Converting octal numbers to binary is just thereverse operation of converting binary to octal

    Simply convert each octal digit to its three-bitbinary pattern. The resulting set of 1s and 0s isthe binary equivalent of the octal number

    ENGR 1200UWinter 2013 - UOIT

    5 6 7

    1 0 1 1 1 0 1 1 1

  • 8/14/2019 Lecture 2 - Data Representation & Storage.pdf

    25/38

  • 8/14/2019 Lecture 2 - Data Representation & Storage.pdf

    26/38

    1/9/2013

    2

    ENGR 1200UWinter 2013 - UOIT

    8 3 8 2 8 1 8 0

    512 64 8 1

    7 5 0 1

    ENGR 1200UWinter 2013 - UOIT

    Multiply each digit of the octal number by itsplace value and add the results

    Example: 7501

    7 x 512 = 3584 5 x 64 = 320

    0 x 8 = 0 1 x 1 = 1

    3905

    +

  • 8/14/2019 Lecture 2 - Data Representation & Storage.pdf

    27/38

    1/9/2013

    2

    ENGR 1200UWinter 2013 - UOIT

    Prefix hexa stands for 6 and the prefix decistands for 10

    Hexadecimal number system is a base 16 numbersystem

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

    Each digit multiples a power of 16

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

    ENGR 1200UWinter 2013 - UOIT

    STEP 1Start with the LEAST SIGNIFICANT digit, mark outthe digits in groups of 4

    Example

    1 0 1 1 1 0 1 1 1

  • 8/14/2019 Lecture 2 - Data Representation & Storage.pdf

    28/38

    1/9/2013

    2

    ENGR 1200UWinter 2013 - UOIT

    STEP 2Convert each group of four digits to ishexadecimal digit (or symbol)

    Example

    1 0 1 1 1 0 1 1 1

    1 7 7

    The last groupon the left can

    have anywherefrom 1 to 4binary digit

    group

    ENGR 1200UWinter 2013 - UOIT

    Converting hexadecimal numbers to binary is just the reverse operation of converting binary tohexadecimal

    Simply convert each hexadecimal digit to itsfour-bit binary pattern. The resulting set of 1s

    and 0s is the binary equivalent of thehexadecimal number

  • 8/14/2019 Lecture 2 - Data Representation & Storage.pdf

    29/38

    1/9/2013

    2

    ENGR 1200UWinter 2013 - UOIT

    16 3 16 2 16 1 16 0

    4096 256 16 1

    2 E C 3

    ENGR 1200UWinter 2013 - UOIT

    Multiply each digit of the hexadecimal numberby its place value and add the results

    Example: AB87

    10 x 4096 = 40960 11 x 256 = 2816

    8 x 16 = 128 7 x 1 = 7

    43911

    +

  • 8/14/2019 Lecture 2 - Data Representation & Storage.pdf

    30/38

    1/9/2013

    3

    ENGR 1200UWinter 2013 - UOIT

    There are a set of terms used in electronicswhich are used to represent different powers often

    There is also a set of terms used to representlarge whole numbers and a set of terms used torepresent small fractional numbers

    ENGR 1200UWinter 2013 - UOIT

    Prefix Value Abbreviation______ ______ ____________

    Kilo 1,000 K

    Mega 1,000,000 M

    Giga 1,000,000,000 G

    Tera 1,000,000,000,000 T

  • 8/14/2019 Lecture 2 - Data Representation & Storage.pdf

    31/38

  • 8/14/2019 Lecture 2 - Data Representation & Storage.pdf

    32/38

    1/9/2013

    3

    ENGR 1200UWinter 2013 - UOIT

    INFR 2810 Computer Architecture

    Data Types and Storage

    Negative Binary Numbers

    ENGR 1200UWinter 2013 - UOIT

    When data is represented in memory, it isrepresented 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

  • 8/14/2019 Lecture 2 - Data Representation & Storage.pdf

    33/38

    1/9/2013

    3

    ENGR 1200UWinter 2013 - UOIT

    Computers use a standard binary code torepresent letters of the alphabet, numerals,punctuation marks, and other special characters

    The code is referred to as American NationalStandards Institute (ANSI) Based on a previous encoding scheme called (ASCII)

    American Standard Code for Information Interchange

    Currently, there are 256 code combinations inthe ANSI format ASCII had 128 code combinations

    ENGR 1200UWinter 2013 - UOIT

  • 8/14/2019 Lecture 2 - Data Representation & Storage.pdf

    34/38

    1/9/2013

    3

    ENGR 1200UWinter 2013 - UOIT

    Character Code HEX_________ _____ ____

    B 0100 0010 42

    b 0110 0010 62

    & 0010 0110 26

    [BACKSPACE] 0000 1000 8

    ENGR 1200UWinter 2013 - UOIT

    Two basic data types

    integer floating

    point

  • 8/14/2019 Lecture 2 - Data Representation & Storage.pdf

    35/38

    1/9/2013

    3

    ENGR 1200UWinter 2013 - UOIT

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

    Left most bit is reserved for the sign of thenumber

    Remaining 31 bits represent the magnitude ofthe number

    ENGR 1200UWinter 2013 - UOIT

    Representation of data affects the efficiency ofarithmetic and logic operations

    For efficiency, negative integers are oftenrepresented in their 2s complement form

    The 2s complement of an integer is formed bynegating all of the bits and adding one

  • 8/14/2019 Lecture 2 - Data Representation & Storage.pdf

    36/38

    1/9/2013

    3

    ENGR 1200UWinter 2013 - UOIT

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

    Switch all 0s to 1s and 1s to 0sBinary # 101100111s complement 01001100

    ENGR 1200UWinter 2013 - UOIT

    Twos ComplementHas a sign bit that is 0 for plus and 1 for minus. Tonegate a number, it is done in a two step process:

    1. Each 1 is replaced by a 0 and each 0 is replaced by a 1 (justas in the ones complement)

    2. 1 is added to the result.Binary addition is the same as decimal addition except thata carry is generated if the sum is greater than 1 rather thangreater 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)

  • 8/14/2019 Lecture 2 - Data Representation & Storage.pdf

    37/38

    1/9/2013

    3

    ENGR 1200UWinter 2013 - UOIT

    A signed number has the range -2 n-1 to 2 n-1 -1

    An unsigned number has the range 0 to 2 n-1

    ENGR 1200UWinter 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 thefractional part of the number

    Digits to the left of the decimal point form theintegral part of the number

  • 8/14/2019 Lecture 2 - Data Representation & Storage.pdf

    38/38

    1/9/2013

    ENGR 1200UWinter 2013 - UOIT

    Convert 12.25 10 to binary

    First convert the integer part: 12 10 =1100 2Then repeatedly multiply the fractional part by 2: .25*2=0.5 C0 .50*2=1.0 C1Therefore:

    12.2510 =1100.01 2

    ENGR 1200UWinter 2013 - UOIT

    Textbook: Page 20: Practice exercises 1-10

    Textbook: Page 22: Practice exercises 1-10

    Textbook: Page 32

    Practice exercises 10-13