0% found this document useful (0 votes)
100 views8 pages

15-213 Integer Arithmetic Operations Jan. 25, 2001 C Puzzles

The document discusses integer arithmetic operations in C and their implications. It begins by listing several C expressions and asking the reader to either argue they are true for all argument values or provide a counterexample. It then discusses: 1) Basic integer operations like addition, negation, and multiplication. 2) Programming implications of integer operations, including consequences of overflow and using shifts to perform powers of two multiplication/division. 3) Unsigned addition in more detail, how it discards the carry and essentially implements modular arithmetic, wrapping around at 2^w.

Uploaded by

adyakanks
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
100 views8 pages

15-213 Integer Arithmetic Operations Jan. 25, 2001 C Puzzles

The document discusses integer arithmetic operations in C and their implications. It begins by listing several C expressions and asking the reader to either argue they are true for all argument values or provide a counterexample. It then discusses: 1) Basic integer operations like addition, negation, and multiplication. 2) Programming implications of integer operations, including consequences of overflow and using shifts to perform powers of two multiplication/division. 3) Unsigned addition in more detail, how it discards the carry and essentially implements modular arithmetic, wrapping around at 2^w.

Uploaded by

adyakanks
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

C Puzzles

15-213 • Taken from Exam #2, CS 347, Spring ‘97


“The course that gives CMU its Zip!” • Assume machine with 32 bit word size, two’s complement integers
• For each of the following C expressions, either:

Integer Arithmetic Operations – Argue that is true for all argument values
– Give example where not true
Jan. 25, 2001 Initialization
• x < 0 ⇒ ((x*2) < 0)
• ux >= 0
int x = foo();
Topics • x & 7 == 7 ⇒ (x<<30) < 0
• Basic operations int y = bar();
• ux > -1
– Addition, negation, multiplication unsigned ux = x;
• Programming Implications • x > y ⇒ -x < -y
unsigned uy = y;
– Consequences of overflow • x * x >= 0
– Using shifts to perform power-of-2 • x > 0 && y > 0 ⇒ x + y > 0
multiply/divide
• x >= 0 ⇒ -x <= 0
• x <= 0 ⇒ -x >= 0
class04.ppt CS 213 S’01 class04.ppt –2– CS 213 S’01

Unsigned Addition Visualizing Integer Addition


u • • • Integer Addition
Operands: w bits • 4-bit integers u
+ v • • • and v
True Sum: w+1 bits u+v • • • • Compute true
sum Add4(u , v) Add4(u , v)

Discard Carry: w bits UAddw(u , v) • • • • Values increase 30


linearly with u and
v 25

Standard Addition Function • Forms planar


20
surface
• Ignores carry output
15

Implements Modular Arithmetic 12


14

10
s = UAddw(u , v) = u + v mod 2w 10
8
5
6 v
 u+ v u +v < 2
w 4
UAdd w (u,v) =  0

î u+v −2 u +v ≥ 2w
w 0 1 2
3 4
2
5 6 7 8 0
u 9 10 11
12 13 14
15

class04.ppt –3– CS 213 S’01 class04.ppt –4– CS 213 S’01

Page 1
Visualizing Unsigned Addition Mathematical Properties
Wraps Around Modular Addition Forms an Abelian Group
w Overflow


• If true sum • Closed under addition
• At most once 0 ≤ UAddw(u , v) ≤ 2w –1
UAdd4(u , v) • Commutative
UAddw(u , v) = UAddw(v , u)
16
• Associative
True Sum 14
UAddw(t, UAddw(u , v)) = UAddw(UAddw(t, u ), v)
2w+1 12
• 0 is additive identity
Overflow
10
UAddw(u , 0) = u
2w 8
14 • Every element has additive inverse
6 12
10 – Let UCompw (u ) = 2w – u
4
0 8
UAddw(u , UComp w (u )) = 0
6
Modular Sum 2
4
v
0
2
0 1 2
3 4 5 6
7 8 9 10 11 0
u 12 13 14
15

class04.ppt –5– CS 213 S’01 class04.ppt –6– CS 213 S’01

