Download Numeric Encodings, Programming Implications - Lecture Notes | AMSC 662 and more Study notes Mathematics in PDF only on Docsity!
“The course that gives CMU its Zip!”
Topics Topics
n Numeric Encodings
l Unsigned & Two’s complement
n Programming Implications
l C promotion rules
n Basic operations
l Addition, negation, multiplication
n Programming Implications
l Consequences of overflow l Using shifts to perform power-of-2 multiply/divide
class03.ppt 15-213 F’
Integers
Sep 3, 2002
Integers
Sep 3, 2002
C Puzzles C Puzzles
n Taken from old exams
n Assume machine with 32 bit word size, two’s complement
integers
n For each of the following C expressions, either:
l Argue that is true for all argument values l Give example where not true
- x < 0 ⇒ ((x2) < 0)*
- ux >= 0
- x & 7 == 7 ⇒ (x<<30) < 0
- ux > -
- x > y ⇒ -x < -y
- x * x >= 0
- x > 0 && y > 0 ⇒ x + y > 0
- x >= 0 ⇒ -x <= 0
- x <= 0 ⇒ -x >= 0
int x = foo(); int y = bar(); unsigned ux = x; unsigned uy = y;
Initialization
Encoding Example (Cont.) Encoding Example (Cont.)
x = 15213: 00111011 01101101 y = -15213: 11000100 10010011
Weight 15213 - 1 1 1 1 1 2 0 0 1 2 4 1 4 0 0 8 1 8 0 0 16 0 0 1 16 32 1 32 0 0 64 1 64 0 0 128 0 0 1 128 256 1 256 0 0 512 1 512 0 0 1024 0 0 1 1024 2048 1 2048 0 0 4096 1 4096 0 0 8192 1 8192 0 0 16384 0 0 1 16384 -32768 0 0 1 - Sum 15213 -
Numeric Ranges Numeric Ranges
Unsigned Values Unsigned Values
n UMin = 0
n UMax = 2 w^ – 1
Two’s Complement Values Two’s Complement Values
n TMin = –2 w –
n TMax = 2 w –1^ – 1
Other Values Other Values
n Minus 1
Decimal Hex Binary UMax 65535 FF FF 11111111 11111111 TMax 32767 7F FF 01111111 11111111 TMin -32768 80 00 10000000 00000000 -1 -1 FF FF 11111111 11111111 0 0 00 00 00000000 00000000
Values for W = 16
Unsigned & Signed Numeric Values
Equivalence Equivalence
n Same encodings for
nonnegative values
Uniqueness Uniqueness
n Every bit pattern represents
unique integer value
n Each representable integer
has unique bit encoding
⇒ ⇒ Can Invert MappingsCan Invert Mappings
n U2B( x ) = B2U -1 ( x )
l Bit pattern for unsigned integer
n T2B( x ) = B2T -1 ( x )
l Bit pattern for two’s comp integer
X B2U( X ) B2T( X )
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 Casting Signed to Unsigned
C Allows Conversions from Signed to Unsigned C Allows Conversions from Signed to Unsigned
Resulting Value Resulting Value
n No change in bit representation
n Nonnegative values unchanged
l ux = 15213
n Negative values change into (large) positive values
l uy = 50323
Relation Between Signed & Unsigned Relation Between Signed & Unsigned
n uy = y + 2 * 32768 = y + 65536
Weight -15213 50323 1 1 1 1 1 2 1 2 1 2 4 0 0 0 0 8 0 0 0 0 16 1 16 1 16 32 0 0 0 0 64 0 0 0 0 128 1 128 1 128 256 0 0 0 0 512 0 0 0 0 1024 1 1024 1 1024 2048 0 0 0 0 4096 0 0 0 0 8192 0 0 0 0 16384 1 16384 1 16384 32768 1 -32768 1 32768 Sum -15213 50323
Signed vs. Unsigned in C Signed vs. Unsigned in C
Constants Constants
n By default are considered to be signed integers
n Unsigned if have “U” as suffix
0U, 4294967259U
Casting Casting
n Explicit casting between signed & unsigned same as U2T and
T2U
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;
TMax
TMin
UMax UMax – 1
TMax
TMax + 1
2’s Comp.
Range
Unsigned
Range
Explanation of Casting Surprises Explanation of Casting Surprises
2’s Comp. 2’s Comp. →→ UnsignedUnsigned
n Ordering Inversion
n Negative → Big Positive
Sign Extension Sign Extension
Task: Task:
n Given w -bit signed integer x
n Convert it to w + k -bit integer with same value
Rule: Rule:
n Make k copies of sign bit:
n X ′ = xw –1 ,…, xw –1 , xw –1 , xw –2 ,…, x 0
k copies of MSB X • • •
X ′ • • • • • •
w
k^ w
Justification For Sign Extension Justification For Sign Extension
Prove Correctness by Induction on Prove Correctness by Induction on kk
n Induction Step: extending by single bit maintains value
n Key observation: –2 w –1^ = –2 w^ +2 w –
n Look at weight of upper bits:
X –2 w –1^ x w – X ′ –2 w^ x w –1 + 2 w –1^ x w –1 = –2 w –1^ x w –
X - • • •
X ′ - + • • •
w +
w
Why Should I Use Unsigned? Why Should I Use Unsigned?
Don’t Don’t Use Just Because Number NonzeroUse Just Because Number Nonzero
n C compilers on some machines generate less efficient code
unsigned i; for (i = 1; i < cnt; i++) a[i] += a[i-1];
n Easy to make mistakes
for (i = cnt-2; i >= 0; i--) a[i] += a[i+1];
Do Do Use When Performing Modular ArithmeticUse When Performing Modular Arithmetic
n Multiprecision arithmetic
n Other esoteric stuff
Do Do Use When Need Extra Bit’s Worth of RangeUse When Need Extra Bit’s Worth of Range
n Working right up to limit of word size
Comp. & Incr. Examples Comp. & Incr. Examples
Decimal Hex Binary x 15213 3B 6D 00111011 01101101 ~x (^) -15214 C4 92 11000100 10010010 ~x+1 (^) -15213 C4 93 11000100 1001001^1 y -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
Unsigned Addition Unsigned Addition
Standard Addition FunctionStandard Addition Function
n Ignores carry output
Implements Modular ArithmeticImplements Modular Arithmetic
s = UAdd w ( u , v ) = u + v mod 2 w
UAdd u v u v u v u v u v w
w ( , ) = (^) w w
u
+ v
u + v • • •
True Sum: w +1 bits
Operands: w bits
Discard Carry: w bits UAdd w ( u , v )