Integers Topics n Representations of Integers l Basic
Integers Topics n Representations of Integers l Basic properties and operations l Implications for C
Integer C Puzzles n n Assume 32 -bit word size, two’s complement integers For each of the following C expressions, either: l Argue that is true for all argument values l Give example where not true Initialization int x = foo(); int y = bar(); unsigned ux = x; unsigned uy = y; – 2– • • • • x < 0 ((x*2) < 0) ux >= 0 x & 7 == 7 (x<<30) < 0 ux > -1 x > y -x < -y x * x >= 0 x > 0 && y > 0 x + y > 0 x >= 0 -x <= 0 -x >= 0 (x|-x)>>31 == -1 ux >> 3 == ux/8 x >> 3 == x/8 x & (x-1) != 0 CMSC 313, F ‘ 09
Encoding Integers Unsigned Two’s Complement short int x = 15213; short int y = -15213; n Sign Bit C short 2 bytes long Sign Bit n For 2’s complement, most significant bit indicates sign l 0 for nonnegative l 1 for negative – 3– CMSC 313, F ‘ 09
Encoding Example (Cont. ) x = y = – 4– 15213: 00111011 01101101 -15213: 11000100 10010011 CMSC 313, F ‘ 09
Numeric Ranges Unsigned Values n UMin = 0 Two’s Complement Values n TMin = – 2 w– 1 000… 0 n UMax 100… 0 = 2 w – 1 111… 1 n TMax = 2 w– 1 011… 1 Other Values n Minus 1 Values for W = 16 – 5– 111… 1 CMSC 313, F ‘ 09
Values for Different Word Sizes C Programming Observations n |TMin | = TMax + 1 n l K&R App. B 11 l Asymmetric range n UMax 1 = 2 * TMax + #include <limits. h> n Declares constants, e. g. , l ULONG_MAX l LONG_MIN n – 6– Values platform-specific CMSC 313, F ‘ 09
Unsigned & Signed Numeric Values X 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111 – 7– B 2 U(X) 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 B 2 T(X) 0 1 2 3 4 5 6 7 – 8 – 7 – 6 – 5 – 4 – 3 – 2 – 1 Equivalence n Same encodings for nonnegative values Uniqueness n n Every bit pattern represents unique integer value Each representable integer has unique bit encoding Can Invert Mappings n U 2 B(x) = B 2 U-1(x) l Bit pattern for unsigned integer n T 2 B(x) = B 2 T-1(x) l Bit pattern for two’s comp integer CMSC 313, F ‘ 09
Mapping Between Signed & Unsigned Two’s Complement x Unsigned T 2 U T 2 B B 2 U ux X Maintain Same Bit Pattern Unsigned Two’s Complement U 2 T ux U 2 B B 2 T x X Maintain Same Bit Pattern n – 8– Define mappings been unsigned and two’s complement numbers based on their bit-level representations CMSC 313, F ‘ 09
Mapping Signed Unsigned – 9– Bits Signed Unsigned 0000 0 0 0001 1 1 0010 2 2 0011 3 3 0100 4 4 0101 5 5 0110 6 6 0111 7 1000 -8 1001 -7 1010 -6 10 1011 -5 11 1100 -4 12 1101 -3 13 1110 -2 14 1111 -1 15 T 2 U 7 8 U 2 T 9 CMSC 313, F ‘ 09
Mapping Signed Unsigned – 10 – Bits Signed Unsigned 0000 0 0 0001 1 1 0010 2 2 0011 3 0100 4 4 0101 5 5 0110 6 6 0111 7 7 1000 -8 8 1001 -7 9 1010 -6 10 1011 -5 1100 -4 12 1101 -3 13 1110 -2 14 1111 -1 15 = +16 3 11 CMSC 313, F ‘ 09
Relation between Signed & Unsigned Two’s Complement Unsigned T 2 U T 2 B x B 2 U ux X Maintain Same Bit Pattern w– 1 ux + + + • • • 0 +++ x • • • +++ - ++ Large negative weight Large positive weight – 11 – CMSC 313, F ‘ 09
Signed vs. Unsigned in C Constants n By default are considered to be signed integers n Unsigned if have “U” as suffix 0 U, 4294967259 U Casting n Explicit casting between signed & unsigned same as U 2 T and T 2 U int tx, ty; unsigned ux, uy; tx = (int) ux; uy = (unsigned) ty; n Implicit casting also occurs via assignments and procedure calls tx = ux; uy = ty; – 12 – CMSC 313, F ‘ 09
Casting Surprises Expression Evaluation n If mix unsigned and signed in single expression, signed values implicitly cast to unsigned n Including comparison operations <, >, ==, <=, >= n Examples Constant 1 for W = 32 Constant 2 0 0 U -1 0 U 2147483647 – 13 – Relation 0 U 0 0 U -2147483648 -2147483647 -1 2147483647 U -2147483647 -1 -2147483648 -1 -2 -2 (unsigned) -1 -2 -2 2147483647 2147483648 U 2147483647 (int) 2147483648 U Evaluation == < > > unsigned < > > < > unsigned CMSC 313, F ‘ 09 signed
Explanation of Casting Surprises 2’s Comp. Unsigned n Ordering Inversion n Negative Big Positive TMax 2’s Comp. Range – 14 – 0 – 1 – 2 TMin UMax – 1 TMax + 1 TMax Unsigned Range 0 CMSC 313, F ‘ 09
Sign Extension Task: n Given w-bit signed integer x n Convert it to w+k-bit integer with same value Rule: n n Make k copies of sign bit: X = xw– 1 , …, xw– 1 , xw– 2 , …, x 0 w k copies of MSB X • • • X – 15 – • • • k • • • w CMSC 313, F ‘ 09
Sign Extension Example short int x = 15213; int ix = (int) x; short int y = -15213; int iy = (int) y; Decimal Hex 3 B 15213 00 00 3 B C 4 -15213 FF FF C 4 x ix y iy n n – 16 – 6 D 6 D 93 93 Binary 00111011 00000000 00111011 11000100 11111111 11000100 01101101 10010011 Converting from smaller to larger integer data type C automatically performs sign extension CMSC 313, F ‘ 09
Why Should I Use Unsigned? Don’t Use Just Because Number Nonnegative n Easy to make mistakes unsigned i; for (i = cnt-2; i >= 0; i--) a[i] += a[i+1]; n Can be very subtle #define DELTA sizeof(int) int i; for (i = CNT; i-DELTA >= 0; i-= DELTA). . . Do Use When Performing Modular Arithmetic n Multiprecision arithmetic Do Use When Using Bits to Represent Sets n – 17 – Logical right shift, no sign extension CMSC 313, F ‘ 09
Complement & Increment Claim: Following Holds for 2’s Complement ~x + 1 == -x Complement n Observation: ~x + x == 1111… 112 == -1 x 1001 110 1 + ~x 0110 001 0 -1 111 1 Increment n n n ~x + x == ~x + (-x + 1) ~x + 1 == -x -1 == -1 + (-x + 1) Warning: Be cautious treating int’s as integers – 18 – n OK here CMSC 313, F ‘ 09
Comp. & Incr. Examples x = 15213 0 – 19 – CMSC 313, F ‘ 09
Unsigned Addition u • • • v • • • u+v • • • UAddw(u , v) • • • Operands: w bits + True Sum: w+1 bits Discard Carry: w bits Standard Addition Function n Ignores carry output Implements Modular Arithmetic s – 20 – = UAddw(u , v) v mod 2 w = u+ CMSC 313, F ‘ 09
Two’s Complement Addition u • • • v • • • u+v • • • TAddw(u , v) • • • Operands: w bits + True Sum: w+1 bits Discard Carry: w bits TAdd and UAdd have Identical Bit-Level Behavior n n – 21 – Signed vs. unsigned addition in C: int s, t, u, v; s = (int) ((unsigned) u + (unsigned) v); t = u + v Will give s == t CMSC 313, F ‘ 09
TAdd Overflow Functionality n n n – 22 – True sum requires w+1 bits Drop off MSB Treat remaining bits as 2’s comp. integer True Sum 0 111… 1 2 w– 1 Pos. Over TAdd Result 0 100… 0 2 w – 1 011… 1 0 000… 0 1 011… 1 – 2 w – 1– 1 100… 0 1 000… 0 – 2 w Neg. Over CMSC 313, F ‘ 09
Characterizing TAdd Functionality n n n True sum requires w+1 bits Drop off MSB Treat remaining bits as 2’s comp. integer Pos. Over TAdd(u , v) >0 v <0 <0 u >0 Neg. Over (Neg. Over) (Pos. Over) – 23 – CMSC 313, F ‘ 09
Multiplication Computing Exact Product of w-bit numbers x, y n Either signed or unsigned Ranges n Unsigned: 0 ≤ x * y ≤ (2 w – 1) 2 = 22 w – 2 w+1 + 1 l Up to 2 w bits n Two’s complement min: x * y ≥ (– 2 w– 1)*(2 w– 1– 1) = – 22 w– 2 + 2 w– 1 l Up to 2 w– 1 bits n Two’s complement max: x * y ≤ (– 2 w– 1) 2 = 22 w– 2 l Up to 2 w bits, but only for (TMinw)2 Maintaining Exact Results n n – 24 – Would need to keep expanding word size with each product computed Done in software by “arbitrary precision” arithmetic packages CMSC 313, F ‘ 09
Unsigned Multiplication in C Operands: w bits * True Product: 2*w bits u · v u • • • v • • • UMultw(u , v) Discard w bits: w bits • • • Standard Multiplication Function n Ignores high order w bits Implements Modular Arithmetic UMultw(u , v) 2 w – 25 – = u · v mod CMSC 313, F ‘ 09
Signed Multiplication in C Operands: w bits * True Product: 2*w bits u · v Discard w bits: w bits u • • • v • • • TMultw(u , v) • • • Standard Multiplication Function n Ignores high order w bits n Some of which are different for signed vs. unsigned multiplication Lower bits are the same n – 26 – CMSC 313, F ‘ 09
Power-of-2 Multiply with Shift Operation n u << k gives u * 2 k n Both signed and unsigned Operands: w bits True Product: w+k bits Discard k bits: w bits Examples u · 2 k 2 k 0 • • • 0 1 0 • • • 0 0 • • • UMultw(u , 2 k) • • • 0 • • • 0 0 TMultw(u , 2 k) n u << 3 == u * 8 u << 5 - u << 3 == n Most machines shift and add faster than multiply n – 27 – * u k • • • u * 24 l Compiler generates this code automatically CMSC 313, F ‘ 09
Compiled Multiplication Code C Function int mul 12(int x) { return x * 12; } Compiled Arithmetic Operations leal (%eax, 2), %eax sall $2, %eax n – 28 – Explanation t <- x + x * 2 return t << 2; C compiler automatically generates shift/add code when multiplying by constant CMSC 313, F ‘ 09
Unsigned Power-of-2 Divide with Shift Quotient of Unsigned by Power of 2 n u >> k gives u / 2 k n Uses logical shift k Operands: Division: Result: – 29 – u / 2 k • • • Binary Point • • • 0 • • • 0 1 0 • • • 0 0 u / 2 k 0 • • • u / 2 k 0 • • • CMSC 313, F ‘ 09
Compiled Unsigned Division Code C Function unsigned udiv 8(unsigned x) { return x / 8; } Compiled Arithmetic Operations shrl $3, %eax n – 30 – Explanation # Logical shift return x >> 3; Uses logical shift for unsigned CMSC 313, F ‘ 09
Signed Power-of-2 Divide with Shift Quotient of Signed by Power of 2 n x >> k gives x / 2 k n Uses arithmetic shift Rounds wrong direction when u < 0 n Operands: Division: Result: – 31 – k x / 2 k Round. Down(x / 2 k) • • • Binary Point • • • 0 • • • 0 1 0 • • • 0 0 0 • • • CMSC 313, F ‘ 09
Correct Power-of-2 Divide Quotient of Negative Number by Power of 2 n n Want x / 2 k (Round Toward 0) Compute as (x+2 k-1)/ 2 k l In C: (x + (1<<k)-1) >> k l Biases dividend toward 0 Case 1: No rounding Dividend: k u +2 k – 1 1 / 2 k u / 2 k 0 • • • 0 0 1 • • • 1 1 1 Divisor: • • • 1 • • • 1 1 Binary Point 0 • • • 0 1 0 • • • 0 0 0 • • • 1 1 • • • . 1 • • • 1 1 Biasing has no effect – 32 – CMSC 313, F ‘ 09
Correct Power-of-2 Divide (Cont. ) Case 2: Rounding k Dividend: x +2 k – 1 1 • • • 0 • • • 0 0 1 • • • 1 1 1 • • • Incremented by 1 Divisor: / 2 k x / 2 k 0 • • • 0 1 0 • • • 0 0 0 • • • 1 1 Biasing adds 1 to final result – 33 – Binary Point • • • Incremented by 1 CMSC 313, F ‘ 09
Compiled Signed Division Code C Function int idiv 8(int x) { return x/8; } Compiled Arithmetic Operations testl %eax, %eax js L 4 L 3: sarl $3, %eax ret L 4: addl $7, %eax jmp L 3 – 34 – Explanation if x < 0 x += 7; # Arithmetic shift return x >> 3; n Uses arithmetic shift for int CMSC 313, F ‘ 09
Integer C Puzzles Revisited Initialization int x = foo(); int y = bar(); unsigned ux = x; unsigned uy = y; – 35 – • • • • x < 0 ((x*2) < 0) ux >= 0 x & 7 == 7 (x<<30) < 0 ux > -1 x > y -x < -y x * x >= 0 x > 0 && y > 0 x + y > 0 x >= 0 -x <= 0 -x >= 0 (x|-x)>>31 == -1 ux >> 3 == ux/8 x >> 3 == x/8 x & (x-1) != 0 CMSC 313, F ‘ 09
- Slides: 35