2.3 Int Arthematic
2.3 Int Arthematic
1
Carnegie Mellon
2
Carnegie Mellon
Sign Extension
Task:
▪ 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
k copies of MSB w
X •••
•••
X ••• •••
k w 3
Carnegie Mellon
4
Carnegie Mellon
Sign Extension
One point worth making is that the relative order of conversion from one
data size to another and between unsigned and signed can affect the behavior of
a program.
This shows that, when converting from short to unsigned, the program first
changes the size and then the type. That is, (unsigned) sx is equivalent to
(unsigned) (int) sx, evaluating to 4,294,954,951, not (unsigned) (unsigned
short) sx, which evaluates to 53,191.
5
Carnegie Mellon
Truncating
int x = 53191;
short sx = (short) x; /* -12345 */
int y = sx; /* -12345 */
53191 = 00000000 00000000 11001111 11000111
-12345 = 11001111 11000111
When truncating a w-bit number x = [xw−1, xw−2, . . . , x0] to a k-bit number, we
drop the high-order w − k bits
Truncating a number can alter its value—a form of overflow.
For an unsigned number x, the result of truncating it to k bits is equivalent to
computing x mod 2k
Summary:
Expanding, Truncating: Basic Rules
Expanding (e.g., short int to int)
▪ Unsigned: zeros added
▪ Signed: sign extension
▪ Both yield expected result
7
Carnegie Mellon
Practice
8
Carnegie Mellon
9
Carnegie Mellon
Unsigned Addition
Operands: w bits u •••
+v •••
True Sum: w+1 bits
u+v •••
Discard Carry: w bits UAddw(u , v) •••
u+ v u + v 2w
UAdd w (u,v) = w
u + v − 2 u + v 2w
For example, consider a 4-bit number representation with x = 9 and y = 12, having bit
representations [1001] and [1100], respectively. Their sum is 21, having a 5-bit
representation [10101]. But if we discard the high-order bit, we get [0101],
that is, decimal value 5. This matches the value 21 mod 16 = 5 10
Carnegie Mellon
11
Carnegie Mellon
True Sum 16
14
2w+1 Overflow 12
10
8
2w 6 12
14
4 10
8
2
6 v
0 0 4
Modular Sum 0
2
4
6
2
u 8
10
12
14
0
Mathematical Properties
Modular Addition Forms an Abelian Group
▪ Closed under addition
0 UAddw(u , v) 2w –1
▪ Commutative
UAddw(u , v) = UAddw(v , u)
▪ Associative
UAddw(t, UAddw(u , v)) = UAddw(UAddw(t, u ), v)
▪ 0 is additive identity
UAddw(u , 0) = u
▪ Every element has additive inverse
▪ Let UCompw (u ) = 2w – u
UAddw(u , UCompw (u )) = 0
13
Carnegie Mellon
14
Carnegie Mellon
TAdd Overflow
Functionality True Sum
▪ True sum requires w+1 0 111…1 2w–1
PosOver
bits TAdd Result
▪ Drop off MSB 0 100…0 2w –1 011…1
▪ Treat remaining bits as
2’s comp. integer 0 000…0 0 000…0
1 000…0 NegOver
–2w
15
Carnegie Mellon
16
Carnegie Mellon
Characterizing TAdd
Positive Overflow
Functionality TAdd(u , v)
▪ True sum requires w+1 bits
>0
▪ Drop off MSB v
▪ Treat remaining bits as 2’s <0
comp. integer
<0 >0
u
Negative Overflow
17
Carnegie Mellon
18
Carnegie Mellon
Practice
19
Carnegie Mellon
Multiplication
Computing Exact Product of w-bit numbers x, y
▪ Either signed or unsigned
Ranges
▪ Unsigned: 0 ≤ x * y ≤ (2w – 1) 2 = 0 and (2w − 1)2= 22w – 2w+1 + 1
▪ Two’s complement min: x * y ≥ (–2w–1)*(2w–1–1) = –22w–2 + 2w–1
▪ Two’s complement max: x * y ≤ (–2w–1) 2 = 22w–2
▪ Up to 2w bits
20
Carnegie Mellon
Unsigned Multiplication in C
u •••
Operands: w bits
* v •••
True Product: 2*w bits u · v ••• •••
UMultw(u , v) •••
Discard w bits: w bits
21
Carnegie Mellon
Signed Multiplication in C
u •••
Operands: w bits
* v •••
True Product: 2*w bits u · v ••• •••
TMultw(u , v) •••
Discard w bits: w bits
22
Carnegie Mellon
23
Carnegie Mellon
ele_src
malloc(ele_cnt * ele_size)
24
Carnegie Mellon
XDR Code
void* copy_elements(void *ele_src[], int ele_cnt, size_t ele_size) {
/*
* Allocate buffer for ele_cnt objects, each of ele_size bytes
* and copy from locations designated by ele_src
*/
void *result = malloc(ele_cnt * ele_size);
if (result == NULL)
/* malloc failed */
return NULL;
void *next = result;
int i;
for (i = 0; i < ele_cnt; i++) {
/* Copy object i to destination */
memcpy(next, ele_src[i], ele_size);
/* Move pointer to next memory region */
next += ele_size;
}
return result;
}
25
Carnegie Mellon
XDR Vulnerability
malloc(ele_cnt * ele_size)
What if:
▪ ele_cnt = 220 + 1
▪ ele_size = 4096 = 212
▪ Allocation = ??
▪ Allocation needed: 4,294,971,392
▪ Actual allocation: 4096
How can I make this function secure?
26
Carnegie Mellon
27
Carnegie Mellon
28
Carnegie Mellon
29
Carnegie Mellon
30
Carnegie Mellon
31
Carnegie Mellon
Correct division
Quotient of Negative Number by Power of 2
▪ Want x / 2k (Round Toward 0)
▪ Compute as (x+2k-1)/ 2k
▪ In C: (x + (1<<k)-1) >> k
▪ Biases dividend toward 0
32
Carnegie Mellon
33
Carnegie Mellon
Multiplication:
▪ Unsigned/signed: Normal multiplication followed by truncate,
same operation on bit level
▪ Unsigned: multiplication mod 2w
▪ Signed: modified multiplication mod 2w (result in proper range)
34
Carnegie Mellon
Left shift
▪ Unsigned/signed: multiplication by 2k
▪ Always logical shift
Right shift
▪ Unsigned: logical shift, div (division + round to zero) by 2k
▪ Signed: arithmetic shift
▪ Positive numbers: div (division + round to zero) by 2k
▪ Negative numbers: div (division + round away from zero) by 2k
Use biasing to fix
35
Carnegie Mellon
Today: Integers
Representation: unsigned and signed
Conversion, casting
Expanding, truncating
Addition, negation, multiplication, shifting
Summary
36
Carnegie Mellon
Integer C Puzzles
• x<0 ((x*2) < 0)
• ux >= 0
• x & 7 == 7 (x<<30) < 0
• ux > -1
• x>y -x < -y
• x * x >= 0
Initialization • x > 0 && y > 0 x+y>0
• x >= 0 -x <= 0
int x = foo();
• x <= 0 -x >= 0
int y = bar(); • (x|-x)>>31 == -1
unsigned ux = x; • ux >> 3 == ux/8
unsigned uy = y; • x >> 3 == x/8
• x & (x-1) != 0
37