UCLA CS 33 - Reinman - Spring 2014

download UCLA CS 33 - Reinman - Spring 2014

of 38

description

UCLA CS 33 Lecture 2 Notes Reinman Spring 2014

Transcript of UCLA CS 33 - Reinman - Spring 2014

  • 5/21/2018 UCLA CS 33 - Reinman - Spring 2014

    1/38

    SomenotesadoptedfromBryantandOHallaron

    Integers

    Chapter2ofB&O

  • 5/21/2018 UCLA CS 33 - Reinman - Spring 2014

    2/38

    Encoding Integers

    short int x = 15213;

    short int y = -15213;

    Cshor t 2byteslong

    SignBit

    For2scomplement,mostsignificantbitindicatessign

    0fornonnegative

    1fornegative

    B2T(X) xw1 2w1 xi2

    i

    i0

    w2

    B2U(X) xi2i

    i0

    w1

    Unsigned Twos Complement

    Sign

    Bit

    Decimal Hex Binaryx 15213 3B 6D 00111011 01101101y -15213 C4 93 11000100 10010011

  • 5/21/2018 UCLA CS 33 - Reinman - Spring 2014

    3/38

  • 5/21/2018 UCLA CS 33 - Reinman - Spring 2014

    4/38

    Numeric Ranges UnsignedValues

    UMin = 0

    0000 UMax = 2w 1

    1111

    TwosComplementValues

    TMin = 2w1

    1000 TMax = 2w1 1

    0111

    OtherValues Minus1

    1111

    Decimal Hex BinaryUMax 65535 FF FF 11111111 11111111TMax 32767 7F FF 01111111 11111111TMi n -32768 80 00 10000000 00000000- 1 -1 FF FF 11111111 111111110 0 00 00 00000000 00000000

    Values for W = 16

  • 5/21/2018 UCLA CS 33 - Reinman - Spring 2014

    5/38

    Values for Different Word Sizes

    Observations

    |TMin| = TMax+1Asymmetricrange

    UMax = 2*TMax+1

    CProgramming

    #i ncl ude

    Declaresconstants,e.g.,

    ULONG_MAX

    LONG_MAX

    LONG_MI N

    Valuesplatformspecific

    W

    8 16 32 64

    UMax 255 65,535 4,294,967,295 18,446,744,073,709,551,615

    TMax 127 32,767 2,147,483,647 9,223,372,036,854,775,807

    TMin -128 -32,768 -2,147,483,648 -9,223,372,036,854,775,808

  • 5/21/2018 UCLA CS 33 - Reinman - Spring 2014

    6/38

    short int x = 15213;

    unsigned short int ux = (unsigned short) x;

    short int y = -15213;

    unsigned short int uy = (unsigned short) y;

    Casting Signed to Unsigned CAllowsConversionsfromSignedtoUnsigned

    ResultingValue

    Nochangeinbitrepresentation

    Nonnegativevaluesunchanged

    ux=15213

    Negativevalueschangeinto(large)positivevalues

    uy=50323

  • 5/21/2018 UCLA CS 33 - Reinman - Spring 2014

    7/38

    T2U

    T2B B2U

    Twos Complement Unsigned

    Maintain Same Bit Pattern

    x ux

    X

    ++ + +++

    - + + +++

    ux

    x-

    w1 0

    +2w1 2w1 = 2*2w1 = 2w

    ux

    x x 0

    x 2w x 0

    Relation between Signed & Unsigned

  • 5/21/2018 UCLA CS 33 - Reinman - Spring 2014

    8/38

    0

    TMax

    TMin

    1

    2

    0

    UMax

    UMax 1

    TMax

    TMax + 1

    2s Comp.

    Range

    Unsigned

    Range

    Illustration 2sComp.Unsigned

    OrderingInversion

    NegativeBigPositive

  • 5/21/2018 UCLA CS 33 - Reinman - Spring 2014

    9/38

    Relation Between Signed & Unsigned

    uy = y+2*32768 = y+65536

    Weight -15213 50323

    1 1 1 1 12 1 2 1 24 0 0 0 0

    8 0 0 0 016 1 16 1 1632 0 0 0 064 0 0 0 0

    128 1 128 1 128256 0 0 0 0512 0 0 0 0

    1024 1 1024 1 10242048 0 0 0 04096 0 0 0 08192 0 0 0 0

    16384 1 16384 1 1638432768 1 -32768 1 32768

    Sum -15213 50323

  • 5/21/2018 UCLA CS 33 - Reinman - Spring 2014

    10/38

    Signed vs. Unsigned in C Constants

    Bydefaultareconsideredtobesignedintegers

    UnsignedifhaveUassuffix

    0U, 4294967259U

    Casting

    Explicitcastingbetweensigned&unsigned

    i nt t x, t y;

    unsi gned ux, uy;

    t x = ( i nt ) ux;

    uy = ( unsi gned) t y;

    Implicitcastingalsooccursviaassignmentsandprocedurecalls

    t x = ux;

    uy = t y;

  • 5/21/2018 UCLA CS 33 - Reinman - Spring 2014

    11/38

    0 0U == unsigned

    - 1 0 < signed

    - 1 0U > unsigned

    2147483647 - 2147483648 > signed

    2147483647U - 2147483648 < unsigned- 1 - 2 > signed

    ( unsi gned) - 1 - 2 > unsigned

    2147483647 2147483648U < unsigned

    2147483647 ( i nt ) 2147483648U > signed

    Casting Surprises ExpressionEvaluation

    Ifmixunsignedandsignedinsingleexpression,signedvaluesimplicitlycast

    tounsigned

    Includingcomparisonoperations,==,= ExamplesforW=32

    Constant1 Constant2 Relation Evaluation

    0 0U

    - 1 0

    - 1 0U

    2147483647 - 2147483648

    2147483647U - 2147483648- 1 - 2

    ( unsi gned) - 1 - 2

    2147483647 2147483648U

    2147483647 ( i nt ) 2147483648U

  • 5/21/2018 UCLA CS 33 - Reinman - Spring 2014

    12/38

    Sign Extension Task:

    Givenwbitsignedintegerx

    Convertittow+kbitintegerwithsamevalue

    Rule:

    Makekcopiesofsignbit:

    X= xw1

    ,,x

    w1,x

    w1,x

    w2,,x

    0

    k copies of MSB

    X

    X

    w

    wk

  • 5/21/2018 UCLA CS 33 - Reinman - Spring 2014

    13/38

    Sign Extension Example

    Convertingfromsmallertolargerintegerdatatype

    Cautomaticallyperformssignextension

    short int x = 15213;

    int ix = (int) x;

    short int y = -15213;

    int iy = (int) y;

    Decimal Hex Binary

    x 15213 3B 6D 00111011 01101101

    i x 15213 00 00 3B 6D 00000000 00000000 00111011 01101101y -15213 C4 93 11000100 10010011i y -15213 FF FF C4 93 11111111 11111111 11000100 10010011

  • 5/21/2018 UCLA CS 33 - Reinman - Spring 2014

    14/38

    Justification For Sign Extension ProveCorrectnessbyInductiononk

    InductionStep:extendingbysinglebitmaintainsvalue

    Keyobservation: 2w1 =2w+2w1

    Lookatweightofupperbits:

    X 2w1 xw1

    X 2

    w

    xw1+

    2

    w1

    xw1 = 2

    w1

    xw1

    - X

    X - +

    w+1

    w

  • 5/21/2018 UCLA CS 33 - Reinman - Spring 2014

    15/38

    Beware When Using Unsigned DontUseJustBecauseNumberNonzero

    Easytomakemistakes

    f or ( i = cnt - 2; i >= 0; i - - )a[ i ] += a[ i +1] ;

  • 5/21/2018 UCLA CS 33 - Reinman - Spring 2014

    16/38

    Negating with Complement & Increment Claim:FollowingHoldsfor2sComplement

    ~x + 1 == - x

    Complement

    Observation:~x + x == 1111112 == - 1

    Increment

    ~x + x + ( - x + 1) ==- 1 + ( - x + 1) ~x + 1 == - x

    1 0 0 1 0 11 1x

    0 1 1 0 1 00 0~x+

    1 1 1 1 1 11 1-1

  • 5/21/2018 UCLA CS 33 - Reinman - Spring 2014

    17/38

    Comp. & Incr. Examples

    Decimal Hex Binary

    x 15213 3B 6D 00111011 01101101~x -15214 C4 92 11000100 10010010

    ~x+1 -15213 C4 93 11000100 10010011y -15213 C4 93 11000100 10010011

    x = 15213

    Decimal Hex Binary

    0 0 00 00 00000000 00000000~0 -1 FF FF 11111111 11111111~0+1 0 00 00 00000000 00000000

    0

  • 5/21/2018 UCLA CS 33 - Reinman - Spring 2014

    18/38

    Unsigned Addition

    StandardAdditionFunction

    Ignorescarryoutput

    ImplementsModularArithmetic

    s = UAddw(u,v) =u+v mod2w

    UAddw(u,v) u v u v 2w

    u v 2w u v 2w

    u

    v+

    u + v

    True Sum: w+1 bits

    Operands: wbits

    Discard Carry: wbits UAddw(u , v)

  • 5/21/2018 UCLA CS 33 - Reinman - Spring 2014

    19/38

    0

    2 4 68

    1012

    14

    0

    2

    4

    6

    8

    10

    12

    14

    0

    4

    8

    12

    16

    20

    24

    28

    32

    Integer Addition

    Visualizing Integer AdditionIntegerAddition

    4bitintegersu,v

    ComputetruesumAdd4(u,v)

    Valuesincrease

    linearlywithuandv

    Formsplanar

    surface

    Add4(u , v)

    u

    v

  • 5/21/2018 UCLA CS 33 - Reinman - Spring 2014

    20/38

    02

    46

    810

    1214

    0

    2

    4

    6

    8

    10

    12

    14

    0

    2

    4

    6

    8

    10

    12

    14

    16

    Visualizing Unsigned Addition WrapsAround

    Iftruesum 2w

    Atmostonce

    0

    2w

    2w+1

    UAdd4(u , v)

    u

    v

    True Sum

    Modular Sum

    Overflow

    Overflow

  • 5/21/2018 UCLA CS 33 - Reinman - Spring 2014

    21/38

    Mathematical Properties ModularAddition

    Closedunderaddition

    0 UAddw(u,

    v) 2w

    1 Commutative

    UAddw(u,v) = UAddw(v,u)

    AssociativeUAddw(t,UAddw(u,v)) = UAddw(UAddw(t,u),v)

    0isadditiveidentity

    UAddw(u,0) = u

    Everyelementhasadditiveinverse

    Let UCompw(u) = 2wuUAddw(u,UCompw(u)) = 0

  • 5/21/2018 UCLA CS 33 - Reinman - Spring 2014

    22/38

    Twos Complement Addition

    TAdd

    and

    UAdd

    have

    Identical

    Bit

    Level

    Behavior Signedvs.unsignedadditioninC:

    i nt s, t , u, v;

    s = ( i nt ) ( ( unsi gned) u + ( unsi gned) v) ;

    t = u + v

    Willgive s == t

    u

    v+

    u + v

    True Sum:w

    +1 bits

    Operands: wbits

    Discard Carry: wbits TAddw(u , v)

  • 5/21/2018 UCLA CS 33 - Reinman - Spring 2014

    23/38

    Characterizing TAdd Functionality

    Truesumrequiresw+1

    bits

    DropoffMSB

    Treatremainingbitsas

    2scomp.integer

    2w1

    2w

    0

    2w1

    2w1

    True Sum

    TAdd Result

    10000

    11000

    00000

    01000

    01111

    1000

    0000

    0111

    PosOver

    NegOver

    TAddw (u,v)

    u v 2w1 u v TMinwu v TMinw u v TMaxwu v 2w1 TMaxw u v

    (NegOver)

    (PosOver)u

    v

    < 0 > 0

    < 0

    > 0

    NegOver

    PosOver

    TAdd(u , v)

  • 5/21/2018 UCLA CS 33 - Reinman - Spring 2014

    24/38

    -8-6

    -4-2

    02

    46

    -8

    -6

    -4-2

    0

    2

    4

    6

    -8

    -6

    -4

    -2

    0

    2

    4

    6

    8

    Visualizing 2s Comp. Addition Values

    4bittwoscomp.

    Rangefrom 8to+7

    WrapsAround

    Ifsum2w1 Becomesnegative

    Atmostonce

    Ifsum

  • 5/21/2018 UCLA CS 33 - Reinman - Spring 2014

    25/38

    Detecting 2s Comp. Overflow Task

    Givens = TAddw(u,v)

    Determineifs = Addw(u,v) Example

    i nt s, u, v;

    s = u + v;

    Claim

    Overflowiffeither:

    u,v

  • 5/21/2018 UCLA CS 33 - Reinman - Spring 2014

    26/38

    Multiplication ComputingExactProductofwbitnumbersx,y

    Eithersignedorunsigned

    Ranges

    Unsigned:0x*y (2w 1)2 = 22w 2w+1 +1

    Upto2wbits

    Twoscomplementmin:x*y (2w1)*(2w11) =22w2 +2w1

    Upto2w1bits

    Twoscomplementmax:x*y (2w1) 2 = 22w2

    Upto2wbits,butonlyfor(TMinw)2

    Maintaining

    Exact

    Results Wouldneedtokeepexpandingwordsizewitheachproductcomputed

    Doneinsoftwarebyarbitraryprecisionarithmeticpackages

    (GMP,BigNum,etc.)

  • 5/21/2018 UCLA CS 33 - Reinman - Spring 2014

    27/38

    Unsigned Multiplication in C

    StandardMultiplicationFunction

    Ignoreshighorderwbits

    ImplementsModularArithmeticUMultw(u,v) = u v mod2

    w

    u

    v*

    u

    v

    True Product: 2*

    w bits

    Operands: wbits

    Discard wbits: wbits UMultw(u , v)

  • 5/21/2018 UCLA CS 33 - Reinman - Spring 2014

    28/38

    Unsigned vs. Signed Multiplication UnsignedMultiplication

    unsi gned ux = ( unsi gned) x;

    unsi gned uy = ( unsi gned) y;unsi gned up = ux * uy

    Truncatesproducttowbitnumberup =UMultw(ux,uy)

    Modulararithmetic:up=uxuy mod2w

    TwosComplementMultiplication

    i nt x, y;

    i nt p = x * y;

    Computeexactproductoftwowbitnumbersx,y

    Truncateresulttowbitnumberp =TMultw(x,y)

  • 5/21/2018 UCLA CS 33 - Reinman - Spring 2014

    29/38

    Unsigned vs. Signed Multiplication UnsignedMultiplication

    unsi gned ux = ( unsi gned) x;

    unsi gned uy = ( unsi gned) y;unsi gned up = ux * uy

    TwosComplementMultiplication

    i nt x, y;

    i nt p = x * y;

    Relation Signedmultiplicationgivessamebitlevelresultas

    unsigned

    up == ( unsi gned) p

  • 5/21/2018 UCLA CS 33 - Reinman - Spring 2014

    30/38

    Power-of-2 Multiply with Shift Operation

    u

  • 5/21/2018 UCLA CS 33 - Reinman - Spring 2014

    31/38

    Unsigned Power-of-2 Divide with Shift QuotientofUnsignedbyPowerof2

    u >> k gives u / 2k Useslogicalshift

    Division Computed Hex Binary

    x 15213 15213 3B 6D 00111011 01101101x >> 1 7606.5 7606 1D B6 00011101 10110110

    x >> 4 950.8125 950 03 B6 00000011 10110110

    x >> 8 59.4257813 59 00 3B 00000000 00111011

    0 0 1 0 0 0

    u

    2k/

    u / 2kDivision:

    Operands:

    k

    0

    u / 2k Result:

    .

    Binary Point

    0

    S d P f d h Sh f

  • 5/21/2018 UCLA CS 33 - Reinman - Spring 2014

    32/38

    Signed Power-of-2 Divide with Shift QuotientofSignedbyPowerof2

    x >> k gives x / 2k Usesarithmeticshift Roundswrongdirectionwhenu < 0

    0 0 1 0 0 0

    x

    2k/

    x / 2kDivision:

    Operands:

    k

    0

    RoundDown(x / 2k) Result:

    .

    Binary Point

    0

    Division Computed Hex Binary

    y -15213 -15213 C4 93 11000100 10010011y >> 1 -7606.5 -7607 E2 49 11100010 01001001

    y >> 4 -950.8125 -951 FC 49 11111100 01001001

    y >> 8 -59.4257813 -60 FF C4 11111111 11000100

    C P f D d

  • 5/21/2018 UCLA CS 33 - Reinman - Spring 2014

    33/38

    Correct Power-of-2 Divide QuotientofNegativeNumberbyPowerof2

    Want x / 2k (RoundToward0) Compute

    as

    ( x+2k

    - 1) / 2k InC:( x + ( 1 k

    Biasesdividendtoward0

    Case

    1:

    No

    rounding

    Divisor:

    Dividend:

    0 0 1 0 0 0

    u

    2k/

    u / 2k

    k

    1 0 0 0

    1 0 1 1 .

    Binary Point

    1

    0 0 0 1 1 1+2k +1

    1 1 1

    1 1 1 1

    Biasing has no effect

    C P f D d (C )

  • 5/21/2018 UCLA CS 33 - Reinman - Spring 2014

    34/38

    Correct Power-of-2 Divide (Cont.)

    Divisor:

    Dividend:

    Case 2: Rounding

    0 0 1 0 0 0

    x

    2k/

    x / 2k

    k

    1

    1 0 1 1 .

    Binary Point

    1

    0 0 0 1 1 1+2k +1

    1

    Biasing adds 1 to final result

    Incremented by 1

    Incremented by 1

    C P l

  • 5/21/2018 UCLA CS 33 - Reinman - Spring 2014

    35/38

    C Puzzles Assumemachinewith32bitwordsize,twoscomplement

    integers

    ForeachofthefollowingCexpressions,either:

    Arguethatitistrueforallargumentvalues

    Giveexamplewherenottrue x < 0 ((x*2) < 0)

    ux >= 0

    x & 7 == 7 (x y -x < -y

    x * x >= 0

    x > 0 && y > 0 x + y > 0

    x >= 0 -x

  • 5/21/2018 UCLA CS 33 - Reinman - Spring 2014

    36/38

    C Puzzle Answers Assumemachinewith32bitwordsize,twoscomp.

    integers

    TMinmakesagoodcounterexampleinmanycases

    x < 0 ((x*2) < 0) False: TMin

    ux >= 0 True: 0 = UMin

    x & 7 == 7

    (x y -x < -y False: - 1, TMin

    x * x >= 0 False: 30426

    x > 0 && y > 0 x + y > 0 False: TMax, TMax

    x >= 0 -x = 0

    x & 7 == 7

    (x y -x < -y

    x * x >= 0

    x > 0 && y > 0 x + y > 0

    x >= 0 -x

  • 5/21/2018 UCLA CS 33 - Reinman - Spring 2014

    37/38

    ezThreeFourths/*

    *ezThreeFourthsmultipliesby3/4roundingtoward0,

    * ShouldexactlyduplicateeffectofCexpression(x*3/4),

    * includingoverflowbehavior.

    * Examples:ezThreeFourths(11)=8

    * ezThreeFourths(9)= 6

    * ezThreeFourths(1073741824)= 268435456(overflow)

    * Legalops:!~&^|+

    * Maxops:12* Rating:3

    */

    int ezThreeFourths(int x){

    }

    Th F h

  • 5/21/2018 UCLA CS 33 - Reinman - Spring 2014

    38/38

    ezThreeFourths/*

    *ezThreeFourthsmultipliesby3/4roundingtoward0,

    * ShouldexactlyduplicateeffectofCexpression(x*3/4),

    * includingoverflowbehavior.

    * Examples:ezThreeFourths(11)=8

    * ezThreeFourths(9)= 6

    * ezThreeFourths(1073741824)= 268435456(overflow)

    * Legalops:!~&^|+

    * Maxops:12* Rating:3

    */

    int ezThreeFourths(int x){

    int temp=x+x+x;

    int sign=temp>>31;

    int temp2=sign&((12;

    }