lec7 data representation

Upload: deep-an

Post on 07-Apr-2018

224 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/6/2019 Lec7 Data Representation

    1/32

    Chapter 3Data Representation

    Dr. Bernard Chen Ph.D.

    University of Central ArkansasSpring 2009

  • 8/6/2019 Lec7 Data Representation

    2/32

    Data Types The data types stored in digital computers may be

    classified as being one of the following categories:

    1. numbers used in arithmetic computations,2. letters of the alphabet used in data processing, and3. other discrete symbols used for specific purposes.

    All types of data are represented in computers in binary-

    coded form.

  • 8/6/2019 Lec7 Data Representation

    3/32

    Radix representation of

    numbers Radix or base : is the total number

    of symbols used to represent avalue. A number system of radix ruses a string consisting of r distinctsymbols to represent a value.

  • 8/6/2019 Lec7 Data Representation

    4/32

  • 8/6/2019 Lec7 Data Representation

    5/32

    Binary NumbersBinary numbers are made of b inary dig it s ( bit s):0 and 1

    Convert the following to decimal(1011) 2 = 1x2 3 + 0x2 2 + 1x2 1 + 1x2 0

    = (11) 10

  • 8/6/2019 Lec7 Data Representation

    6/32

    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/2 2 = 5.25(101.01) 2 (5.25) 10

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

  • 8/6/2019 Lec7 Data Representation

    7/32

    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) 2 to

    (10111) 2

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

  • 8/6/2019 Lec7 Data Representation

    8/32

    Binary SubtractionWe 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/6/2019 Lec7 Data Representation

    9/32

    The Growth of BinaryNumbers

    n 2n

    0 2 0=11 2 1=22 2 2=43 2 3=84 2 4=165 2 5=32

    6 26

    =647 2 7=128

    n 2n

    8 2 8=2569 2 9=512

    10 2 10 =1024

    11 2 11 =2048

    12 2 12 =4096

    20 2 20 =1M30 2 30 =1G

    40 240

    =1T

    Mega

    Giga

    Tera

  • 8/6/2019 Lec7 Data Representation

    10/32

    Octal Numbers

    Octal numbers (Radix or base=8) are made of octaldigits: (0,1,2,3,4,5,6,7)

    How many items does an octal number represent?

    Convert the following octal number to decimal(465.27) 8 = 4x8 2+ 6x8 1+ 5x8 0 + 2x8 -1+ 7x8 -2

  • 8/6/2019 Lec7 Data Representation

    11/32

    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/6/2019 Lec7 Data Representation

    12/32

    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/6/2019 Lec7 Data Representation

    13/32

    Converting an Integer from Decimal to AnotherBase

    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 divisor remains.

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

    For each digit position:

  • 8/6/2019 Lec7 Data Representation

    14/32

    Converting an Integer from Decimalto

    Another Base

    Example for (13) 10 :Integer Quotient

    13/2 = (12+1) a 0 = 1

    6/2 = ( 6+0 ) a 1 = 03/2 = (2+1 ) a 2 = 11/2 = (0+1) a 3 = 1

    Remainder Coefficient

    Answer (13) 10 = (a 3 a 2 a 1 a 0)2 = (1101) 2

  • 8/6/2019 Lec7 Data Representation

    15/32

    Converting a Fraction from

    Decimalto

    Another 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/6/2019 Lec7 Data Representation

    16/32

  • 8/6/2019 Lec7 Data Representation

    17/32

    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 quotientand a remainder 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 partand remove the integer. Arrange the sequence of integers

    left to right from the period. (Most significant fraction bitfirst)

  • 8/6/2019 Lec7 Data Representation

    18/32

    (Example) (41.6875) 10 (?)2

    Integer = 41, Fraction = 0.6875

    The first procedure produces

    41 = 32+8+1

    = 1 x 2 5 + 0 x 2 4 + 1 x 2 3 + 0 x 2 2 + 0 x 2 + 1

    Integer remainder 41 /2 120 0

    10 05 12 01 1

    Overflow Fraction

    X by 2 .6875

    1 .3750

    0 .750

    1 .51 0

    .Closer tothe point

  • 8/6/2019 Lec7 Data Representation

    19/32

    Converting an Integer fromDecimal to Octal

    1. Divide decimal number by the base (8)

    2. The remainder is the lowest-order digit

    3. Repeat first two steps until no divisor remains.

    For each digit position:

  • 8/6/2019 Lec7 Data Representation

    20/32

    Converting an Integer fromDecimal to Octal

    Example for (175) 10 :Integer Quotient

    175/8 = 21 + 7/8 a 0 = 721/8 = 2 + 5/8 a 1 = 5

    2/8 = 0 + 2/8 a 2 = 2

    Remainder Coefficient

    Answer (175) 10 = (a 2 a 1 a 0)2 = (257) 8

  • 8/6/2019 Lec7 Data Representation

    21/32

    Converting an Integer fromDecimal to Octal

    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/6/2019 Lec7 Data Representation

    22/32

  • 8/6/2019 Lec7 Data Representation

    23/32

    Hexadecimal NumbersHexadecimal 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

    = 14999 10 Hexadecimal with fractions:

    (2D3.5) 16 = 2x16 2+ 13x16 1+ 3x16 0 + 5x16 -1 = 723.3125 10Note that each hexadecimal digit can be representedwith four bits.

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

    (1110) 2

  • 8/6/2019 Lec7 Data Representation

    24/32

    ExampleConvert the decimal number(107.00390625)10 into hexadecimalnumber.

    (107.00390625)10 (6B.01)16

    Integer remainder

    107 Divide/16

    6 11=B

    0 6

    Overflow Fraction

    X by 16 .00390625

    0 . 06251 . 0000

    .Closer to

    the period

  • 8/6/2019 Lec7 Data Representation

    25/32

    One to one comparisonBinary, octal, andhexadecimal similarEasy to build circuitsto operate on theserepresentationsPossible to convertbetween the threeformats

  • 8/6/2019 Lec7 Data Representation

    26/32

    Converting between Base 16and Base 2

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

    Note that there are 2 4 = 16 different values of four bits which means each 16 value is

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

    Representations are equivalent!

    3A9F 16 = 0011 1010 1001 1111 23 A 9 F

  • 8/6/2019 Lec7 Data Representation

    27/32

    Converting between Base 16and Base 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 is

    35237 8 = 011 101 010 011 111 25 2 3 73

    3A9F 16 = 0011 1010 1001 1111 23 A 9 F

  • 8/6/2019 Lec7 Data Representation

    28/32

    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 3101011110110011 = (57B3) 16

    Conversion from binary to hexadecimal is similar except that the bitsdivided into groups of four.

    Bi C d d

  • 8/6/2019 Lec7 Data Representation

    29/32

    Binary CodedDecimal Binary coded decimal (BCD) represents each decimal digit with four bits

    Ex. 0011 0010 1001 = 329 10

    This is NOT the same as 001100101001 2 Why use binary coded decimal? Because people think in decimal.

    Digit BCD Code Digit BCD Code

    0 0000 5 0101

    1 0001 6 01102 0010 7 01113 0011 8 10004 0100 9 1001

    3 2 9

  • 8/6/2019 Lec7 Data Representation

    30/32

    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/6/2019 Lec7 Data Representation

    31/32

  • 8/6/2019 Lec7 Data Representation

    32/32