Lect4 Base Conversion

Embed Size (px)

Citation preview

  • 8/13/2019 Lect4 Base Conversion

    1/17

    TA C162

    Lecture 4 Base Conversion

  • 8/13/2019 Lect4 Base Conversion

    2/17

    Todays Agenda

    Base Conversion

    Binary to Decimal - Recap

    Decimal to Binary Decimal to Octal & Octal to Decimal

    Octal to Binary & Binary to Octal

    Binar to Hexadecimal & Hexadecimal to

    Binary

    Saturday, January 16, 2010 2Biju K Raveendran@BITS Pilani.

  • 8/13/2019 Lect4 Base Conversion

    3/17

    What is Base? Decimal Number System

    Base is 10

    All numbers are represented by 0 to 9

    Binary Number System

    ase s

    All numbers are represented by 0 and 1

    Base is 8

    Inference

    -

    Saturday, January 16, 2010 3Biju K Raveendran@BITS Pilani.

  • 8/13/2019 Lect4 Base Conversion

    4/17

    Converting Binary (2s C) to Decimal8-bit 2s complement Number is of the forma7 a6 a5 a4 a3 a2 a1 a0

    n 2n

    0 1

    ea ng a7 s one; en e num er s

    negative take twos complement to get a

    positive number; Otherwise number is positive.

    1 2

    2 4

    3 8

    The Magnitude is:

    a6.26+a5.25+a4.24+a3.23+a2.22+a1.21+a0.20

    4 16

    5 32

    Add powers of 2 that have 1 in the7 128

    8 256

    . If original number was negative, affix a minus

    sign in front.

    9 512

    10 1024

    ssum ng - s comp emen num ers.

    Saturday, January 16, 2010 4Biju K Raveendran@BITS Pilani.

  • 8/13/2019 Lect4 Base Conversion

    5/17

  • 8/13/2019 Lect4 Base Conversion

    6/17

    Addition & SubtractionExample 1: 5 + 4 ? (in 5 bit notation)5 001014 00100

    Example 2: 9 + - 12 ? (in 5 bit notation)9 01001

    -12 101009 + (-12) 11101 (-3)Example 3: -3 + -7 ? (in 5 bit notation)

    3 00011- 7 00111-7 11001

    - + - -Saturday, January 16, 2010 6Biju K Raveendran@BITS Pilani.

  • 8/13/2019 Lect4 Base Conversion

    7/17

    Converting Decimal to Binary (2s C)

    First Method: Division

    +

    Divide by two remainder is least significant bit. Keep dividing by two until answer is zero, writing

    remainders from right to left.

    Append a zero as the MS bit.

    If original number was negative, take twos

    . Example: X = 105ten

    Saturday, January 16, 2010 7Biju K Raveendran@BITS Pilani.

  • 8/13/2019 Lect4 Base Conversion

    8/17

    Example:

    X = 105ten

    =

    52/2 = 26 r 0 bit 1

    = r

    13/2 = 06 r 1 bit 36/2 = 03 r 0 bit 4

    3/2 = 01 r 1 bit 5

    1/2 = 0 r 1 bit 6

    X = 01101001

    Saturday, January 16, 2010 8Biju K Raveendran@BITS Pilani.

  • 8/13/2019 Lect4 Base Conversion

    9/17

    Converting Decimal to Binary (2s C) Second Method: Subtract Powers of Two

    n

    Subtract largest power of two less than or equalto number.

    0 1

    1 2

    2 4

    Put a 1 in the corresponding bit position.

    Keep subtracting until result is zero.

    3 8

    4 16

    5 32

    ppen a zero as ; or g na was

    negative, take twos complement.

    Put a 0 in the other bit ositions

    6 64

    7 128

    8 256

    10 1024

    Saturday, January 16, 2010 9Biju K Raveendran@BITS Pilani.

  • 8/13/2019 Lect4 Base Conversion

    10/17

    Example:

    = = en

    40 32 = 8 bit 6 is 1

    X = 01101000two

    Saturday, January 16, 2010 10Biju K Raveendran@BITS Pilani.

  • 8/13/2019 Lect4 Base Conversion

    11/17

    Algorithm

    -

    Step 1:

    Step 2:

    ,

    If N is negative, then form negative of this 2s

    Saturday, January 16, 2010 11Biju K Raveendran@BITS Pilani.

  • 8/13/2019 Lect4 Base Conversion

    12/17

    Decimal to Octal1. Find magnitude of decimal number. (Always +ve)

    2. Divide by Eight remainder is least significant bit.

    . eep v ng y g un answer s zero, wr ng

    remainders from right to left.

    Example: Convert 87410 to its octal equivalent.

    Octal to Decimal

    Sum the multiplication of each digit with its position value

    Example: Convert 6248 to its decimal equivalent.6 x 82 + 2 x 81 + 4 x 80 = 40410

    Saturday, January 16, 2010 12Biju K Raveendran@BITS Pilani.

  • 8/13/2019 Lect4 Base Conversion

    13/17

  • 8/13/2019 Lect4 Base Conversion

    14/17

    Hexadecimal Notation It is more convenient to write binar base-2 numbers as

    hexadecimal (base-16) numbers.

    Why?

    ewer g s -- our s per ex g se u or ea ng w ong s r ngs

    of binary digits)

    Less error prone -- easy to corrupt long string of 1s and 0s

    Binary Hex Decimal

    0000 0 0

    Binary Hex Decimal

    1000 8 8

    0010 2 2

    0011 3 3

    1010 A 10

    1011 B 11

    0101 5 5

    0110 6 6

    1101 D 13

    1110 E 14

    Saturday, January 16, 2010 14Biju K Raveendran@BITS Pilani.

  • 8/13/2019 Lect4 Base Conversion

    15/17

    Hexadecimal NotationA 16-bit binary string takes the forma15 a14 a13 a12 a11 a10 a9 a8 a7 a6 a5 a4 a3 a2 a1 a0

    uns gne n eger s va ue can e compu e as

    a15.215 + a14.2

    14 + a13.213 + a12.2

    12 + a11.211 + a10.2

    10 + a9.29 +

    8 7 6 5 4 3 2 1 08. 7. 6. 5. 4. 3. 2. 1. 0.

    Can be represented as Observe212 [a15.2

    3 + a14.22 + a13.2

    1 + a12.20] +

    28 [a11.23 + a10.2

    2 + a9.21 + a8.2

    0] +

    Value inside the

    square bracket

    a7.

    a6.

    a5.

    a4. 20 [a3.2

    3 + a2.22 + a1.2

    1 + a0.20]

    = 3 2 1 0

    0 to 15

    . . . .

    Saturday, January 16, 2010 15Biju K Raveendran@BITS Pilani.

  • 8/13/2019 Lect4 Base Conversion

    16/17

  • 8/13/2019 Lect4 Base Conversion

    17/17