You are on page 1of 11

15-213 C Puzzles

Integers • Taken from Exam #2, CS 347, Spring ‘97


• Assume machine with 32 bit word size, two’s complement integers
January 22, 2002 • For each of the following C expressions, either:
Topics – Argue that is true for all argument values
• Numeric Encodings – Give example where not true
– Unsigned & Two’s complement • x < 0 ⇒ ((x*2) < 0)
• Programming Implications Initialization
– C promotion rules • ux >= 0
• Basic operations
int x = foo();
• x & 7 == 7 ⇒ (x<<30) < 0
– Addition, negation, multiplication int y = bar();
• Programming Implications • ux > -1
– Consequences of overflow unsigned ux = x;
• x > y ⇒ -x < -y
– Using shifts to perform power-of-2 multiply/divide unsigned uy = y;
• x * x >= 0
• Reading: 2.2 – 2.3
• x > 0 && y > 0 ⇒ x + y > 0
• Suggested Practice Problems: 2.40 – 2.45
• x >= 0 ⇒ -x <= 0
• x <= 0 ⇒ -x >= 0
class03.ppt CS 213 F’01 class03.ppt –2– 15-213 S’02 (Based on 15-213 F’01)

2’s Compliment Encoding Two’s Compliment in Math-ese


Conversion from Binary: Unsigned (Raw) Two’s Complement
• Compliment each bit, add 1 w− 1 w− 2
w−1
B2U(X ) = ∑ xi ⋅2 + ∑ x i ⋅2
B2T (X) = − xw − 1 ⋅2
i i
3 = 0011 (raw binary)
i=0 i=0
1100 (after compliment)
1101 (2’s compliment, after adding 1)
The left side is like a switch:
If positive number, no effect. Second part handled as for unsigned number
Conversion to Binary:
If negative: w− 2

•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)

Numeric Ranges Values for Different Word Sizes


Unsigned Values Two’s Complement Values
W
• UMin = 0 • TMin = –2 w–1 8 16 32 64
000… 0 100… 0 UMax 255 65,535 4,294,967,295 18,446,744,073,709,551,615
• UMax = 2w – 1 • TMax = 2 w–1 – 1 TMax 127 32,767 2,147,483,647 9,223,372,036,854,775,807
111… 1 011… 1 TMin -128 -32,768 -2,147,483,648 -9,223,372,036,854,775,808
Other Values
• Minus 1
111… 1
Observations C Programming
Values for W = 16 • |TMin | = TMax + 1 • #include <limits.h>
Decimal Hex Binary – Asymmetric range – K&R App. B11
UMax 65535 FF FF 11111111 11111111 • UMax = 2 * TMax + 1 • Declares constants, e.g.,
TMax 32767 7F FF 01111111 11111111 – ULONG_MAX
TMin -32768 80 00 10000000 00000000
– LONG_MAX
-1 -1 FF FF 11111111 11111111
0 0 00 00 00000000 00000000 – LONG_MIN
• Values platform-specific
class03.ppt –7– 15-213 S’02 (Based on 15-213 F’01) class03.ppt –8– 15-213 S’02 (Based on 15-213 F’01)
Unsigned & Signed Numeric Values Casting Signed to Unsigned
X B2U(X) B2T(X) Example Values C Allows Conversions from Signed to Unsigned
0000 0 0 • W=4
short int x = 15213;
0001 1 1 Equivalence unsigned short int ux = (unsigned short) x;
0010 2 2
• Same encodings for nonnegative short int y = -15213;
0011 3 3 values unsigned short int uy = (unsigned short) y;
0100 4 4
0101 5 5
Uniqueness
0110 6 6 • Every bit pattern represents Resulting Value
unique integer value
0111 7 7 • No change in bit representation
• Each representable integer has
1000 8 –8 • Nonnegative values unchanged
unique bit encoding
1001 9 –7 – ux = 15213
1010 10 –6 ⇒ Can Invert Mappings • Negative values change into (large) positive values
1011 11 –5 • U2B(x) = B2U-1(x)
– uy = 50323
1100 12 –4 – Bit pattern for unsigned integer
1101 13 –3 • T2B(x) = B2T-1(x)
1110 14 –2 – Bit pattern for two’s comp integer
1111 15 –1
class03.ppt –9– 15-213 S’02 (Based on 15-213 F’01) class03.ppt – 10 – 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)

Explanation of Casting Surprises Why Should I Use Unsigned?


2’s Comp. → Unsigned
• Ordering Inversion UMax
• Negative → Big Positive UMax – 1 Don’t Use Just Because Number Nonzero
Easy to make mistakes with implicit casting
for (i = cnt-2; i >= 0; i--)
TMax + 1 a[i] += a[i+1];
Unsigned
TMax TMax Range Do Use When Performing Modular Arithmetic
• Multiprecision arithmetic
• Bit masks, flags structures, and other bitwise data

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 • • •

• Converting from smaller to larger integer data type


• • •
• C automatically performs sign extension
X′ • • • • • •
k w

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)

Justification For Sign Extension Negating with Complement & Increment


Prove Correctness by Induction on k
• Induction Step: extending by single bit maintains value In C
~x + 1 == -x
w
X - • • • Complement
• Observation: ~x + x == 1111…112 == -1

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)

Visualizing Integer Addition Visualizing Unsigned Addition


Integer Addition Add4(u , v) Wraps Around Overflow
• 4-bit integers u • If true sum = 2w
and v Integer Addition
• At most once UAdd4(u , v)
• Compute true
sum Add4(u , v)
• Values increase
linearly with u and 32

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)

