15-13 The corse that gies CMU its Zip! Topics class3.ppt Integers Sep 3,! Nmeric Encodings " Unsigned & Two s complement! Programming Implications " C promotion rles! Basic operations " Addition, negation, mltiplication! Programming Implications " Conseqences of oerflow " Using shifts to perform power-of- mltiply/diide 15-13 F C Pzzles! Taen from old exams! Assme machine with 3 bit word size, two s complement integers! For each of the following C expressions, either: " Arge that is tre for all argment ales " Gie example where not tre Initialization int x = foo(); int y = bar(); nsigned x = x; nsigned y = y; x <! ((x*) < ) x >= x & 7 == 7! (x<<3) < x > -1 x > y! -x < -y x * x >= x > && y >! x + y > x >=! -x <= x <=! -x >= 15-13, F Encoding Integers Unsigned Two s Complement w"1 # BU(X) = x i! i i= w! # BT(X) =!x w!1 " w!1 + x i " i i= Encoding Example (Cont.) x = 1513: 11111 11111 y = -1513: 111 1111 Weight1513-1513 short int x = 1513; short int y = -1513;! C short bytes long Sign Bit DecimalHexBinaryx Sign Bit! For s complement, most significant bit indicates sign " for nonnegatie " 1 for negatie 3 15-13, F 15-13, F
Nmeric Ranges Vales for Different Word Sizes Unsigned Vales! UMin =! UMax = w 1 111 1 Vales for W = 1 DecimalHexBinaryUMax Two s s Complement Vales! TMin = w 1 1! TMax = w 1 1 11 1 Other Vales! Mins 1 111 1 W13 Obserations! TMin = TMax + 1 " Asymmetric range! UMax = * TMax + 1 C Programming!!#inclde <limits.h> " K&R App. B11! Declares constants, e.g., "!ULONG_MAX "!LONG_MAX "!LONG_MIN! Vales platform-specific 5 15-13, F 15-13, F Unsigned & Signed Nmeric Vales X BU(X) BT(X) 1 1 1 1 11 3 3 1 11 5 5 11 111 1 7 7 11 9 7 11 1 111 11 5 11 1 111 13 3 111 1111 Eqialence! Same encodings for nonnegatie ales Uniqeness! Eery bit pattern represents niqe integer ale! Each representable integer has niqe bit encoding! Can Inert Mappings! UB(x) = BU -1 (x) " Bit pattern for nsigned integer 1! TB(x) = BT -1 (x) 15 1 " Bit pattern for two s comp 7 integer 15-13, F Casting Signed to Unsigned C Allows Conersions from Signed to Unsigned short int x = 1513; nsigned short int x = (nsigned short) x; short int y = -1513; nsigned short int y = (nsigned short) y; Reslting Vale! No change in bit representation! Nonnegatie ales nchanged " x = 1513! Negatie ales change into (large) positie ales " y = 533 15-13, F
Relation between Signed & Unsigned Relation Between Signed & Unsigned Weight-1513533 Two s Complement x TB TU X BU Unsigned x Maintain Same Bit Pattern - w 1 x + + + + + + x - + + + + + + w 1 w 1 = * w 1 = w x = " x x! # $ x + w x <! y = y + * 37 = y + 553 9 15-13, F 1 15-13, F Signed s. Unsigned in C Constants! By defalt are considered to be signed integers! Unsigned if hae U as sffix U, 99759U Casting! Explicit casting between signed & nsigned same as UT and TU int tx, ty; nsigned x, y; tx = (int) x; y = (nsigned) ty;! Implicit casting also occrs ia assignments and procedre calls tx = x; y = ty; 11 15-13, F Casting Srprises Expression Ealation! If mix nsigned and signed in single expression, signed ales implicitly cast to nsigned! Inclding comparison operations <, >, ==, <=, >=! Examples for W = 3 Constant 1 Constant Relation Ealation U == nsigned -1 < signed -1 U > nsigned 1737-173 > signed 1737U -173 < nsigned -1 - > signed (nsigned) -1 - > nsigned 1737 173U < nsigned 1737 (int) 173U > signed 1 15-13, F
Explanation of Casting Srprises Sign Extension s s Comp. " Unsigned! Ordering Inersion! Negatie " Big Positie UMax UMax 1 Tas:! Gien w-bit signed integer x! Conert it to w+-bit integer with same ale TMax TMax + 1 TMax Unsigned Range Rle:! Mae copies of sign bit:! X # = x w 1,, x w 1, x w 1, x w,, x s Comp. Range 1 copies of MSB X w TMin 13 15-13, F X # 1 15-13, F w Sign Extension Example short int x = 1513; int ix = (int) x; short int y = -1513; int iy = (int) y; Decimal Hex Binary x 1513 3B D 11111 11111 ix 1513 3B D 11111 11111 y -1513 C 93 111 1111 iy -1513 FF FF C 93 11111111 11111111 111 1111! Conerting from smaller to larger integer data type! C atomatically performs sign extension Jstification For Sign Extension Proe Correctness by Indction on! Indction Step: extending by single bit maintains ale X w - X # - + w+1! Key obseration: w 1 = w + w 1! Loo at weight of pper bits: X w 1 x w 1 15 15-13, F X # w x w 1 + w 1 x w 1 = w 1 x w 1 1 15-13, F
Why Shold I Use Unsigned? Don t Use Jst Becase Nmber Nonzero! C compilers on some machines generate less efficient code nsigned i; for (i = 1; i < cnt; i++) a[i] += a[i-1];! Easy to mae mistaes for (i = cnt-; i >= ; i--) a[i] += a[i+1]; Do Use When Performing Modlar Arithmetic! Mltiprecision arithmetic! Other esoteric stff Do Use When Need Extra Bit s s Worth of Range! Woring right p to limit of word size 17 15-13, F Negating with Complement & Increment Claim: Following Holds for s s Complement ~x + 1 == -x Complement! Obseration: ~x + x == 1111 11 == -1 Increment! ~x + x + (-x + 1) == -1 + (-x + 1)! ~x + 1 == -x Warning: Be catios treating int s as integers 1 15-13, F! OK here + x ~x -1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 Comp. & Incr. Examples x = 1513 lhexbinaryx Unsigned Addition Operands: w bits Tre Sm: w+1 bits + + DecimalHexBinary ~ Discard Carry: w bits UAdd w (, ) Standard Addition Fnction! Ignores carry otpt Implements Modlar Arithmetic s = UAdd w (, ) = + mod w UAdd w (,) = # + $ % +! w + < w + " w 19 15-13, F 15-13, F
Visalizing Integer Addition Visalizing Unsigned Addition Integer Addition! -bit integers,! Compte tre sm Add (, )! Vales increase linearly with and! Forms planar srface 3 1 1 Add (, ) Integer Addition 1 1 1 1 1 1 Wraps Arond! If tre sm " w! At most once Tre Sm w+1 w Oerflow Modlar Sm 1 1 1 1 Oerflow UAdd (, ) 1 1 1 1 1 1 1 15-13, F 15-13, F Mathematical Properties Modlar Addition Forms an Abelian Grop! Closed nder addition!!$!uadd w (, )!! $! w 1! Commtatie UAdd w (, )!!=!! UAdd w (, )! Associatie UAdd w (t, UAdd w (, ))!!=!! UAdd w (UAdd w (t, ), )! is additie identity UAdd w (, )!!=!!! Eery element has additie inerse " Let UComp w ( )!!=! w UAdd w (, UComp w ( ))!!=!! Two s Complement Addition Operands: w bits Tre Sm: w+1 bits TAdd and UAdd hae Identical Bit-Leel Behaior! Signed s. nsigned addition in C: int s, t,, ; s = (int) ((nsigned) + (nsigned) ); t = +! Will gie s == t + + Discard Carry: w bits TAdd w (, ) 3 15-13, F 15-13, F
Characterizing TAdd Fnctionality! Tre sm reqires w+1 bits! Drop off MSB! Treat remaining bits as s comp. integer TAdd(, ) > < < > NegOer PosOer 111 1 1 1 1 1 TAdd w (,) = Tre Sm w 1 w 1 w 1 5 15-13, F w # + + w!1 % $ + % & +! w!1 PosOer NegOer TAdd Reslt 11 1 1 + < TMin (NegOer) w TMin w " + " TMax w TMax w < + (PosOer) Visalizing s Comp. Addition Vales! -bit two s comp.! Range from - to +7 Wraps Arond! If sm % w 1 " Becomes negatie " At most once! If sm < w 1 " Becomes positie " At most once NegOer - - - - - - - - TAdd (, ) 15-13, F - - - - PosOer Detecting s Comp. Oerflow Tas Claim! Gien s = TAdd w (, )! Determine if s = Add w (, )! Example int s,, ; s = + ;! Oerflow iff either:, <, s % (NegOer), %, s < (PosOer) w 1 w 1 of = (< == <) && (<!= s<); PosOer NegOer Mathematical Properties of TAdd Isomorphic Algebra to UAdd! TAdd w (, ) = UT(UAdd w (TU( ), TU())) " Since both hae identical bit patterns Two s s Complement Under TAdd Forms a Grop! Closed, Commtatie, Associatie, is additie identity! Eery element has additie inerse Let TComp w ( )!!=! UT(UComp w (TU( )) TAdd w (, TComp w ( ))!!=!! TComp w () = #! " TMin w $ % TMin w = TMin w 7 15-13, F 15-13, F
Mltiplication Compting Exact Prodct of w-bit nmbers x, y! Either signed or nsigned Ranges! Unsigned:! x * y! ( w 1) = w w+1 + 1 " Up to w bits! Two s complement min: x * y " ( w 1 )*( w 1 1) = w + w 1 " Up to w 1 bits! Two s complement max: x * y! ( w 1 ) = w " Up to w bits, bt only for (TMin w ) Maintaining Exact Reslts! Wold need to eep expanding word size with each prodct compted! Done in software by arbitrary precision arithmetic pacages Unsigned Mltiplication in C Operands: w bits Tre Prodct: *w bits Discard w bits: w bits UMlt w (, ) Standard Mltiplication Fnction! Ignores high order w bits Implements Modlar Arithmetic UMlt w (, ) = mod w * 9 15-13, F 3 15-13, F Unsigned s. Signed Mltiplication Unsigned Mltiplication nsigned x = (nsigned) x; nsigned y = (nsigned) y; nsigned p = x * y! Trncates prodct to w-bit nmber p = UMlt w (x, y)! Modlar arithmetic: p = x & y mod w Two s s Complement Mltiplication int x, y; int p = x * y;! Compte exact prodct of two w-bit nmbers x, y! Trncate reslt to w-bit nmber p = TMlt w (x, y) Unsigned s. Signed Mltiplication Unsigned Mltiplication nsigned x = (nsigned) x; nsigned y = (nsigned) y; nsigned p = x * y Two s s Complement Mltiplication int x, y; Relation int p = x * y;! Signed mltiplication gies same bit-leel reslt as nsigned! p == (nsigned) p 31 15-13, F 3 15-13, F
Power-of- Mltiply with Shift Operation! << gies *! Both signed and nsigned Operands: w bits Tre Prodct: w+ bits Examples Discard bits: w bits UMlt w (, )! << 3 == *! << 5 - << 3 == * 1! Most machines shift and add mch faster than mltiply " Compiler generates this code atomatically 33 15-13, F * TMlt w (, ) Unsigned Power-of- Diide with Shift Qotient of Unsigned by Power of! >> gies ) / *! Uses logical shift Operands: Diision: Reslt: / / 1 DiisionComptedHexBinaryx x > 3 15-13, F. ) / * Binary Point Signed Power-of- Diide with Shift Correct Power-of- Diide Qotient of Signed by Power of! x >> gies ) x / *! Uses arithmetic shift! Ronds wrong direction when < x Operands: / 1 Diision: Reslt: x / RondDown(x / ) DiisionComptedHexBinaryy. Binary Point y Qotient of Negatie Nmber by Power of! Want ' x / ( (Rond Toward )! Compte as ) (x+ -1)/ * " In C: (x + (1<<)-1) >> " Biases diidend toward Case 1: No ronding Diidend: Diisor: / ' / ( 1 + + 1 1 1 1 1 1 1 1 1 1 1 1 1. Binary Point 1 1 1 35 15-13, F Biasing has no effect 3 15-13, F
Correct Power-of- Diide (Cont.) Case : Ronding Diidend: Diisor: / x ' x / ( 1 + + 1 1 1 1 1 Incremented by 1 Biasing adds 1 to final reslt 1 1 1 1 1. Incremented by 1 Binary Point Properties of Unsigned Arithmetic Unsigned Mltiplication with Addition Forms Commtatie Ring! Addition is commtatie grop! Closed nder mltiplication!!$!umlt w (, )!!$! w 1! Mltiplication Commtatie UMlt w (, )!!=!! UMlt w (, )! Mltiplication is Associatie UMlt w (t, UMlt w (, ))!!=!! UMlt w (UMlt w (t, ), )! 1 is mltiplicatie identity UMlt w (, 1)!!=!!! Mltiplication distribtes oer addtion UMlt w (t, UAdd w (, ))!!=!! UAdd w (UMlt w (t, ), UMlt w (t, )) 37 15-13, F 3 15-13, F Properties of Two s Comp. Arithmetic Isomorphic Algebras! Unsigned mltiplication and addition " Trncating to w bits! Two s complement mltiplication and addition " Trncating to w bits Both Form Rings! Isomorphic to ring of integers mod w Comparison to Integer Arithmetic! Both are rings! Integers obey ordering properties, e.g., >! + > >, >! >! These properties are not obeyed by two s comp. arithmetic C Pzzle Answers! Assme machine with 3 bit word size, two s comp. integers! TMin maes a good conterexample in many cases # x <! ((x*) < ) False: TMin # x >= Tre: = UMin # x & 7 == 7! (x<<3) < Tre: x 1 = 1 # x > -1 False: # x > y! -x < -y False: -1, TMin # x * x >= False: 3 # x > && y >! x + y > False: TMax, TMax # x >=! -x <= Tre: TMax < # x <=! -x >= False: TMin TMax + 1 == TMin 39 1513 * 3 == -13 (1-bit words) 15-13, F 15-13, F