10 subtraction

Upload: santoshkrishna12

Post on 06-Mar-2016

33 views

Category:

Documents


0 download

DESCRIPTION

10 Subtraction

TRANSCRIPT

  • 7/21/2019 10 Subtraction

    1/30

    Subtraction (lvk) 1

    Negative Numbers and Subtraction

    The adders we designed can add only non-negative numbers If we can reresent negative numbers! then subtraction is "#ust$

    the ability to add two numbers (one of which may be negative)% &e'll look at three different ways of reresenting signed numbers% ow can we decide reresentation is better

    The best one should result in the simlest and fastest oerations% This is #ust like choosing a data structure in rogramming%

    &e're mostly concerned with two articular oerations* +egating a signed number! or converting , into -,% dding two signed numbers! or comuting , . y% So! we will comare the reresentation on how fast (and how easily)

    these oerations can be done on them

  • 7/21/2019 10 Subtraction

    2/30

    Subtraction (lvk) /

    Signed magnitude representation

    umans use a signed-magnitudesystem* we add .or -in front of amagnitude to indicate the sign%

    &e could do this in binary as well! by adding an e,tra sign bitto thefront of our numbers% 0y convention*

    sign bit reresents a ositive number% 1sign bit reresents a negative number%

    2,amles*

    111/ 3 141 (a 5-bit unsigned number)

    111 3 .141 (a ositive number in 6-bit signed magnitude)

    1 111 3 -141 (a negative number in 6-bit signed magnitude)

    1/3 51 (a 5-bit unsigned number)1 3 .51 (a ositive number in 6-bit signed magnitude)

    1 1 3 -51 (a negative number in 6-bit signed magnitude)

  • 7/21/2019 10 Subtraction

    3/30

    Subtraction (lvk) 4

    Signed magnitude operations

    +egating a signed-magnitude number is trivial* #ust change the sign bitfrom to 1! or vice versa%

    dding numbers is difficult! though% Signed magnitude is basically whateole use! so think about the grade-school aroach to addition% It'sbased on comaring the signs of the augend and addend*

    If they have the same sign! add the magnitudes and kee that sign% If they have different signs! then subtract the smaller magnitude

    from the larger one% The sign of the number with the largermagnitude is the sign of the result%

    This method of subtraction would lead to a rather comle, circuit%

    . 4 7 8. - 9 5 7 -/ 9 :

    6 14 17

    9 5 7- 4 7 8/ 9 :

    because

  • 7/21/2019 10 Subtraction

    4/30

    Subtraction (lvk) 5

    Ones complement representation

    different aroach! one's comlement! negates numbers bycomlementing each bit of the number%

    &e kee the sign bits* for ositive numbers! and 1 for negative% Thesign bit is comlemented along with the rest of the bits%

    2,amles*

    111/ 3 141 (a 5-bit unsigned number)

    111 3 .141 (a ositive number in 6-bit one's comlement)1 1 3 -141 (a negative number in 6-bit one's comlement)

    1/3 51 (a 5-bit unsigned number)

    1 3 .51 (a ositive number in 6-bit one's comlement)

    1 111 3 -51 (a negative number in 6-bit one's comlement)

  • 7/21/2019 10 Subtraction

    5/30

    Subtraction (lvk) 6

    Why is it called ones complement?

    ;omlementing a single bit is e

  • 7/21/2019 10 Subtraction

    6/30

    Subtraction (lvk) 9

    Ones complement addition

    To add one's comlement numbers* =irst do unsigned addition on the numbers! includingthe sign bits%

    Then take the carry out and add it to the sum% Two e,amles*

    This is simler and more uniform than signed magnitude addition%

    111 (.7). 111 . (-5)

    1 1

    1. 1

    11 (.4)

    11 (.4). 1 . (./)

    11

    11.

    11 (.6)

  • 7/21/2019 10 Subtraction

    7/30

    Subtraction (lvk) 7

    Twos complement

    >ur final idea is two's comlement% To negate a number! comlementeach bit (#ust as for ones' comlement) and then add 1%

    2,amles*

    111/ 3 141 (a 5-bit unsigned number)

    111 3 .141 (a ositive number in 6-bit two's comlement)

    1 1 3 -141 (a negative number in 6-bit ones'comlement)

    1 11 3 -141 (a negative number in 6-bit two's comlement)

    1/3 51 (a 5-bit unsigned number)

    1 3 .51 (a ositive number in 6-bit two's comlement)

    1 111 3 -51

    (a negative number in 6-bit ones'comlement)

    1 11 3 -51 (a negative number in 6-bit two's comlement)

  • 7/21/2019 10 Subtraction

    8/30

    Subtraction (lvk) :

    Two other eften! eole talk about "taking the two's comlement$ of a number% This is aconfusing hrase! but it usually means to negate some number that's alreadyintwo's comlement format%

    More about twos complement

    1 - 1 1 1 (.141)

    1 1 1 (-141)

    1 - 1 (.51)

    1 1 1 (-51)

  • 7/21/2019 10 Subtraction

    9/30

    Subtraction (lvk) 8

    Twos complement addition

    +egating a two's comlement number takes a bit of work! but additionis much easier than with the other two systems%

    To find . 0! you #ust have to* @o unsigned addition on and 0! including their sign bits% Ignore any carry out%

    =or e,amle! to find 111 . 11! or (.7) . (-5)* =irst add 111 . 11 as unsigned numbers*

    @iscard the carry out (1)% The answer is 11 (.4)%

    1 1 1. 1 1

    1 1 1

  • 7/21/2019 10 Subtraction

    10/30

    Subtraction (lvk) 1

    nother twos complement e!ample

    To further convince you that this works! let's try adding two negativenumbersA111 . 111! or (-4) . (-/) in decimal%

    dding the numbers gives 1111*

    @roing the carry out (1) leaves us with the answer! 111 (-6)%

    1 1 1. 1 1 1

    1 1 1 1

  • 7/21/2019 10 Subtraction

    11/30

    Subtraction (lvk) 11

    Why does this wor"?

    =or n-bit numbers! the negation of 0 in two's comlement is /n- 0 (thisis one of the alternative ways of negating a two's-comlement number)%

    - 0 3 . (-0)3 . (/n- 0)3 ( - 0) . /n

    If 0! then ( - 0) is a ositive number! and /nreresents a carry

    out of 1% @iscarding this carry out is e

  • 7/21/2019 10 Subtraction

    12/30

    Subtraction (lvk) 1/

    #omparing the signed number systems

    ere are all the 5-bit numbersin the different systems%

    Bositive numbers are the same inall three reresentations%

    Signed magnitude and one'scomlement have twoways ofreresenting % This makesthings more comlicated%

    Two's comlement hasasymmetric rangesC there is onemore negative number thanositive number% ere! you canreresent -:but not .:%

    owever! two's comlement isreferred because it has onlyone ! and its addition algorithmis the simlest%

    @ecimal S%D% 1's com% /'s com%7 0111 0111 0111

    6 0110 0110 01105 0101 0101 0101

    4 0100 0100 0100

    3 0011 0011 0011

    2 0010 0010 0010

    1 0001 0001 0001

    0 0000 0000 0000-0 1000 1111

    -1 1001 1110 1111

    -2 1010 1101 1110

    -3 1011 1100 1101

    -4 1100 1011 1100

    -5 1101 1010 1011

    -6 1110 1001 1010

    -7 1111 1000 1001

    -8 1000

  • 7/21/2019 10 Subtraction

    13/30

    Subtraction (lvk) 14

    ow many negative and ositive numbers can be reresented in each ofthe different systems on the revious age

    In general! with n-bit numbers including the sign! the ranges are*

    Ensigned

    Signed

    Dagnitude

    >ne's

    comlement

    Two's

    comlement

    Smallest -(/n-1

    -1) -(/n-1

    -1) -/n-1

    Fargest /n-1 .(/n-1-1) .(/n-1-1) .(/n-1-1)

    $anges o% the signed number systems

    Ensigned

    Signed

    Dagnitude

    >ne's

    comlement

    Two's

    comlement

    Smallest () 1111 (-7) 1 (-7) 1 (-:)

    Fargest 1111 (16) 111 (.7) 111 (.7) 111 (.7)

  • 7/21/2019 10 Subtraction

    14/30

    Subtraction (lvk) 15

    #onverting signed numbers to decimal

    ;onvert 1111 to decimal! assuming this is a number in*

    (a) signed magnitude format

    (b) ones' comlement

    (c) two's comlement

  • 7/21/2019 10 Subtraction

    15/30

    Subtraction (lvk) 16

    &!ample solution

    ;onvert 1111 to decimal! assuming this is a number in*

    Since the sign bit is 1! this is a negative number% The easiest way tofind the magnitude is to convert it to a ositive number%

    (a) signed magnitude format

    +egating the original number! 1111! gives 111! which is ./1 indecimal% So 1111 must reresent -/1%

    (b) ones' comlement

    +egating 1111 in ones' comlement yields 11 3 .11! so theoriginal number must have been -11%

    (c) two's comlement

    +egating 1111 in two's comlement gives 111 3 111! whichmeans 1111 3 -111%

    The most imortant oint here is that a binary number has differentmeanings deending on which reresentation is assumed%

  • 7/21/2019 10 Subtraction

    16/30

    's #omplement

    &hat is the negative of 111

    * 111

    0* 1111 ;* 111 @* 1111

    Gune 4th! /8 19 Subtraction

  • 7/21/2019 10 Subtraction

    17/30

    's #omplement

    &hat is the negative of 1

    * 111111 0* ;* 11111

    @* 1

    Gune 4th! /8 17 Subtraction

  • 7/21/2019 10 Subtraction

    18/30

    Subtraction (lvk) 1:

    (nsigned numbers over%low

    ;arry-out can be used to detect overflow The largest number that we can reresent with 5-bits using unsigned

    numbers is 16 Suose that we are adding 5-bit numbers* 8 (11) and 1 (11)%

    The value 18 cannot be reresented with 5-bits &hen oerating with unsigned numbers! a carry-out of 1 can be used to

    indicate overflow

    1 1 (8). 1 1 (1)

    1 1 1 (18)

  • 7/21/2019 10 Subtraction

    19/30

    Subtraction (lvk) 18

    Signed over%low

    &ith two's comlement and a 5-bit adder! for e,amle! the largestreresentable decimal number is .7! and the smallest is -:%

    &hat if you try to comute 5 . 6! or (-5) . (-6)

    &e cannot #ust include the carry out to roduce a five-digit result! asfor unsigned addition% If we did! (-5) . (-6) would result in ./4H lso! unlike the case with unsigned numbers! the carry out cannotbe

    used to detect overflow%

    In the e,amle on the left! the carry out is but there isoverflow%

    ;onversely! there are situations where the carry out is 1 but thereis no overflow%

    1 (.5). 1 1 (.6)

    1 1 (-7)

    1 1 (-5). 1 1 1 (-6)

    1 1 1 1 (.7)

  • 7/21/2019 10 Subtraction

    20/30

    Subtraction (lvk) /

    )etecting signed over%low

    The easiest way to detect signed overflow is to look at all the sign bits%

    >verflow occurs only in the two situations above* If you add two ositivenumbers and get a negativeresult% If you add two negativenumbers and get a ositiveresult%(The only e,cetion is when you add two negativenumbers in Signed

    Dagnitude% In Signed Dagnitude! you #ust add the magnitudes and

    kee the same sign% In this case there is overflow if there is acarry% Try to add -5 . -6 using Signed Dagnitude)%

    >verflow cannot occur if you add a ositive number to a negativenumber% @o you see why

    1 (.5). 1 1 (.6)

    1 1 (-7)

    1 1 (-5). 1 1 1 (-6)

    1 1 1 1 (.7)

  • 7/21/2019 10 Subtraction

    21/30

    Subtraction (lvk) /1

    Sign e!tension

    In everyday life! decimal numbers are assumed to have an infinitenumber of s in front of them% This hels in "lining u$ numbers%

    To subtract /41 and 4! for instance! you can imagine*/41

    - 4

    //:

    ?ou need to be careful in e,tending signed binary numbers! because theleftmost bit is the signand not art of the magnitude% If you #ust add s in front! you might accidentally change a negative

    number into a ositive oneH

    =or e,amle! going from 5-bit to :-bit numbers*

    11 (.6) should become 11 (.6)%

    0ut 11 (-5) should become 1111 11 (-5)% The roer way to e,tend a signed binary number is to relicate the

    sign bit! so the sign is reserved%

  • 7/21/2019 10 Subtraction

    22/30

    Subtraction (lvk) //

    Signed Numbers are Sign*&!tended to +n%inity,

    Demorie this idea% It will save you every time when you are doing binary arithmetic by

    hand% 111 in twos comlement is actually J111 111 in twos comlement is actually J1111111

    111 . 11 3 1111% This is overflow% 0ut can you see it Fook again*

    J1111111 . J111111 3 J1111111 The sign of the result (1) is different than bit 4 ()% &e cannot reresent the result in 5 bits

    The sign of the result is e,tended to infinity ;heck this every time in your answers and you will always see the

    recision of the result (number of significant bits) very clearly% Sign-e,tend inuts to infinity @o the math on an infinite number of bits (concetually) Truncate the result to the desired outut tye

  • 7/21/2019 10 Subtraction

    23/30

    )oes over%low occur in my program?

    ublic class test K

    ublic static void main(StringMN args) K

    int i 3 Cwhile (i O3 ) K

    i..C

    P

    System%out%rintln(Qi 3 Q . i)C

    i--C

    System%out%rintln(Qi 3 Q . i)C

    i..C

    System%out%rintln(Qi 3 Q . i)C

    PP

    >utut*

    i 3 -/1575:495: /41

    i 3 /1575:4957 /41-1

    i 3 -/1575:495:

    Subtraction (lvk) /4

  • 7/21/2019 10 Subtraction

    24/30

    Subtraction (lvk) /5

    Ma"ing a subtraction circuit

    &e could build a subtraction circuit directly! similar to the way wemade unsigned adders%

    owever! by using two's comlement we can convert any subtractionroblem into an addition roblem% lgebraically!

    - 0 3 . (-0)

    So to subtract0 from ! we can instead addthe negation of 0 to %

    This way we can re-use the unsigned adder hardware from last time%

  • 7/21/2019 10 Subtraction

    25/30

    Subtraction (lvk) /6

    twos complement subtraction circuit

    To find - 0 with an adder! we'll need to* ;omlement each bit of 0%

    Set the adder's carry in to 1% The net result is . 0' . 1! where 0' . 1 is the two's comlement negation of 0%

    Remember that 4! 04 and S4 here are actually sign bits%

  • 7/21/2019 10 Subtraction

    26/30

    Subtraction (lvk) /9

    Small di%%erences

    The only differences between the adder and subtractor circuits are* The subtractor has to negate 04 0/ 01 0%

    The subtractor sets the initial carry in to 1! instead of %

    It's not too hard to make one circuit that does bothaddition and subtraction%

  • 7/21/2019 10 Subtraction

    27/30

    Subtraction (lvk) /7

    n adder*subtractor circuit

    >R gates let us selectively comlement the 0 inut%

    3 1 3 '

    &hen Sub 3 ! the >R gates outut 04 0/ 01 0 and the carry in is %The adder outut will be . 0 . ! or #ust . 0%

    &hen Sub 3 1! the >R gates outut 04' 0/' 01' 0' and the carry in is1% Thus! the adder outut will be a two's comlement subtraction! - 0%

  • 7/21/2019 10 Subtraction

    28/30

    Subtraction (lvk) /:

    Subtraction summary

    good reresentation for negative numbers makes subtractionhardware much easier to design%

    Two's comlement is used most often (although signed magnitudeshows u sometimes! such as in floating-oint systems! which we'lldiscuss later)%

    Esing two's comlement! we can build a subtractor with minorchanges to the adder from last time%

    &e can also make a single circuit which can both add and subtract% >verflow is still a roblem! but signed overflow is very different fromthe unsigned overflow we mentioned last time%

    Sign e,tension is needed to roerly "lengthen$ negative numbers%

    fter the midterm we'll use most of the ideas we've seen so far to buildan FE an imortant art of a rocessor%

  • 7/21/2019 10 Subtraction

    29/30

    ;onvert (-//)1to /'s comlement 9-bitbinary*

    * 111

    0* 111 ;* 111 @* 1111

    Gune 4th! /8 /8 Subtraction

  • 7/21/2019 10 Subtraction

    30/30

    In : bit twos comlement binary*1111113

    * 11111

    0* 1111 ;* 1111 @* 1111

    Gune 4th /8 4 Subtraction