Lect3 Number Representation

download Lect3 Number Representation

of 24

Transcript of Lect3 Number Representation

  • 7/29/2019 Lect3 Number Representation

    1/24

    TA C162

    Lecture 3 Representation of Binary Numbers

  • 7/29/2019 Lect3 Number Representation

    2/24

    Todays Agenda Representation of Binary Numbers

    Unsigned, Signed Magnitude, 1s Compliment

    Done !!! 2s Complement

    Base Conversion Binary to Decimal and Decimal to Binary

    Decimal to Octal and Octal to Decimal

    Binary to Hexadecimal and Hexadecimal to Binary

    Tuesday, January 12, 2010 2Biju K Raveendran@BITS Pilani.

  • 7/29/2019 Lect3 Number Representation

    3/24

    How to represent Signed Integers

    2s complement representation

    Tuesday, January 12, 2010 3Biju K Raveendran@BITS Pilani.

  • 7/29/2019 Lect3 Number Representation

    4/24

    Representation of Signed Integers Cont

    Ex: n = 3

    Signed

    magnitude

    000 +0

    010 +2

    100 -0

    101 -1

    110 -2

    111 -3

    Tuesday, January 12, 2010 4Biju K Raveendran@BITS Pilani.

  • 7/29/2019 Lect3 Number Representation

    5/24

    1s Complement Representation Positive numbers representation is same as signed

    integers.

    Negative numbers represented by flippingall the bits ofcorresponding positive numbers

    For Example:

    +5 is re resented as 00101

    -5 is represented by 11010

    This representation was used in some early computers

    Tuesday, January 12, 2010 5Biju K Raveendran@BITS Pilani.

  • 7/29/2019 Lect3 Number Representation

    6/24

    Question Given n bit string, What is the Maximum number we can representin 1s complement form?

    he Maximum +ve value is : 2n-1 -1

    The Maximum -ve value is : 2n-1 -1

    Ex: Given 5 bits,

    . - =

    Min. number in signed magnitude form is: -24 -1= -15

    Tuesday, January 12, 2010 6Biju K Raveendran@BITS Pilani.

  • 7/29/2019 Lect3 Number Representation

    7/24

    Representation of Signed Integers Cont

    Ex: n = 3

    Signed 1s

    magnitude complement

    000 + 0 + 0

    010 + 2 + 2

    100 0 3

    101 1 2

    110 2 1

    111 3 0

    Tuesday, January 12, 2010 7Biju K Raveendran@BITS Pilani.

  • 7/29/2019 Lect3 Number Representation

    8/24

    Example: 1s ComplementExample 1:How will we represent-12in 1s complement form in 5 digits?

    Step1: Take +12 in binary representation

    Step2: Flip all the bits of the above

    10011

    Inference

    -12 representation in 1s complement form: 10011

    +12 representation in 1s complement form: 01100

    Tuesday, January 12, 2010 8Biju K Raveendran@BITS Pilani.

  • 7/29/2019 Lect3 Number Representation

    9/24

    Representation of Signed IntegersEx: n = 4 Signed magnitude 1s Complement

    0000 + 0 + 0

    0001 + 1 + 1

    0010 + 2 + 2

    0011 + 3 + 3

    0100 + 4 + 4

    0101 + 5 + 5

    0110 + 6 + 6

    0111 + 7 + 7

    1000 0 7

    1001 1 6

    1010 2 5

    1011 3 4

    1100 4 31101 5 2

    1110 6 1

    1111 7 0

    Tuesday, January 12, 2010 9Biju K Raveendran@BITS Pilani.

  • 7/29/2019 Lect3 Number Representation

    10/24

    Limitations of Signed magnitude

    Problems with sign-magnitude and 1s complement!

    Two representations of zero (+0 and 0)

    Arithmetic circuits are complex to implement aboverepresentation (hardware complexity is more)

    How to add two sign-magnitude numbers?

    e.g., try 2 + (-3)00010

    10011

    10101 => -5 ??

    How to add two ones complement numbers?

    e.g., ry -

    Tuesday, January 12, 2010 10Biju K Raveendran@BITS Pilani.

  • 7/29/2019 Lect3 Number Representation

    11/24

    A Good Representation

    Representation of Negative Numbers should makeArithmetic & Logic Unit (ALU) simple

    Addition of an arbitrary integer with same magnitude butopposite sign should return 0

    .

    A + (-A) = 0

    From (2n-1-1) to +(2n-1-1) ALU adds 1 to successive representations

    Mathematically we can represent as:

    REPRESENTATION(value+1) =REPRESENTATION(value) + REPRESENTATION(1)

    Tuesday, January 12, 2010 11Biju K Raveendran@BITS Pilani.

  • 7/29/2019 Lect3 Number Representation

    12/24

    Twos Complement Representation If number is positive or zero Normal binary representation

    Start with positive number Flip every bit (i.e., take the ones complement)

    Then add one

    Example:

    11010 (1s comp) 10110 (1s comp)

    + 1 + 1

    11011 (-5) 10111 (-9)

    Tuesday, January 12, 2010 12Biju K Raveendran@BITS Pilani.

  • 7/29/2019 Lect3 Number Representation

    13/24

    Example Represent -12 in 2s complement form (5

    Step 1:Represent +12 in binary

    Step 2: Find 1s complement of that

    Step 3: Add 00001 to 1s complement

    Result (-12 in 2s complement form) is

    Tuesday, January 12, 2010 13Biju K Raveendran@BITS Pilani.

  • 7/29/2019 Lect3 Number Representation

    14/24

    Twos Complement Shortcut To take the twos complement of a number: Copy bits from right to left until (and including) the

    first 1 Flip remaining bits to the left

    011010000 011010000

    + 1

    100110000 100110000

    (copy)(flip)

    Tuesday, January 12, 2010 14Biju K Raveendran@BITS Pilani.

  • 7/29/2019 Lect3 Number Representation

    15/24

    Advantage of 2s complement Twos complementrepresentation developed to makecircuits easy for arithmetic.

    -, ,

    such that X + (-X) = 0 with normal addition, ignoring carry out

    00101 (5) 01001 (9)

    + 11011 (-5) +10111 (-9)00000 (0) 00000 (0)

    Tuesday, January 12, 2010 15Biju K Raveendran@BITS Pilani.

  • 7/29/2019 Lect3 Number Representation

    16/24

    Exam le:2+(-3) =?? (in 5 digits)

    Ex ress -3 in 2s com lement form:

    +3 000111s Complement of 3 11100

    s omp emen o .e. -+2 00010+ +

    -3 1110111111

    Look at MSB 1 So take 2s complement again 00001

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

  • 7/29/2019 Lect3 Number Representation

    17/24

    Twos Complement Signed Integers MS bit is sign bit: it has weight(2n-1)

    Range of an n-bit number: (2n-1)through 2n-1 1.-

    - .

    23 22 21 20 23 22 21 20

    -

    0 0 0 1 1

    0 0 1 0 2

    1 0 0 1 -7

    1 0 1 0 -6

    0 0 1 1 3

    0 1 0 0 4

    1 0 1 1 -5

    1 1 0 0 -4

    0 1 0 1 5

    0 1 1 0 6

    1 1 0 1 -3

    1 1 1 0 -2

    0 1 1 1 7 -

    Tuesday, January 12, 2010 17Biju K Raveendran@BITS Pilani.

  • 7/29/2019 Lect3 Number Representation

    18/24

    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

    -

    Tuesday, January 12, 2010 18Biju K Raveendran@BITS Pilani.

  • 7/29/2019 Lect3 Number Representation

    19/24

    Converting Binary (2s C) to Decimal8-bit 2s complement Number is of the form

    a7 a6 a5 a4 a3 a2 a1 a0n 2n

    0 1

    ea ng a7 s one; en e num er s

    negative take twos complement to get apositive 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 minussign in front.

    9 51210 1024

    ssumng - s comp emen num ers.

    Tuesday, January 12, 2010 19Biju K Raveendran@BITS Pilani.

  • 7/29/2019 Lect3 Number Representation

    20/24

    Example: Binary to Decimal Conversion

    X = 01101000two

    Step 1: Leading bit is 0 no need to take 2s complementX = 01101000

    Step 2: Add powers of 2 that have 1 in the correspondingt pos tons.

    = 26+25+23 = 64+32+8

    ten

    Tuesday, January 12, 2010 20Biju K Raveendran@BITS Pilani.

  • 7/29/2019 Lect3 Number Representation

    21/24

    More Examples

    n 2nX =00100111two1 2

    2 4

    3 8

    = + + + = + + +X =39ten

    4 16

    5 32

    6 64

    X =11100110two- = 7 128

    8 256

    9 512

    =24+23+21=16+8+2=26ten

    X =-26ten

    Assuming 8-bit 2s complement numbers.

    Tuesday, January 12, 2010 21Biju K Raveendran@BITS Pilani.

  • 7/29/2019 Lect3 Number Representation

    22/24

    Examples

    = wo

    X=10001100two

    Tuesday, January 12, 2010 22Biju K Raveendran@BITS Pilani.

  • 7/29/2019 Lect3 Number Representation

    23/24

    Algorithm

    -

    Step 1:

    Step 2:

    ,

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

    Tuesday, January 12, 2010 23Biju K Raveendran@BITS Pilani.

  • 7/29/2019 Lect3 Number Representation

    24/24

    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

    - + - -Tuesday, January 12, 2010 24Biju K Raveendran@BITS Pilani.