Detecting Unsigned Overflow Two’s Complement Addition


Task u • • •
• Given s = UAddw(u , v) No Overflow Operands: w bits
• Determine if s = u + v + v • • •
u v
Application True Sum: w+1 bits u+v • • •
s
unsigned s, u, v; 2w • • •
Discard Carry: w bits TAddw(u , v)
s = u + v;
Overflow
• Did addition overflow?
u v TAdd and UAdd have Identical Bit-Level Behavior
Claim
s • Signed vs. unsigned addition in C:
• Overflow iff s < u
2w int s, t, u, v;
ovf = (s < u)
s = (int) ((unsigned) u + (unsigned) v);
• Or symmetrically iff s < v
t = u + v
Proof • Will give s == t
• Know that 0 ≤ v < 2w
• No overflow ⇒ s = u + v ≥ u +0 =u
• Overflow ⇒ s = u + v – 2w < u +0 =u
class04.ppt –7– CS 213 S’01 class04.ppt –8– CS 213 S’01

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

• If sum ≥ 2w–1
4
TAdd(u , v) 1 100…0 –2w –1 100…0 2
– Becomes
>0 negative 0
6
1 000…0 –2w NegOver
v – At most once -2 4
2
<0 • If sum < –2w–1 -4
u + v + 2 w−1 u + v < TMin w (NegOver)
0

 – Becomes -6 -2

<0
u
>0 TAddw (u,v) = u + v TMinw ≤ u + v ≤ TMax w positive -8
-4

u + v − 2 w−1 TMax w < u + v (PosOver) -8 -7 -6


-5 -4 -3
-6
v
NegOver î – At most once u
-2 -1 0
1 2 3 4 5 6
-8 PosOver
7

class04.ppt –9– CS 213 S’01 class04.ppt – 10 – CS 213 S’01

Detecting 2’s Comp. Overflow Mathematical Properties of TAdd


