CISC3 Integers Sep, 9 Encoding Integers Unsigned w 1 BU(X) = x i i i= short int x = 1513; short int y = -1513; Twoʼs Complement w BT(X) = x w 1 w 1 + x i i i= Sign Bit Decimal Hex Binary x 1513 3B D 11111 11111 y -1513 C 93 111 1111 Powerpoint Lectre Notes for Compter Systems: A Programmer's Perspective, R. Bryant and D. O'Hallaron, Prentice Hall, 3 15-13 Fʼ CISC3 Fa9 Encoding Example (Cont.) x = 1513: 11111 11111 y = -1513: 111 1111 Weight 1513-1513 1 1 1 1 1 1 1 1 1 1 1 3 1 3 1 1 1 1 5 1 5 51 1 51 1 1 1 1 9 1 9 19 1 19 13 1 13-37 1-37 3 Sm 1513-1513 CISC3 Fa9 Nmeric Ranges Vales for W = 1 Decimal Hex Binary UMax 5535 FF FF 11111111 11111111 TMax 377 7F FF 1111111 11111111 TMin -37 1-1 -1 FF FF 11111111 11111111 CISC3 Fa9 Page 1
Vales for Different Word Sizes W 1 3 UMax 55 5,535,9,97,95 1,,7,73,79,551,15 TMax 17 3,77,17,3,7 9,3,37,3,5,775,7 TMin -1-3,7 -,17,3, -9,3,37,3,5,775, 5 CISC3 Fa9 Unsigned & Signed Nmeric Vales X BU(X) BT(X) 1 1 1 1 11 3 3 1 11 5 5 11 111 7 7 1 11 9 7 11 1 111 11 5 11 1 111 13 3 111 1 1111 15 1 CISC3 Fa9 Casting Signed to Unsigned Relation between Signed & Unsigned short int x = 1513; nsigned short int x = (nsigned short) x; short int y = -1513; nsigned short int y = (nsigned short) y; Twoʼs Complement TU Unsigned x TB BU x X Maintain Same Bit Pattern - w 1 x + + + + + + x - + + + + + + w 1 w 1 = * w 1 = w x = x x x + w x < 7 CISC3 Fa9 CISC3 Fa9 Page
Relation Between Signed & Unsigned Weight -1513 533 1 1 1 1 1 1 1 1 1 1 1 1 3 1 1 1 1 1 5 51 1 1 1 1 1 9 19 13 1 13 1 13 37 1-37 1 37 Sm -1513 533 Signed vs. Unsigned in C y = y + * 37 = y + 553 9 CISC3 Fa9 1 CISC3 Fa9 Casting Srprises Explanation of Casting Srprises UMax UMax 1 U == nsigned -1 < signed -1 U > nsigned 1737-173 > signed 1737U -173 < nsigned -1 - > signed (nsigned) -1 - > nsigned 1737 173U < nsigned 11 1737 (int) 173U > signed CISC3 Fa9 ʼs Comp. Range TMax 1 TMax + 1 TMax TMin 1 CISC3 Fa9 Unsigned Range Page 3
Sign Extension Sign Extension Example short int x = 1513; int ix = (int) x; short int y = -1513; int iy = (int) y; k copies of MSB X w 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 Converting from smaller to larger integer data type C atomatically performs sign extension X 13 k w CISC3 Fa9 1 CISC3 Fa9 Jstification For Sign Extension Why Shold I Use Unsigned? X w - X - + w+1 15 CISC3 Fa9 1 CISC3 Fa9 Page
Negating with Complement & Increment + x ~x -1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 Comp. & Incr. Examples x = 1513 Decimal Hex Binary x 1513 3B D 11111 11111 ~x -151 C 9 111 111 ~x+1-1513 C 93 111 1111 y -1513 C 93 111 1111 Decimal Hex Binary ~ -1 FF FF 11111111 11111111 ~+1 17 CISC3 Fa9 1 CISC3 Fa9 Unsigned Addition Visalizing Integer Addition Operands: w bits Tre Sm: w+1 bits + v + v Add (, v) Integer Addition Discard Carry: w bits UAdd w (, v) 3 1 1 1 1 UAdd w (,v) = + v + v w + v < w + v w 1 1 1 1 v 19 CISC3 Fa9 CISC3 Fa9 Page 5
Visalizing Unsigned Addition Mathematical Properties Overflow UAdd (, v) Tre Sm w+1 Overflow 1 1 1 w Modlar Sm 1 1 1 1 1 1 1 v 1 CISC3 Fa9 CISC3 Fa9 Twoʼs Complement Addition Characterizing TAdd Operands: w bits Tre Sm: w+1 bits + v + v Tre Sm 111 1 w 1 PosOver 1 w 1 TAdd Reslt 11 1 Discard Carry: w bits TAdd w (, v) PosOver TAdd(, v) > v < < > NegOver 1 1 w 1 1 1 w NegOver + v + w 1 + v < TMin w (NegOver) TAdd w (,v) = + v TMin w + v TMax w + v w 1 TMax w < + v (PosOver) 3 CISC3 Fa9 CISC3 Fa9 Page
Visalizing ʼs Comp. Addition Detecting ʼs Comp. Overflow NegOver w 1 PosOver TAdd (, v) w 1 NegOver - - - - - - - - - - - - v PosOver 5 CISC3 Fa9 CISC3 Fa9 Mathematical Properties of TAdd Mltiplication TComp w () = TMin w TMin w = TMin w 7 CISC3 Fa9 CISC3 Fa9 Page 7
Unsigned Mltiplication in C Unsigned vs. Signed Mltiplication Operands: w bits Tre Prodct: *w bits v Discard w bits: w bits * v UMlt w (, v) 9 CISC3 Fa9 3 CISC3 Fa9 Unsigned vs. Signed Mltiplication Power-of- Mltiply with Shift Operands: w bits Tre Prodct: w+k bits k * k Discard k bits: w bits UMlt w (, k ) TMlt w (, k ) k 1 31 CISC3 Fa9 3 CISC3 Fa9 Page
Unsigned Power-of- Divide with Shift Signed Power-of- Divide with Shift Operands: Division: Reslt: / k / k / k k 1 Division Compted Hex Binary x 1513 1513 3B D 11111 11111 x >> 1 7.5 7 1D B 1111 11111 x >> 95.15 95 3 B 11 11111 x >> 59.5713 59 3B 11111 33 CISC3 Fa9. Binary Point Operands: Division: Reslt: / x k x / k RondDown(x / k ) k 1 3 CISC3 Fa9. Binary Point Division Compted Hex Binary y -1513-1513 C 93 111 1111 y >> 1-7.5-77 E 9 1111 111 y >> -95.15-951 FC 9 111111 111 y >> -59.5713 - FF C 11111111 111 Correct Power-of- Divide Correct Power-of- Divide (Cont.) Case : Ronding Dividend: x k 1 + k + 1 1 1 1 1 Dividend: Divisor: / k / k k 1 + k + 1 1 1 1 1 1 1 1 1 Binary Point 1 1 1 1. 1 1 1 Divisor: Incremented by 1 Binary Point / k 1 x / k 1 1 1 1. Biasing adds 1 to final reslt Incremented by 1 Biasing has no effect 35 CISC3 Fa9 3 CISC3 Fa9 Page 9
Properties of Unsigned Arithmetic Properties of Twoʼs Comp. Arithmetic 37 CISC3 Fa9 3 CISC3 Fa9 Work in Grop: C Pzzles Work in grop of two to solve these C pzzles Taken 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 vales Give example where not tre x < ((x*) < ) Initialization int x = foo(); int y = bar(); nsigned x = x; nsigned y = y; x >= x & 7 == 7 (x<<3) < x > -1 x > y -x < -y x * x >= x > && y > x + y > x >= -x <= 39 x <= CISC3 Fa9 -x >= C Pzzle Answers Assme machine with 3 bit word size, twoʼs comp. integers TMin makes 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 CISC3 Fa9 Page 1