Lect - 2.ppt

Embed Size (px)

Citation preview

  • 7/27/2019 Lect - 2.ppt

    1/28

    UNSIGNED BINARY NUMBERS In some applications, data either

    positive or negative.

    We concentrate on absolute value ormagnitude only.

    For example :

    Smallest 8-bit number is 0000 0000Largest 8-bit number is 1111 1111

    Equivalent to a decimal 0 to 255.

  • 7/27/2019 Lect - 2.ppt

    2/28

    Data is called unsigned binarybecauseall bits are used to represent magnitudeof the corresponding decimal only

    Limitations: With 8-bit unsigned arithmetic, all

    magnitudes must fall in the range 0 to255

    Addition and subtraction must also fallbetween 0 and 255.

    If greater than 255, use 16-bit

    arithmetic

  • 7/27/2019 Lect - 2.ppt

    3/28

    OVERFLOW In 8-bit arithmetic , addition of 2

    unsigned numbers whose sum is

    greater than 255 , causes overflow.

    a carry into 9th column

    Microprocessors have carry flag to

    detect a carry

    which warns , answer is invalid.

  • 7/27/2019 Lect - 2.ppt

    4/28

    Example Addition of 175 10 and 118 10

    175 1010 1111118 0111 0110293 1 0010 0101

    In case of overflow, programmer has tocarry instructions for carry flag and use16-bit arithmetic.

  • 7/27/2019 Lect - 2.ppt

    5/28

    Signed Integer Representation We have been ignoring signed

    integers.

    The PROBLEMwith signed integers ( -

    45, + 27, -99) is the SIGN!

    How do we encode the sign?

    The sign is an extra piece of information

    that has to be encoded in addition to the

    magnitude.

  • 7/27/2019 Lect - 2.ppt

    6/28

    What can we do?

    Following are the three possible

    techniques

    Signed Magnitude Representation

    Diminished Radix-Complement Representation

    Radix-Complement Representation

  • 7/27/2019 Lect - 2.ppt

    7/28

    Signed Magnitude

    Representation

    Signed Magnitude (SM) is a method forencoding signed integers.

    The Most Significant Bit (MSB) is used

    to represent the sign.

    1 is used for a - (negative sign),

    0 for a + (positive sign).

  • 7/27/2019 Lect - 2.ppt

    8/28

    Format of SM Representation The format of a SM number in 8 bits is:

    smmmmmmm Where s is the sign bit

    The other 7 bits represent the magnitude.

    NOTE: for positive numbers, the result isthe same as the unsigned binary

    representation.

  • 7/27/2019 Lect - 2.ppt

    9/28

    Signed Magnitude Ex. (8 bits) -5 = (1 0000101)2 = (85)16 +5 = (0 0000101)2 = (05)16

    +127 = (0 1111111)2 = (7F)16

    -127 = (1 1111111)2 = (FF)16

    + 0 = (0 0000000)2 = (00)16 - 0 = (1 0000000)2 = (80)16

  • 7/27/2019 Lect - 2.ppt

    10/28

    For N bits, can represent the signedintegers

    - {2(N-1)

    1} to {+ 2(N-1)

    1} For 8 bits, can represent the signed integers

    -127 to +127.

    Signed magnitude easy to understand andencode.

    Simple. Negative numbers are identical topositive numbers except the sign bit.

    Used today in some applications.

  • 7/27/2019 Lect - 2.ppt

    11/28

    Problems with Signed Magnitude Representation

    One problem is that it has two ways of

    representing 0 (-0,and +0)

    Another problem is that addition of

    K + (-K) does not give Zero!

    -5 + 5 = (85)16

    + (05)16

    = (8A)16

    Requires complicated arithmetic

    circuits.

  • 7/27/2019 Lect - 2.ppt

    12/28

    Ones Complement

    Representation

    Ones complement is another way torepresent signed integers.

    To encode a negative number, get thebinary representation of its magnitude,then COMPLEMENT each bit.

    Complementing each bit mean that 1sare replaced with 0s, 0s are replacedwith 1s.

  • 7/27/2019 Lect - 2.ppt

    13/28

    Example: How is -5 represented in Ones

    Complement (encoded in 8 bits) ?

    The magnitude 5 in 8-bits is( 00000101)2 = (05)16

    Now complement each bit:

    (11111010)2 = (FA)16 (FA)16 is ones complement

    representation of -5.

  • 7/27/2019 Lect - 2.ppt

    14/28

    Ones Complement Examples

    +5 = (00000101)2 = ( 05 )16

    -5 = (11111010) = ( FA )16

    +127 = (01111111)2 = ( 7F )16

    -127 = (10000000)2 = ( 80 )16

    + 0 = (00000000)2 = ( 00 )16 - 0 = (11111111)2 = ( FF )16

  • 7/27/2019 Lect - 2.ppt

    15/28

    For N bits, can represent the signedintegers

    - {2(N-1) 1} to + {2(N-1) 1}

    For 8 bits, can represent the signed

    integers -127 to +127.

  • 7/27/2019 Lect - 2.ppt

    16/28

    Limitations of Ones

    Complement Method

    Still have the problem that there are twoways of representing 0 (-0, and +0)

    Mathematically speaking, no such thing astwo representations for zeros.

    However, addition of K + (-K) now gives

    Zero!

    -5 + 5 = ( FA )16 + ( 05 )16

    = ( FF)16

    = -0 !!!

  • 7/27/2019 Lect - 2.ppt

    17/28

    Unfortunately, K + 0 = K only works

    if we use +0,

    Does not work if we use -0.

    5 + (+0) = (05)16 + (00)16 = (05)16 = 5 (ok)

    5 + (-0) = (05)16 + (FF)16= (04)16 = 4(wrong)

  • 7/27/2019 Lect - 2.ppt

    18/28

    Twos Complement

    Representation

    Twos complement is another way torepresent signed integers.

    To encode a negative number, get the binaryrepresentation of its magnitude,

    COMPLEMENT each bit, then ADD 1.

    or

    See 1 from LSB , mark it and complement all

    bits which are occurring before it.

  • 7/27/2019 Lect - 2.ppt

    19/28

    Example

    What is -5 in Twos Complement, 8 bits?

    The magnitude 5 in 8-bits is:

    (00000101)2 = (05)16

    Now complement each bit:

    (11111010)2 = (FA)16

    Now add one: (FA) 16 + 1 = (FB)16

    (FB)16 is the 8-bit, twos complement

    representation of -5.

  • 7/27/2019 Lect - 2.ppt

    20/28

    Twos Complement Examples

    -5 = ( 11111011) = ( FB)16 +5 = ( 00000101 )= ( 05)16

    +127 = ( 01111111) = (7F)16

    -127 = ( 10000001) = ( 81)16

    -128 = ( 10000000 )= ( 80 )16

    + 0 = ( 00000000) = (00)16

    - 0 = ( 00000000) = (00 )16 (only 1 zero!!!)

  • 7/27/2019 Lect - 2.ppt

    21/28

    For N bits, can represent the signed

    integers

    -2(N-1) to + 2(N-1) - 1

    Note that negative range extends one

    more than positive range.

    For 8 bits, can represent the signed

    integers -128 to +127.

  • 7/27/2019 Lect - 2.ppt

    22/28

    Twos Complement

    Comments

    Twos complement is the method of choicefor representing signed integers.

    It has none of the drawbacks of SignedMagnitude or Ones Complement.

    There is only one zero, and K + (-K) = 0.

    -5 + 5 = (FB)16 + ( 05)16 = (00)16 = 0 !!!

    Normal binary addition is used for addingnumbers that represent twos complementintegers.

  • 7/27/2019 Lect - 2.ppt

    23/28

    Complements

    Used in digital computer forsimplifying the subtraction operation

    and for logic manipulations. There are two types of complements

    for each base R system Diminished Radix-Complement Representation

    [ (R-1)s Complement]

    Radix-Complement Representation [ RsComplement ]

  • 7/27/2019 Lect - 2.ppt

    24/28

    (1) The rs complement. e.g. 2scomplement for binary and 10scomplement for decimal.

    (2) The (r-1)s complement. e.g. 1scomplement for binary and 9scomplement for decimal.

  • 7/27/2019 Lect - 2.ppt

    25/28

    (R-1)'s Complement Represen tation

    +ve numbers. : sign and magnitude

    -ve numbers : -N represented by , the (R-1)'scomplement

    where = ( Rn R-m ) N,

    n = Total number of digits in integerpart of the number N

    m = Total number of digits in fractionalpart of the number N

  • 7/27/2019 Lect - 2.ppt

    26/28

    Examples

    9s complements of (52520)10(105-100-52520)= (105-1-

    52520)=47479

    9s complements of (0.3267)10(100-10-4-0.3267)= (0.9999- 0.3267)

    =0.6732

  • 7/27/2019 Lect - 2.ppt

    27/28

    R's Complement Representat ion

    +ve nos.: sign and magnitude

    -ve nos. : -N is represented by N*, the

    R's Complementwhere N* = Rn N

    n = Total number of digits in integer

    part of the number N

  • 7/27/2019 Lect - 2.ppt

    28/28

    Examples:

    10s complements of (52520)10

    (105

    -52520) = (105

    -52520)= 47480

    10s complements of (0.3267)10

    (100-0.3267) = (1.0- 0.3267)= 0.6733