Task Isomorphic Algebra to UAdd
• Given s = TAddw(u , v) 2w–1 • TAddw(u , v) = U2T(UAddw(T2U(u ), T2U(v)))
• Determine if s = Addw(u , v) PosOver
– Since both have identical bit patterns
• Example 2w –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 TCompw (u ) = U2T(UCompw (T2U(u ))
• Overflow iff either: TAddw(u , TCompw (u )) = 0
u, v < 0, s ≥ 0 (NegOver)
NegOver
u, v ≥ 0, s < 0 (PosOver)
−u u ≠ TMin w
ovf = (u<0 == v<0) && (u<0 != s<0);
TCompw (u) = 
Proof î TMin w u = TMin w
• Easy to see that if u ≥ 0 and v < 0, then TMinw ≤ u +v ≤ TMaxw
• Symmetrically if u < 0 and v ≥ 0
• Other cases from analysis of TAdd

class04.ppt – 11 – CS 213 S’01 class04.ppt – 12 – CS 213 S’01

Page 3
Two’s Complement Negation Negating with Complement & Increment
Mostly like Integer Negation In C
• TComp(u) = –u ~x + 1 == -x
2w–1
TMin is Special Case Complement
• TComp(TMin) = TMin
• Observation: ~x + x == 1111…112 == -1
Negation in C is Actually
TComp x 1 001 110 1
Tcomp(u )
mx = -x
• mx = TComp(x) + ~x 0 110 001 0
• Computes additive inverse –2w–1 2w–1

for TAdd -1 1 111 111 1


x + -x == 0 Increment
• ~x + x + (-x + 1) == -1 + (-x + 1)
• ~x + 1 == -x
–2w–1
1001 1011 1101 1111 0001 0011 0101 0111 Warning: Be cautious treating int’s as integers
1000 1010 1100 1110 0000 0010 0100 0110
• OK here: We are using group properties of TAdd and TComp
u
class04.ppt – 13 – CS 213 S’01 class04.ppt – 14 – CS 213 S’01

Comp. & Incr. Examples Comparing Two’s Complement Numbers


x = 15213 Task
Decimal Hex Binary • Given signed numbers u, v
x 15213 3B 6D 00111011 01101101 • Determine whether or not u > v
~x -15214 C4 92 11000100 10010010 – Return 1 for numbers in shaded region below
~x+1 -15213 C4 93 11000100 10010011
y -15213 C4 93 11000100 10010011 u==v
u < v

TMin >0
Decimal Hex Binary v u > v
TMin -32768 80 00 10000000 00000000 <0
~TMin 32767 7F FF 01111111 11111111
~TMin+1 -32768 80 00 10000000 00000000 <0 >0
u
0 Bad Approach
Decimal Hex Binary • Test (u–v) > 0
0 0 00 00 00000000 00000000 – TSub(u,v) = TAdd(u, TComp(v))
~0 -1 FF FF 11111111 11111111 • Problem: Thrown off by either Negative or Positive Overflow
~0+1 0 00 00 00000000 00000000

class04.ppt – 15 – CS 213 S’01 class04.ppt – 16 – CS 213 S’01

Page 4
Comparing with TSub Working Around Overflow Problems
Will Get Wrong Results Partition into Three Regions
NegOver TSub4(u , v) • u < 0, v ≥ 0 ⇒ u<v • u ≥ 0, v < 0 ⇒ u>v
• NegOver: u < 0, v > 0
– but u-v > 0 u<0
≥0 ≥0



• PosOver: u > 0, v < 0 v v v



– but u-v < 0 <0 <0 u
8 v<0

NegOver
6
<0 u ≥0 <0 u ≥0
4

u-v 2
• u, v same sign ⇒ u-v does not overflow
– Can safely use test (u–v) > 0
>0 + - 0

- + 6
-2 u-v
v 4
2



<0 - + ≥0 u ≥0 -
-4
0
+ - v +



-2 -6
v v v
-4
<0 >0 -6
-8
<0 u<0 <0 -
u -1 0 1
2 3 4 5
6 7
+
-8
-8 -7 -6
-5 -4 -3 -2
u v<0
PosOver
<0 u ≥0 <0 ≥0
u
PosOver
class04.ppt – 17 – CS 213 S’01 class04.ppt – 18 – CS 213 S’01

Multiplication Unsigned Multiplication in C


Computing Exact Product of w-bit numbers x, y u • • •
• Either signed or unsigned Operands: w bits
* v • • •
Ranges
w – 1) 2 = 22w – 2w+1 + 1
True Product: 2*w bits u · v • • • • • •

• Unsigned: 0 x * y

– Up to 2w bits UMultw(u , v) • • •
Discard w bits: w bits
–2w–1)*(2w–1–1) = –22w–2 + 2w–1


• Two’s complement min: x * y




– Up to 2w–1 bits Standard Multiplication Function


• Two’s complement max: x * y –2w–1) 2 = 22w–2



• Ignores high order w bits


– Up to 2w bits, but only for TMinw2
Implements Modular Arithmetic
Maintaining Exact Results UMultw(u , v) = u · v mod 2w
• Would need to keep expanding word size with each product computed
• Done in software by “arbitrary precision” arithmetic packages
• Also implemented in Lisp, ML, and other “advanced” languages

class04.ppt – 19 – CS 213 S’01 class04.ppt – 20 – CS 213 S’01

Page 5
Unsigned vs. Signed Multiplication Multiplication Examples
short int x = 15213;
Unsigned Multiplication
int txx = ((int) x) * x;
unsigned ux = (unsigned) x; int xx = (int) (x * x);
unsigned uy = (unsigned) y; int xx2 = (int) (2 * x * x);
unsigned up = ux * uy
• Truncates product to w-bit number up = UMultw(ux, uy) x = 15213: 00111011 01101101
txx = 231435369: 00001101 11001011 01101100 01101001
• Simply modular arithmetic
xx = 27753: 00000000 00000000 01101100 01101001
up = ux ⋅ uy mod 2w xx2 = -10030: 11111111 11111111 11011000 11010010
Two’s Complement Multiplication
int x, y; Observations
int p = x * y; • Casting order important
• Compute exact product of two w-bit numbers x, y – If either operand int, will perform int multiplication
• Truncate result tow-bit number p = TMultw(x, y) – If both operands short int, will perform short int multiplication
• Really is modular arithmetic
Relation – Computes for xx: 152132 mod 65536 = 27753
• Signed multiplication gives same bit-level result as unsigned
– Computes for xx2: (int) 55506U = -10030
• up == (unsigned) p
• Note that xx2 == (xx << 1)
class04.ppt – 21 – CS 213 S’01 class04.ppt – 22 – CS 213 S’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 ••• . •••

