02Data Representation

Embed Size (px)

Citation preview

  • 8/13/2019 02Data Representation

    1/46

  • 8/13/2019 02Data Representation

    2/46

    Outline Data Representation

    Compliments

  • 8/13/2019 02Data Representation

    3/46

    Data Types The data types stored in digital computers may

    be classified as being one of the followingcategories:

    1. numbers used in arithmetic computations,

    2. letters of the alphabet used in data processing, and

    3. other discrete symbols used for specific purposes.

    All types of data are represented in computers inbinary-coded form.

  • 8/13/2019 02Data Representation

    4/46

    Radix representation of

    numbers Radix or base: is the total number of

    symbols used to represent a value. A

    number system of radix r uses a stringconsisting of r distinct symbols torepresent a value.

  • 8/13/2019 02Data Representation

    5/46

    Radix representation of

    numbers Example: convert the following number to

    the radix 10 format. 97654.35

    The positions indicate the power of the radix.

    Start from the decimal point right to left we get0,1,2,3,4 for the whole numbers.

    And from the decimal point left to right

    We get -1, -2 for the fractions

    = 9x104+ 7x103 + 6x102 + 5x101 + 4x100+ 3x10-1 +

    5x10-2

  • 8/13/2019 02Data Representation

    6/46

    Binary Numbers Binary numbers are made of binary

    digits (bits):

    0 and 1

    Convert the following to decimal

    (1011)2= 1x23+ 0x22+ 1x21 +1x20 =

    (11)10

  • 8/13/2019 02Data Representation

    7/46

    Example

    Use radix representation to convert the binary number (101.01) into

    decimal.

    The position value is power of 2

    1 0 1. 0 1

    22 21 20 2-1 2-2

    4 + 0 + 1 + 0 + 1/22 = 5.25

    (101.01)2(5.25)10

    = 1 x 22+ 0 x 2 + 1 + 0 x 2-1+ 1 x 2-2

  • 8/13/2019 02Data Representation

    8/46

    Binary Addition

    1 1 1 1 0 1

    + 1 0 1 1 1

    ---------------------

    0

    1

    0

    1

    1

    1111

    1 1 00

    carries

    Example Add (11110)2to

    (10111)2

    (111101)2+ (10111) 2 = (1010100)2

    carry

  • 8/13/2019 02Data Representation

    9/46

    Binary Subtraction We can also perform subtraction (with borrows).

    Example: subtract (10111) from (1001101)

    1 10

    0 10 10 0 0 10

    1 0 0 1 1 0 1

    - 1 0 1 1 1

    ------------------------

    0 1 1 0 1 1 0

    borrows

    1+1=

    2

    (1001101)2- (10111)2= (0110110)2

  • 8/13/2019 02Data Representation

    10/46

    The Growth of Binary Numbers

    n 2n0 20=1

    1 21=2

    2 22=4

    3 23=8

    4 24=16

    5 25=32

    6 26=647 27=12

    8

    n 2n8 28=256

    9 29=512

    10 210=1024

    11 211=2048

    12 212=4096

    20 220=1M

    30 230=1G

    40 240

    =1T

    Mega

    Giga

    Tera

  • 8/13/2019 02Data Representation

    11/46

  • 8/13/2019 02Data Representation

    12/46

    Counting in Octal

    0 1 2 3 4 5 6 7

    10 11 12 13 14 15 16 17

    20 21 22 23 24 25 26 27

  • 8/13/2019 02Data Representation

    13/46

    Conversion Between Number Bases

    Decimal(base 10)

    Octal(base 8)

    Binary(base 2)

    Hexadecimal

    (base16)

    We normally convert to base 10because we are naturally used to the

    decimal number system.

    We can also convert to other number

    systems

  • 8/13/2019 02Data Representation

    14/46

    Converting an Integer from Decimal toAnother Base

    1. Divide the decimal number by the base (e.g. 2)

    2. The remainder is the lowest-order digit

    3. Repeat the first two steps until no div isorremains.

    4. For binary the even number has no remainder0, while the odd has 1

    For each digit position:

  • 8/13/2019 02Data Representation

    15/46

    Converting an Integer from Decimal toAnother Base

    Example for (13)10:Integer

    Quotient

    13/2 = (12+1) a0 = 1

    6/2 = ( 6+0 ) a1 = 0

    3/2 = (2+1 ) a2 = 11/2 = (0+1) a3 = 1

    Remainder Coefficient

    Answer (13)10 = (a3 a2 a1 a0)2 = (1101)2

  • 8/13/2019 02Data Representation

    16/46

    Converting a Fraction from DecimaltoAnother Base

    1. Multiply decimal number by the base (e.g. 2)2. The integer is the highest-order digit

    3. Repeat the first two steps until fraction becomes zero.

    For each digit position:

  • 8/13/2019 02Data Representation

    17/46

    Converting a Fraction from DecimaltoAnother Base

    Example for (0.625)10:

    Integer

    0.625 x 2 = 1 + 0.25 a-1 = 1

    0.250 x 2 = 0 + 0.50 a-2 = 0

    0.500 x 2 = 1 + 0 a-3 = 1

    Fraction Coefficient

    Answer (0.625)10 = (0.a-1 a-2 a-3 )2 = (0.101)2

  • 8/13/2019 02Data Representation

    18/46

    DECIMAL TO BINARY CONVERSION(INTEGER+FRACTION)

    (1) Separate the decimal number into integer and fraction parts.

    (2) Repeatedly divide the integer part by 2 to give a quotient and aremainder and

    Remove the remainder. Arrange the sequence of remaindersright to left from the period. (Least significant bit first)

    (3) Repeatedly multiply the fraction part by 2 to give an integerand a fraction part

    and remove the integer. Arrange the sequence of integers leftto right from the period. (Most significant fraction bit first)

  • 8/13/2019 02Data Representation

    19/46

    (Example) (41.6875)10 (?)2

    Integer = 41, Fraction = 0.6875

    The first procedure produces

    41 = 32+8+1

    = 1 x 25+ 0 x 24+ 1 x 23+ 0 x 22+ 0 x 2 + 1 = (101001)

    Integer remainder

    41 /2 1

    20 0

    10 05 1

    2 0

    1 1

    Overflow Fraction

    X by 2 .6875

    1 .3750

    0 .750

    1 .51 0

    .Closer tothe point

  • 8/13/2019 02Data Representation

    20/46

    Converting an Integer fromDecimal toOctal

    1. Divide decimal number by the base (8)

    2. The remainder is the lowest-order digit

    3. Repeat first two steps until no div isorremains.

    For each digit position:

  • 8/13/2019 02Data Representation

    21/46

    Converting an Integer fromDecimal toOctal

    Example for (175)10:

    IntegerQuotient

    175/8 = 21 + 7/8 a0 = 7

    21/8 = 2 + 5/8 a1 = 5

    2/8 = 0 + 2/8 a2 = 2

    Remainder Coefficient

    Answer (175)10 = (a2 a1 a0)2 = (257)8

  • 8/13/2019 02Data Representation

    22/46

    Converting an Integer fromDecimal toOctal

    1. Multiply decimal number by the base (e.g. 8)2. The integer is the highest-order digit

    3. Repeat first two steps until fraction becomeszero.

    For each digit position:

  • 8/13/2019 02Data Representation

    23/46

    Converting an Integer fromDecimal toOctal

    Example for (0.3125)10:

    Integer

    0.3125 x 8 = 2 + 0.5 a-1 = 2

    0.5000 x 8 = 4 + 0 a-2 = 4

    Fraction Coefficient

    Answer (0.3125)10 = (0.24)8

    Combine the two (175.3125)10 = (257.24)8

    Remainder

    of division

    Overflow of

    multiplication

  • 8/13/2019 02Data Representation

    24/46

    Hexadecimal Numbers

    Hexadecimal numbers are made of 16 symbols: (0,1,2,3,4,5,6,7,8,9,A, B, C, D, E, F)

    Convert a hexadecimal number to decimal (3A9F)16= 3x163+ 10x162 + 9x161 + 15x160 = 1499910

    Hexadecimal with fractions: (2D3.5)16= 2x16

    2 + 13x161 + 3x160+5x16-1 = 723.312510

    Note that eachhexadecimal digit can be represented

    with four bits. (1110) 2 = (E)16

    Groups of four bits are called a nibble. (1110) 2

  • 8/13/2019 02Data Representation

    25/46

    Example

    Convert the decimal number(107.00390625)10into hexadecimal number.

    (107.00390625)10(6B.01)16

    Integer remainder

    107 Divide/16

    6 11=B

    0 6

    Overflow FractionX by 16 .

    00390625

    0 .0625

    1 .0000

    .Closer to

    the period

  • 8/13/2019 02Data Representation

    26/46

    One to one comparison

    Binary, octal, andhexadecimal similar

    Easy to build circuits tooperate on theserepresentations

    Possible to convertbetween the threeformats

  • 8/13/2019 02Data Representation

    27/46

    Converting between Base 16 andBase 2

    Conversion is easy! Determine 4-bit value for each hex digit

    Note that there are 24= 16 different values offour bits which means each 16 value is

    converted to four binary bits. Easier to read and write in hexadecimal.

    Representations are equivalent!

    3A9F16= 0011 1010 1001 11112

    3 A 9 F

  • 8/13/2019 02Data Representation

    28/46

    Converting between Base 16 andBase 8

    1. Convert from Base 16 to Base 22. Regroup bits into groups of three starting from right

    3. Ignore leading zeros

    4. Each group of three bits forms an octal digit (8 isrepresented by 3 binary bits).

    352378= 011 101 010 011 1112

    5 2 3 73

    3A9F16= 0011 1010 1001 11112

    3 A 9 F

  • 8/13/2019 02Data Representation

    29/46

    ExampleConvert 101011110110011 to

    a. octal number

    b. hexadecimal number

    a. Each 3 bits are converted to octal :

    (101) (011) (110) (110) (011)

    5 3 6 6 3

    101011110110011 = (53663)8

    b. Each 4 bits are converted to hexadecimal:

    (0101) (0111) (1011) (0011)

    5 7 B 3

    101011110110011 = (57B3)16

    Conversion from binary to hexadecimal is similar except that the bits

    divided into groups of four.

    Binary Coded Decimal

  • 8/13/2019 02Data Representation

    30/46

    Binary Coded Decimal

    Binary coded decimal (BCD) represents each decimal digit

    with four bits

    Ex. 0011 0010 1001 = 32910

    This is NOTthe same as 0011001010012

    Why use binary coded decimal? Because people think indecimal.

    Digit BCD Code Digit BCD Code

    0 0000 5 0101

    1 0001 6 0110

    2 0010 7 0111

    3 0011 8 1000

    4 0100 9 1001

    3 2 9

  • 8/13/2019 02Data Representation

    31/46

    BCD versus other codes

    BCD not very efficient

    Used in earlycomputers (40s, 50s)

    Used to encodenumbers for seven-

    segment displays. Easier to read?

    (Example)

    The decimal 99 is represented

    by 1001 1001.

  • 8/13/2019 02Data Representation

    32/46

    ASCII Code

    American Standard Code for Information Interchange

    ASCII is a 7-bit code, frequently used with an 8th

    bit forerror detection (more about that in a bit).

    Character ASCII (bin) ASCII

    (hex)

    Decimal Octal

    A 1000001 41 65 101

    B 1000010 42 66 102

    C 1000011 43 67 103

    Z

    a

    1

  • 8/13/2019 02Data Representation

    33/46

    Outline

    Data Representation

    Compliments

  • 8/13/2019 02Data Representation

    34/46

    Subtraction using addition

    Conventional addition (using carry) is easily

    implemented in digital computers.

    However; subtraction by borrowing is difficultand inefficient for digital computers.

    Much more efficient to implement subtraction

    using ADDITION OF the COMPLEMENTS ofnumbers.

  • 8/13/2019 02Data Representation

    35/46

    Complements of numbers

    (r-1 )sComplement

    Given a numberN in base r having n digits,the (r- 1)scomplement ofN is defined as

    (rn- 1) - N

    For decimal numbers thebase or r =10 and r- 1=9,

    so the 9s complement ofN

    is (10n-1)-N

    99999. - N

    Digit

    n

    Digit

    n-1

    Next

    digit

    Next

    digit

    First

    digit

    9 9 9 9 9

    -

  • 8/13/2019 02Data Representation

    36/46

    2- Find the 9s complement of 546700 and 12389

    The 9s complement of 546700 is 999999 - 546700=453299

    and the 9s complement of 12389 is99999- 12389=87610.

    9s complementExamples

    5 4 6 7 0- 09 9 9 9 9 9

    4 5 3 2 9 9

    1 2 3 8- 99 9 9 9 9

    8 7 6 1 0

  • 8/13/2019 02Data Representation

    37/46

    ls complement

    For binary numbers, r =2 and r1=1,

    r-1s complement is the ls complement.

    The ls complement of N is (2^n- 1) - N.

    Digit

    n

    Digit

    n-1

    Next

    digit

    Next

    digit

    First

    digit

    1 1 1 1 1

    Bit n-1 Bit n-2 . Bit 1 Bit 0

    -

  • 8/13/2019 02Data Representation

    38/46

    ls complement

    Find r-1 complement for binary number N with four binary digits.

    r-1 complement for binary means 2-1 complement or 1s complement.

    n =4, we have 24=(10000)2 and 24 - 1 =(1111)2.

    The ls complement ofN is (24- 1) -N. = (1111) -N

  • 8/13/2019 02Data Representation

    39/46

    The complement 1s of

    1011001 is 0100110

    0 1 1 0 0- 1

    1 1 1 1 1 1

    1 0 0 1 1 0

    0 0 1 1 1- 11 1 1 1 1 1

    1 1 0 0 0 0

    1

    1

    0

    The 1s complement of

    0001111 is 1110000

    0

    1

    1

    ls complement

  • 8/13/2019 02Data Representation

    40/46

    rs Complement

    Given a numberN in base r having n digits,

    the rscomplement ofN is defined as

    rn- N.

    For decimal numbers the

    base or r =10,

    so the 10s complement ofNis 10n-N.

    100000. - N

    Digit

    n

    Digit

    n-1

    Next

    digit

    Next

    digit

    First

    digit

    0 0 0 0 0

    -

    1

  • 8/13/2019 02Data Representation

    41/46

    10s complementExamples

    Find the 10s complement of

    546700 and 12389

    The 10s complement of 546700is 1000000 - 546700= 453300

    and the 10s complement of

    12389 is

    100000 - 12389=87611.

    Notice that it is the same as 9s

    complement + 1.

    5 4 6 7 0- 0

    0 0 0 0 0 0

    4 5 3 3 0 0

    1 2 3 8- 91 0 0 0 0 0

    8 7 6 1 1

    1

  • 8/13/2019 02Data Representation

    42/46

    For binary numbers, r =2,

    rs complement is the 2s complement.

    The 2s complement ofN is 2n -N.

    2s complement

    Digit

    n

    Digit

    n-1

    Next

    digit

    Next

    digit

    First

    digit

    0 0 0 0 0

    -

    1

  • 8/13/2019 02Data Representation

    43/46

    2s complement Example

    The 2s complement of

    1011001 is 0100111

    The 2s complement of

    0001111 is 1110001

    0 1 1 0 0- 10 0 0 0 0 0

    1 0 0 1 1 1

    0 0 1 1 1

    -1

    1 1 0 0 0 1

    1

    0

    0

    0

    1

    1

    0 0 0 0 0 001

  • 8/13/2019 02Data Representation

    44/46

    Fast Methods for 2sComplement

    Method 1:The 2s complement of binary number is obtained by adding 1 to the

    ls complement value.

    Example:

    1s complement of 101100 is 010011 (invert the 0s and 1s)

    2s complement of 101100 is 010011 + 1 =010100

  • 8/13/2019 02Data Representation

    45/46

    Fast Methods for 2sComplement

    Method 2:The 2scomplement can be formed by leaving all least significant 0s

    and the first 1 unchanged, and then replacing lsby 0sand 0sby lsin all other higher significant bits.

    Example:

    The 2s complement of 1101100is

    0010100

    Leave the two low-order 0s and the first 1 unchanged, and then

    replacing 1s by 0s and 0s by 1s in the four most significant bits.

    E l

  • 8/13/2019 02Data Representation

    46/46

    Examples

    Finding the 2s complement of (01100101)2

    Method 1 Simply complement each bit andthen add 1 to the result.

    (01100101)2[N] = 2s complement = 1s complement (10011010)2+1

    =(10011011)2

    Method 2Starting with the least significant bit,

    copy all the bits up to and including the first 1 bitand then complement the remaining bits.

    N = 0 1 1 0 0 1 0 1

    [N] = 1 0 0 1 1 0 1 1