Professional Documents
Culture Documents
C Puzzles PDF
C Puzzles PDF
•Realize that xw − 1 ⋅2 = ∑
w− 1
• Subtract 1 then compliment each bit (reverse example above) 2 +1 i
i =0
•Remember that we added 1 when converting to 2’s complement? Subtract it and the +1
goes away.
Signed Representation Using Two’s Compliment •This leaves us with a set with a number as wide as the original, with all of the powers of
• Encode magnitudes in raw binary, leaving high-order bit for sign two represented (all bits on)
• Take 2’s compliment of negative numbers, this will set sign bit. 3 = - •Now, think about the right side of that addition. Originally those bits contained the
-3 = 0011 (3 in raw binary) powers of two that were present. But we complemented it. So, now it contains the powers
of two that were not present in the original number.
1100 (after compliment) •So, by subtracting those powers of two that were not represented in our original number
1101 (2’s compliment, after adding 1) from a set of all powers of two, we are left with only those that were originally
represented.
class03.ppt –3– 15-213 S’02 (Based on 15-213 F’01) class03.ppt –4– 15-213 S’02 (Based on 15-213 F’01)
Encoding Integers Encoding Example (Cont.)
x = 15213: 00111011 01101101
short int x = 15213; y = -15213: 11000100 10010011
short int y = -15213; Weight 15213 -15213
1 1 1 1 1
2 0 0 1 2
C short 2 bytes long 4 1 4 0 0
8 1 8 0 0
Decimal Hex Binary 16 0 0 1 16
x 15213 3B 6D 00111011 01101101 32 1 32 0 0
y -15213 C4 93 11000100 10010011 64 1 64 0 0
128 0 0 1 128
Sign Bit 256 1 256 0 0
512 1 512 0 0
• For 2’s complement, most significant bit indicates sign 1024 0 0 1 1024
– 0 for nonnegative 2048 1 2048 0 0
– 1 for negative 4096 1 4096 0 0
8192 1 8192 0 0
16384 0 0 1 16384
-32768 0 0 1 -32768
Sum 15213 -15213
class03.ppt –5– 15-213 S’02 (Based on 15-213 F’01) class03.ppt –6– 15-213 S’02 (Based on 15-213 F’01)
Relation Between 2’s Comp. & Unsigned Relation Between Signed & Unsigned
Weight -15213 50323
1 1 1 1 1
2 1 2 1 2
Two’s Complement Unsigned 4 0 0 0 0
T2U
8 0 0 0 0
x T2B B2U ux 16 1 16 1 16
X 32 0 0 0 0
64 0 0 0 0
Maintain Same Bit Pattern 128 1 128 1 128
w–1 0 256 0 0 0 0
512 0 0 0 0
ux + + + • • • +++ 1024 1 1024 1 1024
- x -++ • • • +++ 2048 0 0 0 0
4096 0 0 0 0
+2w–1 – –2w–1 = 2*2w–1 = 2w 8192 0 0 0 0
16384 1 16384 1 16384
x x≥0
ux = 32768 1 -32768 1 32768
x + 2 x <0
w
Sum -15213 50323
• uy = y + 2 * 32768 = y + 65536
class03.ppt – 11 – 15-213 S’02 (Based on 15-213 F’01) class03.ppt – 12 – 15-213 S’02 (Based on 15-213 F’01)
Signed vs. Unsigned in C Casting Surprises
Constants Expression Evaluation
• By default are considered to be signed integers • If mix unsigned and signed in single expression, signed values
• Unsigned if have “U” as suffix implicitly cast to unsigned
0U, 4294967259U • Including comparison operations <, >, ==, <=, >=
• Examples for W = 32
Casting
• Explicit casting between signed & unsigned same as U2T and T2U Constant1 Constant2 Relation Evaluation
int tx, ty; 0 0U == unsigned
unsigned ux, uy; -1 0 < signed
tx = (int) ux; -1 0U > unsigned
uy = (unsigned) ty; 2147483647 -2147483648 > signed
• Implicit casting also occurs via assignments and procedure calls 2147483647U -2147483648 < unsigned
tx = ux; -1 -2 > signed
uy = ty; (unsigned) -1 -2 > unsigned
2147483647 2147483648U < unsigned
2147483647 (int) 2147483648U > signed
class03.ppt – 13 – 15-213 S’02 (Based on 15-213 F’01) class03.ppt – 14 – 15-213 S’02 (Based on 15-213 F’01)
2’s Comp.
Range 0 0 Do Use When Need Extra Bit’s Worth of Range
–1 • Working right up to limit of word size
–2
TMin
class03.ppt – 15 – 15-213 S’02 (Based on 15-213 F’01) class03.ppt – 16 – 15-213 S’02 (Based on 15-213 F’01)
Sign Extension Sign Extension Example
Task: short int x = 15213;
• Given w-bit signed integer x int ix = (int) x;
• Convert it to w+k-bit integer with same value short int y = -15213;
int iy = (int) y;
Rule:
• Make k copies of sign bit: Decimal Hex Binary
x 15213 3B 6D 00111011 01101101
• X ′= x w–1 ,… , x w–1 , x w–1 , x w–2 ,… , x0
ix 15213 00 00 C4 92 00000000 00000000 00111011 01101101
k copies of MSB w y -15213 C4 93 11000100 10010011
iy -15213 FF FF C4 93 11111111 11111111 11000100 10010011
X • • •
class03.ppt – 17 – 15-213 S’02 (Based on 15-213 F’01) class03.ppt – 18 – 15-213 S’02 (Based on 15-213 F’01)
x 10011101
X ′- + • • • + ~x 01100010
w+1
-1 11111111
• Key observation: –2w–1 = –2w +2w–1
-2w–1 - 2 w–1 = –2w +2w–1 - 2 w–1 Increment
2(-2w–1) = –2w • ~x + x + (-x + 1) == -1 + (-x + 1)
–2w = –2w
• ~x + 1 == -x
• Look at weight of upper bits:
X –2w–1 x w–1 Warning: Be cautious treating int’s as integers
X′ –2w xw–1 + 2w–1 x w–1 = –2w–1 x w–1
• OK here
class03.ppt – 19 – 15-213 S’02 (Based on 15-213 F’01) class03.ppt – 20 – 15-213 S’02 (Based on 15-213 F’01)
Comp. & Incr. Examples Unsigned Addition
x = 15213
u • • •
Decimal Hex Binary Operands: w bits
x 15213 3B 6D 00111011 01101101 + v • • •
~x -15214 C4 92 11000100 10010010
True Sum: w+1 bits u+v • • •
~x+1 -15213 C4 93 11000100 10010011
y -15213 C4 93 11000100 10010011
Discard Carry: w bits UAddw (u , v) • • •
0
Decimal Hex Binary
0 0 00 00 00000000 00000000 Standard Addition Function
~0 -1 FF FF 11111111 11111111 • Ignores carry output
~0+1 0 00 00 00000000 00000000
Implements Modular Arithmetic
s = UAdd w(u , v) = u + v mod 2 w
u+ v u + v < 2w
UAdd w (u,v) =
u + v − 2 u + v ≥ 2w
w
class03.ppt – 21 – 15-213 S’02 (Based on 15-213 F’01) class03.ppt – 22 – 15-213 S’02 (Based on 15-213 F’01)
v 28 True Sum 16
14
• Forms planar
24
2 w+1
20 Overflow 12
surface 16
14
10
12 8
10
12
2w 6 12
14
8
8 4 10
4 8
0 4
6
v 2
6
v
0
2 2
0 0 4
4
6
8 Modular Sum 0
2 2
u 10
12
14
0
u
4
6
8 0
10
12
14
class03.ppt – 23 – 15-213 S’02 (Based on 15-213 F’01) class03.ppt – 24 – 15-213 S’02 (Based on 15-213 F’01)
Mathematical Properties Two’s Complement Addition
Modular Addition Forms an Abelian Group u • • •
• Closed under addition Operands: w bits
+ v • • •
0 ≤UAdd w(u , v) ≤ 2 w –1
• Commutative True Sum: w+1 bits u+v • • •
UAdd w(u , v) = UAdd w(v , u)
Discard Carry: w bits TAddw (u , v) • • •
(u+v) == (v+u)
• Associative
UAdd w(t, UAdd w(u , v)) = UAdd w(UAdd w(t, u ), v) TAdd and UAdd have Identical Bit-Level Behavior
(t + (u+v)) == ((t+u) + v) • Signed vs. unsigned addition in C:
• 0 is additive identity int s, t, u, v;
UAdd w(u , 0) = u s = (int) ((unsigned) u + (unsigned) v);
(u + 0) == u t = u + v
• Every element has additive inverse • Will give s == t
– Let UComp w (u ) = 2 w – u
UAdd w(u , UComp w (u )) = 0
class03.ppt – 25 – 15-213 S’02 (Based on 15-213 F’01) class03.ppt – 26 – 15-213 S’02 (Based on 15-213 F’01)
>0 negative 2
1 000… 0 –2 w NegOver 0
6
v – At most once -2 4
2
<0 w− 1 • If sum < –2w–1 -4
u + v + 2
0
u + v < TMin w (NegOver) -6
– Becomes -8
-2
v
TAddw (u,v) = u + v TMinw ≤u + v ≤TMax w
-4
<0 >0 positive -8
u -6
-4 -6
u + v − 2 w − 1
-2
TMax w < u + v
0 -8
2
NegOver (PosOver) – At most once u 4
6 PosOver
class03.ppt – 27 – 15-213 S’02 (Based on 15-213 F’01) class03.ppt – 28 – 15-213 S’02 (Based on 15-213 F’01)
Detecting 2’s Comp. Overflow Mathematical Properties of TAdd
Task Isomorphic Algebra to UAdd
• Given s = TAdd w(u , v) 2 w–1 • TAdd w(u , v) = U2T(UAdd w(T2U(u ), T2U(v)))
• Determine if s = Add w(u , v) PosOver
– Since both have identical bit patterns
• Example 2 w –1
int s, u, v; Two’s Complement Under TAdd Forms a Group
s = u + v; • Closed, Commutative, Associative, 0 is additive identity
0
• Every element has additive inverse
Claim Let TComp w (u ) = U2T(UComp w(T2U(u ))
• Overflow iff either: TAdd w(u , TComp w (u )) = 0
u, v < 0, s ≥ 0 (NegOver)
NegOver
u, v ≥ 0, s < 0 (PosOver)
ovf = (u<0 == v<0) && (u<0 != s<0); − u u ≠ TMinw
TCompw (u) =
TMinw u = TMinw
class03.ppt – 29 – 15-213 S’02 (Based on 15-213 F’01) class03.ppt – 30 – 15-213 S’02 (Based on 15-213 F’01)
class03.ppt – 31 – 15-213 S’02 (Based on 15-213 F’01) class03.ppt – 32 – 15-213 S’02 (Based on 15-213 F’01)
Unsigned Multiplication in C Unsigned vs. Signed Multiplication
u • • • Unsigned Multiplication
Operands: w bits unsigned ux = (unsigned) x;
* v • • •
unsigned uy = (unsigned) y;
True Product: 2*w bits u · v • • • • • • unsigned up = ux * uy
• • • • Truncates product to w-bit number up = UMult w(ux, uy)
Discard w bits: w bits UMult w (u , v)
• Simply modular arithmetic
up = ux ⋅uy mod 2 w
Standard Multiplication Function
• Ignores high order w bits
Two’s Complement Multiplication
int x, y;
Implements Modular Arithmetic int p = x * y;
UMult w(u , v) = u · v mod 2w • Compute exact product of two w-bit numbers x, y
• Truncate result tow-bit number p = TMult w(x, y)
Relation
• Signed multiplication gives same bit-level result as unsigned
• up == (unsigned) p
class03.ppt – 33 – 15-213 S’02 (Based on 15-213 F’01) class03.ppt – 34 – 15-213 S’02 (Based on 15-213 F’01)
class03.ppt – 37 – 15-213 S’02 (Based on 15-213 F’01) class03.ppt – 38 – 15-213 S’02 (Based on 15-213 F’01)
class03.ppt – 39 – 15-213 S’02 (Based on 15-213 F’01) class03.ppt – 40 – 15-213 S’02 (Based on 15-213 F’01)
Properties of Two’s Comp. Arithmetic C Puzzle Answers
Isomorphic Algebras • Assume machine with 32 bit word size, two’s complement integers
• Unsigned multiplication and addition • TMin makes a good counterexample in many cases
– Truncating to w bits
• Two’s complement multiplication and addition • x < 0 ⇒ ((x*2) < 0) False: TMin
– Truncating to w bits
• ux >= 0 True: 0 = UMin
Both Form Rings
• x & 7 == 7 ⇒ (x<<30) < 0 True: x1 = 1
• Isomorphic to ring of integers mod 2 w
• ux > -1 False: 0
Comparison to Integer Arithmetic
• x > y ⇒ -x < -y False: -1, TMin
• Both are rings
• Integers obey ordering properties, e.g., • x * x >= 0 False: 30426
u>0 ⇒ u+v>v • x > 0 && y > 0 ⇒ x + y > 0 False: TMax, TMax
u > 0, v > 0 ⇒ u·v>0 • x >= 0 ⇒ -x <= 0 True: –TMax < 0
• These properties are not obeyed by two’s complement arithmetic
TMax + 1 == TMin • x <= 0 ⇒ -x >= 0 False: TMin
15213 * 30426 == -10030 (16-bit words)
class03.ppt – 41 – 15-213 S’02 (Based on 15-213 F’01) class03.ppt – 42 – 15-213 S’02 (Based on 15-213 F’01)