UMultw(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
class04.ppt – 23 – CS 213 S’01 class04.ppt – 24 – CS 213 S’01

Page 6
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
Divisor: / 2k 0 ••• 0 1 0 ••• 0 0
y -15213 -15213 C4 93 11000100 10010011
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

class04.ppt – 25 – CS 213 S’01 class04.ppt – 26 – CS 213 S’01

Correct Power-of-2 Divide (Cont.) Correct Power-of-2 Divide Examples


Case 2: Rounding
k
Dividend: u 1 ••• •••
+2k +–1 0 ••• 0 0 1 ••• 1 1
1 ••• ••• y/2k Computed Hex Binary
y -15213 -15213 C4 93 11000100 10010011
Incremented by 1 Binary Point y+1 -15212 C4 94 11000100 10010100
(y+1)>>1 -7606.5 -7606 E2 4A 11100010 01001010
Divisor: / 2k 0 ••• 0 1 0 ••• 0 0 y -15213 -15213 C4 93 11000100 10010011
y+15 -15197 C4 A2 11000100 10100010
 u / 2k  0 ••• 1 1 1
1 ••• . ••• (y+15)>>4 -950.8125 -950 FC 4A 11111100 01001010
y -15213 -15213 C4 93 11000100 10010011
y+255 -14958 C5 92 11000101 10010010
Incremented by 1 (y+255)>>8 -59.4257813 -59 FF C5 11111111 11000101

Biasing adds 1 to final result

class04.ppt – 27 – CS 213 S’01 class04.ppt – 28 – CS 213 S’01

Page 7
Properties of Unsigned Arithmetic Properties of Two’s Comp. Arithmetic
Unsigned Multiplication with Addition Forms Isomorphic Algebras
Commutative Ring • Unsigned multiplication and addition
• Addition is commutative group – Truncating to w bits
• Closed under multiplication • Two’s complement multiplication and addition
0 ≤ UMultw(u , v) ≤ 2w –1 – Truncating to w bits
• Multiplication Commutative Both Form Rings
UMultw(u , v) = UMultw(v , u) • Isomorphic to ring of integers mod 2w
• Multiplication is Associative
UMultw(t, UMultw(u , v)) = UMultw(UMultw(t, u ), v)
Comparison to Integer Arithmetic
• 1 is multiplicative identity • Both are rings
• Integers obey ordering properties, e.g.,
UMultw(u , 1) = u
• Multiplication distributes over addtion u>0 ⇒ u+v>v
UMultw(t, UAddw(u , v)) = UAddw(UMultw(t, u ), UMultw(t, v)) u > 0, v > 0 ⇒ u·v>0
• These properties are not obeyed by two’s complement arithmetic
TMax + 1 == TMin
15213 * 30426 == -10030 (16-bit words)

class04.ppt – 29 – CS 213 S’01 class04.ppt – 30 – CS 213 S’01

C Puzzle Answers
• Assume machine with 32 bit word size, two’s complement integers
• TMin makes a good counterexample in many cases

• x < 0 ⇒ ((x*2) < 0) False: TMin


• ux >= 0 True: 0 = UMin
• x & 7 == 7 ⇒ (x<<30) < 0 True: x1 = 1
• ux > -1 False: 0
• x > y ⇒ -x < -y False: -1, TMin
• x * x >= 0 False: 65535
(x*x = -131071)
• x > 0 && y > 0 ⇒ x + y > 0 False: TMax, TMax
• x >= 0 ⇒ -x <= 0 True: –TMax < 0
• x <= 0 ⇒ -x >= 0 False: TMin

class04.ppt – 31 – CS 213 S’01

Page 8

You might also like