Characterizing TAdd Visualizing 2’s Comp. Addition


Functionality True Sum
• True sum requires w+1 0 111… 1 2 w–1 Values
bits PosOver NegOver
TAdd Result • 4-bit two’s
• Drop off MSB 0 100… 0 comp. TAdd4(u , v)
2 w –1 011… 1
• Treat remaining bits as • Range from -8
2’s comp. integer to +7
0 000… 0 0 000… 0
PosOver
Wraps Around 8
TAdd(u , v) 1 100… 0
• If sum ≥ 2w–1 6
–2 w –1 100… 0
– Becomes 4

>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)

Negating with Complement & Increment Multiplication


In C Computing Exact Product of w-bit numbers x, y
~x + 1 == -x • Either signed or unsigned
Complement Ranges
• Observation: ~x + x == 1111…112 == -1 • Unsigned: 0 = x * y = (2 w – 1) 2 = 2 2w – 2 w+1 + 1
– Up to 2w bits
x 10011101 • Two’s complement min: x * y = (–2 w–1)*(2 w–1–1) = –22w–2 + 2 w–1
+ ~x 01100010 – Up to 2w–1 bits
• Two’s complement max: x * y = (–2 w–1) 2 = 22w–2
-1 11111111 – Up to 2w bits, but only for TMinw2

Increment Maintaining Exact Results


• ~x + x + (-x + 1) == -1 + (-x + 1) • Would need to keep expanding word size with each product
computed
• ~x + 1 == -x
• Done in software by “arbitrary precision” arithmetic packages
Warning: Be cautious treating int’s as integers • Also implemented in Lisp, and other “advanced” languages
• OK here: We are using group properties of TAdd and TComp

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)

Power-of-2 Multiply with Shift Unsigned Power-of-2 Divide with Shift


Operation Quotient of Unsigned by Power of 2
• u << k gives u * 2k • u >> k gives u / 2k 
• Both signed and unsigned • Uses logical shift
k k
u • • • u ••• ••• Binary Point
Operands: w bits Operands:
* 2k 0 ••• 0 1 0 ••• 0 0 / 2k 0 ••• 0 1 0 ••• 0 0

True Product: w+k bits u · 2k • • • 0 ••• 0 0 Division: u / 2k 0 ••• 0 0 ••• . •••

UMult w (u , 2k ) ••• 0 ••• 0 0 Quotient: u / 2k  0 ••• 0 0 •••


Discard k bits: w bits
TMultw (u , 2k)
Division Computed Hex Binary
x 15213 15213 3B 6D 00111011 01101101
Examples x >> 1 7606.5 7606 1D B6 00011101 10110110
• u << 3 == u * 8 x >> 4 950.8125 950 03 B6 00000011 10110110
• u << 5 - u << 3 == u * 24 x >> 8 59.4257813 59 00 3B 00000000 00111011
• Most machines shift and add much faster than multiply
– Compiler will generate this code automatically
class03.ppt – 35 – 15-213 S’02 (Based on 15-213 F’01) class03.ppt – 36 – 15-213 S’02 (Based on 15-213 F’01)
2’s Comp Power-of-2 Divide with Shift Correct Power-of-2 Divide
Quotient of Signed by Power of 2 Quotient of Negative Number by Power of 2
• u >> k gives u / 2k  • Want u / 2k  (Round Toward 0)
• Uses arithmetic shift • Compute as (u+2k-1)/ 2k 
• Rounds wrong direction when u < 0 – In C: (u + (1<<k)-1) >> k
k
– Biases dividend toward 0
u ••• ••• Binary Point
Operands:
/ 2k 0 ••• 0 1 0 ••• 0 0 Case 1: No rounding
k
Division: u / 2k 0 ••• ••• . ••• Dividend: u 1 ••• 0 ••• 0 0
+2k +–1 0 ••• 0 0 1 ••• 1 1
Result: RoundDown(u / 2k ) 0 ••• •••
1 ••• 1 ••• 1 1 Binary Point
Division Computed Hex Binary
y -15213 -15213 C4 93 11000100 10010011 Divisor: / 2k 0 ••• 0 1 0 ••• 0 0
y >> 1 -7606.5 -7607 E2 49 11100010 01001001 u / 2k  0 ••• 1 1 1
1 ••• . 1 ••• 1 1
y >> 4 -950.8125 -951 FC 49 11111100 01001001
y >> 8 -59.4257813 -60 FF C4 11111111 11000100
Biasing has no effect

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)

Correct Power-of-2 Divide (Cont.) Properties of Unsigned Arithmetic


Case 2: Rounding Unsigned Multiplication with Addition Forms
k
Commutative Ring
Dividend: u 1 ••• •••
• Addition is commutative group
+2k +–1 0 ••• 0 0 1 ••• 1 1
• Closed under multiplication
1 ••• ••• 0 ≤UMult w(u , v) ≤ 2 w –1
• Multiplication Commutative
Incremented by 1 Binary Point UMult w(u , v) = UMult w(v , u)
• Multiplication is Associative
Divisor: / 2k 0 ••• 0 1 0 ••• 0 0
UMult w(t, UMult w(u , v)) = UMult w(UMult w(t, u ), v)
u / 2k  0 ••• 1 1 1
1 ••• . ••• • 1 is multiplicative identity
UMult w(u , 1) = u
Incremented by 1 • Multiplication distributes over addtion
UMult w(t, UAdd w(u , v)) = UAdd w(UMult w(t, u ), UMult w(t, v))

Biasing adds 1 to final result

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)

You might also like