number representation and binary arithmetic

37
08 ARTH Page 1 ECEn/CS 224 Number Representation and Binary Arithmetic

Upload: tybalt

Post on 17-Jan-2016

61 views

Category:

Documents


0 download

DESCRIPTION

Number Representation and Binary Arithmetic. Unsigned Addition. Binary Addition. 0 + 0 = 0 0 + 1 = 1 1 + 0 = 1 1 + 1 = 0 and carry 1 to the next column. Examples:. Carries. 1 1 1 0101 (5 10 ) + 0011 (3 10 ) 1000 (8 10 ). 0101 (5 10 ) + 0010 (2 10 ) - PowerPoint PPT Presentation

TRANSCRIPT

  • Number RepresentationandBinary Arithmetic

    ECEn 224 Winter 2002

  • Unsigned Addition

    ECEn 224 Winter 2002

  • Binary Addition0 + 0 = 00 + 1 = 11 + 0 = 11 + 1 = 0 and carry 1 to the next columnExamples: 0101 (510)+ 0010 (210) 0111 (710)Carries 1 1 1 0101 (510)+ 0011 (310) 1000 (810)

    ECEn 224 Winter 2002

  • Binary Addition w/OverflowAdd 4510 and 4410 in binaryCarries 1 1 1 101101 (4510)+ 101100 (4410) 1011001 (8910)If the operands are unsigned, you can use the final carry-out as the MSB of the result.

    Adding 2 k bit numbers k+1 bit result

    ECEn 224 Winter 2002

  • More Binary Addition w/Overflow 1 1 1 1 1111 (1510)+ 0001 (110) 0000 (010)If you dont want a 5-bit result, just keep the lower 4 bits.

    This is insufficient to hold the result (16).

    It rolls over back to 0.

    ECEn 224 Winter 2002

  • Signed Numbers

    ECEn 224 Winter 2002

  • Negative Binary NumbersSeveral ways of representing negative numbersMost obvious is to add a sign (+ or -) to the binary integer

    ECEn 224 Winter 2002

  • Sign-Magnitude0 is +1 is -Easy to interpret number value

    ECEn 224 Winter 2002

    Sheet1

    [( A B' ) + C ) D' + E ]= [ ( A B' + C ) D' ] E'(2-1)

    = [ ( A B' + C )' + D ] E'(2-2)

    = [ ( A B' ) C )' + D ] E'(2-3)

    = [ ( A' + B' ) C' + D ] E'(2-4)

    F = A ( A' + B )

    Lect 1

    valuesymbolvaluesymbol

    0088

    1199

    2210A

    3311B

    4412C

    5513D

    6614E

    7715F

    20480012048

    10240000

    5120000

    256001256

    1281128011282430

    641640164

    320013200

    161160000

    8181800

    4001400

    2120000

    1111100

    2194524960

    8-4-2-1

    DecimalCodeDecimal2-out-of-5

    Digit(BCD)DigitCodeNumberSignMagnitudeFull Number

    00 0 0 000 0 0 1 1+10000100001110110

    10 0 0 110 0 1 0 1-11000110001x101101

    20 0 1 020 0 1 1 0+50010100101_________________

    30 0 1 130 1 0 0 1-51010110101

    40 1 0 040 1 0 1 0+00000000000110111

    50 1 0 150 1 1 0 0-01000010000110110

    60 1 1 061 0 0 0 160 1 0 1\000000

    70 1 1 171 0 0 1 070 1 0 0110110

    81 0 0 081 0 1 0 081 1 0 0110110

    91 0 0 191 1 0 0 091 1 0 1000000

    101 1 1 1110110

    111 1 1 0__________________________________

    121 0 1 0100101111110

    131 0 1 1

    141 0 0 1

    151 0 0 0

    161 1 0 0 0

    Sheet2

    564.4810

    Sheet3

  • Sign-Magnitude Examples 0 0 0 0 0011 (+310)+ 0 0100 (+410) 0 0111 (+710)Signs are the same Just add the magnitudes 0 0 0 1 0011 (-310)+ 1 0100 (-410) 1 0111 (-710)

    ECEn 224 Winter 2002

  • Another Sign-Magnitude Example 0 0101 (+510)+ 1 0011 (-310) Signs are different -determine which is larger magnitudePut larger magnitude number on top

    Subtract

    Result has sign of larger magnitude number 0 0101 (+510)- 1 0011 (-310) 0 0010 (+210)

    ECEn 224 Winter 2002

  • Yet Another Sign-Magnitude Example 0 0010 (+210)+ 1 0101 (-510) Signs are different -determine which is larger magnitudePut larger magnitude number on top

    Subtract

    Result has sign of larger magnitude number 1 0101 (-510)- 0 0010 (+210) 1 0011 (-310)

    ECEn 224 Winter 2002

  • Sign-MagnitudeAddition requires two separate operationsadditionsubtractionSeveral decisions:Signs same or different?Which operand is larger?What is sign of final result?Two zeroes (+0, -0)

    ECEn 224 Winter 2002

  • Sign-MagnitudeAdvantagesEasy to understand

    DisadvantagesTwo different 0sHard to implement in logic

    ECEn 224 Winter 2002

  • Ones ComplementPositive numbers are the same as sign-magnitude-N is represented as the complement of N+ -N = N

    ECEn 224 Winter 2002

    Sheet1

    [( A B' ) + C ) D' + E ]= [ ( A B' + C ) D' ] E'(2-1)

    = [ ( A B' + C )' + D ] E'(2-2)

    = [ ( A B' ) C )' + D ] E'(2-3)

    = [ ( A' + B' ) C' + D ] E'(2-4)

    F = A ( A' + B )

    Lect 1

    valuesymbolvaluesymbol

    0088

    1199

    2210A

    3311B

    4412C

    5513D

    6614E

    7715F

    20480012048

    10240000

    5120000

    256001256

    1281128011282430

    641640164

    320013200

    161160000

    8181800

    4001400

    2120000

    1111100

    2194524960

    8-4-2-1

    DecimalCodeDecimal2-out-of-5

    Digit(BCD)DigitCodeNumber1's ComplementMagnitudeFull Number

    00 0 0 000 0 0 1 1+10001000100001110110

    10 0 0 110 0 1 0 1-11110000110001x101101

    20 0 1 020 0 1 1 0+50101010100101_________________

    30 0 1 130 1 0 0 1-51010010110101

    40 1 0 040 1 0 1 0+00000000000000110111

    50 1 0 150 1 1 0 0-01111000010000110110

    60 1 1 061 0 0 0 160 1 0 1\000000

    70 1 1 171 0 0 1 070 1 0 0110110

    81 0 0 081 0 1 0 081 1 0 0110110

    91 0 0 191 1 0 0 091 1 0 1000000

    101 1 1 1110110

    111 1 1 0__________________________________

    121 0 1 0100101111110

    131 0 1 1

    141 0 0 1

    151 0 0 0

    161 1 0 0 0

    Sheet2

    564.4810

    Sheet3

  • Ones Complement Examples 000101 (510)+ 010100 (2010) 011001 (2510) 1 1 0 0 0101 (510)+ 1100 (-310) 0001 0010 (+210) +If there is a carry out on the left, it must be wrapped around and added back in on the right

    ECEn 224 Winter 2002

  • Ones ComplementAddition complicated by end around carryNo decisions (like SM)Still two zeroes (+0, -0)

    ECEn 224 Winter 2002

  • Ones ComplementAdvantagesEasy to generate NOnly one addition process DisadvantagesEnd around carryTwo different 0s

    ECEn 224 Winter 2002

  • Twos ComplementTreat positional digits differently01112C = -0 x 23 + 1 x 22 + 1 x 21 + 1 x 20 = 71011112C = -1 x 23 + 1 x 22 + 1 x 21 + 1 x 20 = -110Most significant bit (MSB) given negative weightOther bits same as in unsigned

    ECEn 224 Winter 2002

  • Twos Complement

    ECEn 224 Winter 2002

    Sheet1

    [( A B' ) + C ) D' + E ]= [ ( A B' + C ) D' ] E'(2-1)

    = [ ( A B' + C )' + D ] E'(2-2)

    = [ ( A B' ) C )' + D ] E'(2-3)

    = [ ( A' + B' ) C' + D ] E'(2-4)

    F = A ( A' + B )

    Lect 1

    valuesymbolvaluesymbol

    0088

    1199

    2210A

    3311B

    4412C

    5513D

    6614E

    7715F

    20480012048

    10240000

    5120000

    256001256

    1281128011282430

    641640164

    320013200

    161160000

    8181800

    4001400

    2120000

    1111100

    2194524960

    8-4-2-1

    DecimalCodeDecimal2-out-of-5

    Digit(BCD)DigitCodeNumber2's ComplementMagnitudeFull Number

    00 0 0 000 0 0 1 1+10001000100001110110

    10 0 0 110 0 1 0 1-11111000110001x101101

    20 0 1 020 0 1 1 0+50101010100101_________________

    30 0 1 130 1 0 0 1-51011010110101

    40 1 0 040 1 0 1 0+00000000000000110111

    50 1 0 150 1 1 0 0-0none000010000110110

    60 1 1 061 0 0 0 160 1 0 1\000000

    70 1 1 171 0 0 1 070 1 0 0110110

    81 0 0 081 0 1 0 081 1 0 0110110

    91 0 0 191 1 0 0 091 1 0 1000000

    101 1 1 1110110

    111 1 1 0__________________________________

    121 0 1 0100101111110

    131 0 1 1

    141 0 0 1

    151 0 0 0

    161 1 0 0 0

    Sheet2

    564.4810

    Sheet3

  • Sign-Extension in 2s ComplementTo make a k-bit number widerreplicate sign bit1102C = -1 x 22 + 1 x 21 = -21011102C = -1 x 23 + 1 x 22 + 1 x 21 = -210111102C = -1 x 24 + 1 x 23 + 1 x 22 + 1 x 21 = -210

    ECEn 224 Winter 2002

  • More Sign-Extension0102C = 1 x 21 = 21000102C = 1 x 21 = 210

    000102C = 1 x 21 = 210

    Works for both positive and negative numbers

    ECEn 224 Winter 2002

  • Negating a 2s Complement NumberInvert all the bitsAdd 1+210 = 00102C 1101 + 1 = 11102C = -210invert-210 = 11102C 0001 + 1 = 00102C = +210invert

    ECEn 224 Winter 2002

  • Twos Complement Addition 0 0 1 0101 (+510)+ 0001 (+110) 0110 (+610) 1 1 1 1011 (-510)+ 1111 (-110) 1010 (-610) 1 1 1 0110 (+610)+ 1111 (-110) 0101 (+510)Operation is same as for unsignedSame rules, same procedure

    Interpretation of operands and results are different

    ECEn 224 Winter 2002

  • Twos ComplementAddition always the sameOnly 1 zero

    Representation of choice

    ECEn 224 Winter 2002

  • Twos Complement OverflowAll representations can overflowFocus on 2s complement here 1 0 0 0 1011 (-510)+ 1100 (-410) 0111 (+710) ??The correct answer (-9) cannot be represented by 4 bitsThe correct answer is 10111

    ECEn 224 Winter 2002

  • OverflowCan you use leftmost carry-out as new MSB?

    The answer is no, in general 1 0 0 0 1011 (-510)+ 1100 (-410) 10111 (-910)Works here 0 0 0 0 1000 (-810)+ 0111 (+710) 01111 (+1510) ??Does NOT work here

    ECEn 224 Winter 2002

  • Handling OverflowSign-extend the operands Do the addition 0 0 0 0 11000 (-810)+00111 (+710) 11111 (-110)

    ECEn 224 Winter 2002

  • When Can Overflow Occur?Adding two positive numbers - yes

    Adding two negative numbers yes

    Adding a positive to a negative no

    Adding a negative to a positive - no

    ECEn 224 Winter 2002

  • General Handling of OverflowAdding two k bit numbers gives k+1 bit result Two options include:

    Sign extend and keep entire k+1 bit resultThrow away new bit generated (keep only k bits)Detect overflow and signal an errorIgnore the overflow (this is what most computers do)

    The choice is up to the designer

    ECEn 224 Winter 2002

  • Detecting OverflowOverflow occurs if:Adding two positive numbers gives what looks like a negative resultAdding two negative numbers gives what looks like a positive result

    Overflow will never occur when adding positive to negative

    There are other ways of detecting overflow vs. what is suggested here

    ECEn 224 Winter 2002

  • Arithmetic Circuits

    ECEn 224 Winter 2002

  • Binary AdderAn example of an iterative network

    FullAdderA0S0C0S1S2C3...B0 C1A1B1FullAdder C2FullAdderA2B2Sn-1Cn Cn-1FullAdderAn-1Bn-1 This type of adder is also called a ripple-carry adder because the carry ripples through from cell to cell

    ECEn 224 Winter 2002

  • Full Adder DerivationSiCi+1AiBiCiAiBiBiCiAiCi

    ECEn 224 Winter 2002

    Sheet1

    [( A B' ) + C ) D' + E ]= [ ( A B' + C ) D' ] E'(2-1)

    = [ ( A B' + C )' + D ] E'(2-2)

    = [ ( A B' ) C )' + D ] E'(2-3)

    = [ ( A' + B' ) C' + D ] E'(2-4)

    F = A ( A' + B )

    Lect 2

    valuesymbolvaluesymbol

    0088

    1199

    2210A

    3311B

    4412C

    5513D

    6614E

    7715F

    20480012048

    10240000

    5120000

    256001256

    1281128011282430

    641640164

    320013200

    161160000

    8181800

    4001400

    2120000

    1111100

    2194524960

    8-4-2-1

    DecimalCodeDecimal2-out-of-5GrayCharacterA6

    Digit(BCD)DigitCodeNumberCode

    00 0 0 000 0 0 1 100 0 0 0110110

    10 0 0 110 0 1 0 110 0 0 1x101101c1100011

    20 0 1 020 0 1 1 020 0 1 1_________________d1100100

    30 0 1 130 1 0 0 130 0 1 0e1100101

    40 1 0 040 1 0 1 040 1 1 0110111f1100110

    50 1 0 150 1 1 0 050 1 1 1110110g1100111

    60 1 1 061 0 0 0 160 1 0 1000000h1101000

    70 1 1 171 0 0 1 070 1 0 0110110I1101001

    81 0 0 081 0 1 0 081 1 0 0110110j1101010

    91 0 0 191 1 0 0 091 1 0 1000000k1101011

    101 1 1 1110110l1101100

    111 1 1 0__________________________________m1101101

    121 0 1 0100101111110n1101110

    131 0 1 1o1101111

    141 0 0 1p1111000

    151 0 0 0q1110001

    161 1 0 0 0

    Lect 3

    AiBiCiCi+1Si

    00000A'B'

    00101A'B

    01001

    01110AB

    10001

    10110

    11010

    11111

  • Binary SubtracterFullSub.X1D1B1D2...Y1B2X2Y2FullSub. B3FullSub.Dn-1Bn Bn-1FullSub.Xn-1Yn-1D(ifference) = X - YB is the borrow signalEquations generated similarly to full adderB0D0X0Y0

    ECEn 224 Winter 2002

  • Another Way to Make a Subtracter(2s complement)FullAdderFullAdderFullAdderFullAdderC0 = 1A0B0B1A1B2A2B3A3B0B1B2B3S3C4S2C3S1C2S0S = A + (-B)-B is generated by inverting and adding 1adding 1C1inverting

    ECEn 224 Winter 2002

  • Binary Arithmetic Comparison

    ECEn 224 Winter 2002

    Sign Magnitude

    One's Complement

    Two's Complement

    Negative Number

    Easiest to Understand Simple to Compute

    Easy to Compute

    Hardest to Compute

    Zeroes

    2 Zeroes

    2 Zeroes

    1 Zero

    Largest Number

    Same number of

    + and - Numbers

    Same number of

    + and - Numbers

    One Extra Negative Number

    Logic Required

    Requires Adder and Subtracter

    Only Adder Required

    Only Adder Required

    Extra Logic to Identify Larger Operand, Compute Sign, etc.

    Carry Wraps Around

    -

    Overflow Detection

    Overflow: Carry from High Order Adder Bits

    Overflow: Sign of Both Operands is the Same and Sign of Sum is Different

    Overflow: Sign of Both Operands is the Same and Sign of Sum is Different

  • SummaryUnderstand the three main representation of binary integersSign-MagnitudeOnes ComplementTwos ComplementDesign binary adders and subtracters for each representation

    ECEn 224 Winter 2002