Presentation is loading. Please wait.

Presentation is loading. Please wait.

Integers Topics Numeric Encodings Programming Implications

Similar presentations


Presentation on theme: "Integers Topics Numeric Encodings Programming Implications"— Presentation transcript:

1 Integers Topics Numeric Encodings Programming Implications
Unsigned & Two’s complement Programming Implications C promotion rules

2 Encoding Integers Unsigned Two’s Complement Sign Bit
For 2’s complement, most significant bit indicates sign 0 for nonnegative 1 for negative

3 Numeric Ranges Unsigned Values UMin = 0 UMax = 2w – 1
000…0 UMax = 2w – 1 111…1 Two’s Complement Values TMin = –2w–1 100…0 TMax = 2w–1 – 1 011…1 Other Values Minus 1 111…1 Values for W = 16

4 Values for Different Word Sizes
C Programming  #include <limits.h> K&R App. B11 Declares constants, e.g.,  INT_MAX  INT_MIN  UINT_MAX Values platform-specific Observations |TMin | = TMax + 1 Asymmetric range UMax = 2 * TMax + 1

5 UMax, Proof by Intuition
Assume 8-bit integers: TMax TMax * 2 TMax * 2 + 1 Therefore UMax = 2 * TMax + 1

6 Unsigned & Signed Numeric Values
X B2T(X) B2U(X) 0000 0001 1 0010 2 0011 3 0100 4 0101 5 0110 6 0111 7 –8 8 –7 9 –6 10 –5 11 –4 12 –3 13 –2 14 –1 15 1000 1001 1010 1011 1100 1101 1110 1111 Equivalence Same encodings for nonnegative values Uniqueness Every bit pattern represents unique integer value Each representable integer has unique bit encoding

7 Relation between Signed & Unsigned
T2U T2B B2U Two’s Complement Unsigned Maintain Same Bit Pattern x ux X

8 Sign Extension Task: Rule: Given w-bit signed integer x
Convert it to w+k-bit integer with same value Rule: Make k copies of sign bit: X  = xw–1 ,…, xw–1 , xw–1 , xw–2 ,…, x0 • • • X X  w k k copies of MSB

9 Sign Extension Example
short int x = ; int ix = (int) x; short int y = ; int iy = (int) y; Decimal Hex Binary x 15213 3B 6D ix B 6D y -15213 C4 93 iy FF FF C4 93 C automatically performs sign extension

10 Negating Short Cut Claim: Following Holds for 2’s Complement
~x + 1 == -x Complement Observation: ~x + x == 1111…112 == -1 Consequently: ~x + x = -1  ~x + 1 = -x 1 x ~x + -1

11 Casting Surprises Expression Evaluation
If mix unsigned and signed in single expression, signed values implicitly cast to unsigned Occurs with comparison operations <, >, ==, <=, >= Examples for W = 32 = 0x7FFFFFFF, and = 0x In the following chart, how does Constant1 compare to Constant2? Constant1 Constant2 Relation Evaluation 0 0U -1 0 -1 0U U (unsigned)-1 -2 0 0U == unsigned -1 0 < signed -1 0U > unsigned > signed U < unsigned > unsigned U (int) U

12 Explanation of Casting Surprises
2’s Comp.  Unsigned Ordering Inversion Negative  Big Positive TMax TMin –1 –2 UMax UMax – 1 TMax + 1 2’s Comp. Range Unsigned

13 Why Should I Use Unsigned?
Don’t Use Just Because Value is Nonzero Easy to make mistakes for (i = cnt-2; i >= 0; i--) a[i] += a[i+1]; Do Use When Representing Flags Bit-mapped devices Program state Do Use When Need Extra Bit’s Worth of Range Working right up to limit of word size


Download ppt "Integers Topics Numeric Encodings Programming Implications"

Similar presentations


Ads by Google