Lecture 3 - Data Rep. and Storage cont..pdf

Embed Size (px)

Citation preview

  • 8/14/2019 Lecture 3 - Data Rep. and Storage cont..pdf

    1/20

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

    ENGR 1200U Introduction to Programming

    Lecture 3

    Data Representation & Storage (contd)

    Dr. Eyhab Al-Masri

    ENGR 1200U

    Winter 2013 - UOIT

    Prefix oct (octo) stands for eight

    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 asingle octal digit

  • 8/14/2019 Lecture 3 - Data Rep. and Storage cont..pdf

    2/20

    ENGR 1200U

    Winter 2013 - UOIT

    STEP 1 Start with the LEAST SIGNIFICANT digit, mark out

    the digits in groups of 3

    Example

    1 0 1 1 1 0 1 1 1

    ENGR 1200U

    Winter 2013 - UOIT

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

    The last group

    on the left can

    have anywhere

    from 1 to 3

    binary digitgroup

  • 8/14/2019 Lecture 3 - Data Rep. and Storage cont..pdf

    3/20

    ENGR 1200U

    Winter 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 1200U

    Winter 2013 - UOIT

    5 6 7

    1 0 1 1 1 0 1 1 1

  • 8/14/2019 Lecture 3 - Data Rep. and Storage cont..pdf

    4/20

    ENGR 1200U

    Winter 2013 - UOIT

    There are two methods to choose from:

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

    Do a direct conversion using the repeated divisionmethod

    Since this is a conversion to octal, 8 is the divisor eachtime

    ENGR 1200U

    Winter 2013 - UOIT

    Performing the same example of 485

    485 / 8 = 60 rem 0.625 0.625 x 8 = 560 / 8 = 7 rem 47 / 8 = X rem 7

    Least Significant Digit

    Most Significant Digit

    7 4 5

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

    may end up with remainders of anywhere from 0 to 7

  • 8/14/2019 Lecture 3 - Data Rep. and Storage cont..pdf

    5/20

    ENGR 1200U

    Winter 2013 - UOIT

    Example:

    Given octal number 7501, what is the equivalentdecimal value?

    83 82 81 80

    512 64 8 1

    7 5 0 1

    ENGR 1200U

    Winter 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 3 - Data Rep. and Storage cont..pdf

    6/20

    ENGR 1200U

    Winter 2013 - UOIT

    Prefix hexa stands for 6 and the prefix deci standsfor 10

    Hexadecimal number system is a base 16 numbersystem

    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 of16

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

    ENGR 1200U

    Winter 2013 - UOIT

    STEP 1 Start with the LEAST SIGNIFICANT digit, mark out

    the digits in groups of 4

    Example

    1 0 1 1 1 0 1 1 1

  • 8/14/2019 Lecture 3 - Data Rep. and Storage cont..pdf

    7/20

    ENGR 1200U

    Winter 2013 - UOIT

    STEP 2 Convert each group of four digits to is

    hexadecimal digit (or symbol)

    Example

    1 0 1 1 1 0 1 1 1

    1 7 7

    The last group

    on the left can

    have anywhere

    from 1 to 4

    binary digit

    group

    ENGR 1200U

    Winter 2013 - UOIT

    Converting hexadecimal numbers to binary isjust 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 3 - Data Rep. and Storage cont..pdf

    8/20

    ENGR 1200U

    Winter 2013 - UOIT

    Example:

    Given hexadecimal number 2EC3, what is theequivalent decimal value?

    163 162 161 160

    4096 256 16 1

    2 E C 3

    ENGR 1200U

    Winter 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 3 - Data Rep. and Storage cont..pdf

    9/20

    ENGR 1200U

    Winter 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 1200U

    Winter 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 3 - Data Rep. and Storage cont..pdf

    10/201

    ENGR 1200U

    Winter 2013 - UOIT

    Prefix Value Abbreviation______ ______ ____________

    milli 1/1,000 m

    micro 1/1,000,000

    nano 1/1,000,000,000 n

    pico 1/1,000,000,000,000 p

    ENGR 1200U

    Winter 2013 - UOIT

    kilobyte (KB) is 1,024 bytes

    megabyte (MB) is 1,048,576 bytes

    These values are derived from the nearest binaryplace values 1,000 and 1,000,000

  • 8/14/2019 Lecture 3 - Data Rep. and Storage cont..pdf

    11/201

    ENGR 1200U

    Winter 2013 - UOIT

    Data Types and Storage

    Negative Binary Numbers

    ENGR 1200U

    Winter 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 3 - Data Rep. and Storage cont..pdf

    12/201

    ENGR 1200U

    Winter 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 1200U

    Winter 2013 - UOIT

  • 8/14/2019 Lecture 3 - Data Rep. and Storage cont..pdf

    13/201

    ENGR 1200U

    Winter 2013 - UOIT

    Character Code HEX_________ _____ ____

    B 0100 0010 42

    b 0110 0010 62

    & 0010 0110 26

    [BACKSPACE] 0000 1000 8

    ENGR 1200U

    Winter 2013 - UOIT

    Two basic data types

    integerfloating

    point

  • 8/14/2019 Lecture 3 - Data Rep. and Storage cont..pdf

    14/201

    ENGR 1200U

    Winter 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 1200U

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

    negating all of the bits and adding one

  • 8/14/2019 Lecture 3 - Data Rep. and Storage cont..pdf

    15/201

    ENGR 1200U

    Winter 2013 - UOIT

    Ones Complement

    Has 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 complement is obsolete

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

    Binary # 10110011

    1s complement 01001100

    ENGR 1200U

    Winter 2013 - UOIT

    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: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 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 3 - Data Rep. and Storage cont..pdf

    16/201

    ENGR 1200U

    Winter 2013 - UOIT

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

    Digits to the left of the decimal point form the

    integral part of the number

  • 8/14/2019 Lecture 3 - Data Rep. and Storage cont..pdf

    17/201

    ENGR 1200U

    Winter 2013 - UOIT

    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

    ENGR 1200U

    Winter 2013 - UOIT

    What does the decimal value of 124 stand for?

    What is the decimal value of the binary value of110?

    1 2 4 rightmost place

    102 101 100

    x100 + x10 + x1

    124

    base

    1 1 0 rightmost place

    22 21 20

    x4 + x2 + x1

    110

    base

    124answer

    6answer

    1 21

  • 8/14/2019 Lecture 3 - Data Rep. and Storage cont..pdf

    18/201

    ENGR 1200U

    Winter 2013 - UOIT

    What is the decimal value of the octal value 176?

    What is the decimal value of the hexadecimalvalue of B29?

    1 7 6 rightmost place

    82 81 80

    x64 + x8 + x1

    176

    base

    B 2 9 rightmost place

    162 161 160

    x256 + x16 + x1

    B29

    base

    2857answer

    126answer

    ENGR 1200U

    Winter 2013 - UOIT

  • 8/14/2019 Lecture 3 - Data Rep. and Storage cont..pdf

    19/201

    ENGR 1200U

    Winter 2013 - UOIT

    Convert the following:

    1012 = ?10

    Begin with rightmost bit (ones place).

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

    1 0 1

    22 21 20

    x4 + x2 + x1

    5

    ENGR 1200U

    Winter 2013 - UOIT

    Convert the following:

    10102 = ?10

    Begin with rightmost bit (ones place).

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

    1 0 1 0

    23 22 21 20

    x8 + x4 + x2 + x1

    10

  • 8/14/2019 Lecture 3 - Data Rep. and Storage cont..pdf

    20/20

    ENGR 1200U

    Winter 2013 - UOIT

    Convert the following:

    2708 = ?10

    Begin with rightmost bit (ones place).

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

    2 7 0

    82 81 80

    x64+ x8 + x1

    184

    ENGR 1200U

    Winter 2013 - UOIT

    Convert the following:

    112A16 = ?10

    Begin with rightmost bit (ones place).

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

    1 1 2 A

    163 162 161 160

    x4096 + x256 + x16 + x1

    4394