Chapter 3: Representing Integers in C: 252-0061-00 V Systems Programming and Computer Architecture
Chapter 3: Representing Integers in C: 252-0061-00 V Systems Programming and Computer Architecture
01010101 {0, 2, 4, 6 }
76543210
Operator Operation Result Meaning
& Intersection 01000001 { 0, 6 }
| Union 01111101 { 2, 3, 4, 5, 6 }
^ Symmetric 00111100 { 2, 3, 4, 5 }
difference
~ Complement 10101010 { 1, 3, 5, 7 }
Systems Programming 2019 Ch. 3: Representing Integers 7
Shift operations
• Left shift: x << y Argument x 01100010
• Shift bit-vector x left y positions << 3 00010000
• Throw away extra bits on left
• Fill with 0’s on right Log. >> 2 00011000
• Right shift: x >> y Arith. >> 2 00011000
• Shift bit-vector x right y positions
• Throw away extra bits on right
• Logical shift Argument x 10100010
• Fill with 0’s on left
• Arithmetic shift << 3 00010000
• Replicate most significant bit on right
Log. >> 2 00101000
• Undefined behavior
• Shift amount < 0 or word size Arith. >> 2 11101000
Java writes
this “>>>”.
Systems Programming 2019 Ch. 3: Representing Integers 8
Summary
• Integer encoding
• Bit-level operators
• Set representation
• Logic operators
• Shift operators
• Sign bit
• For 2’s complement, most significant bit = 1 indicates negative
Systems Programming 2019 Ch. 3: Representing Integers 11
Numeric ranges
• Unsigned values • Two’s complement values
• UMin = 0 • TMin = –2w–1
• 000…0 • 100…0
• UMax = 2w – 1 • TMax = 2w–1 – 1
• 111…1 • 011…1
• Observations: • C Programming
• |TMin | = TMax + 1 • #include <limits.h>
• Asymmetric range • Declares constants, e.g.,
• UMax = 2 * TMax + 1 • ULONG_MAX
• LONG_MAX
• LONG_MIN
• Values platform specific
2’s complement 0 0
range –1
–2
TMin
void getstuff() {
char mybuf[MSIZE];
copy_from_kernel(mybuf, MSIZE);
printf(“%s\n”, mybuf);
}
Systems Programming 2019 Ch. 3: Representing Integers 19
Malicious usage
/* Kernel memory region holding user-accessible data */
#define KSIZE 1024
char kbuf[KSIZE];
void getstuff() {
char mybuf[MSIZE];
copy_from_kernel(mybuf, -MSIZE);
. . .
}
Systems Programming 2019 Ch. 3: Representing Integers 20
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: w
• X’ = xw–1 ,…, xw–1 , xw–1 , xw–2 ,…, x0 X •••
k copies of MSB
•••
X’ ••• •••
k w
Systems Programming 2019 Ch. 3: Representing Integers 21
Sign extension example
• Converting from smaller to larger integer data type
• C automatically performs sign extension for signed values
short int x = 15213;
int ix = (int) x;
short int y = -15213;
int iy = (int) y;
+ ~x 0 1 1 0 0 0 1 0
-1 1 1 1 1 1 1 1 1
• Complete proof?
Systems Programming 2019 Ch. 3: Representing Integers 25
Complement & increment examples
Decimal Hex Binary
x = 15213 x 15213 3B 6D 00111011 01101101
~x -15214 C4 92 11000100 10010010
~x+1 -15213 C4 93 11000100 10010011
y -15213 C4 93 11000100 10010011
𝑢 + 𝑣, 𝑢 + 𝑣 < 2𝑤
𝑈𝐴𝑑𝑑𝑤 𝑢, 𝑣 = ቊ
𝑢 + 𝑣 − 2𝑤 , 𝑢 + 𝑣 ≥ 2𝑤
Systems Programming 2019 Ch. 3: Representing Integers 27
Visualizing (mathematical) integer addition
• 4-bit integers u, v Add
Integer (u
4 , v)
Addition
28
20
16
14
12 12
10
8
4
v 8
6
0 4
u 0
2
4 2
6
8 0
10
12
14
• If true sum ≥ 2w
• At most once
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 2
6
u 8
10
12
0
14
will give: s == t
1 000…0 NegOver
–2w
• Wraps around 6
4
• If sum 2w–1 2
• Becomes negative 0
-2 4
6
• At most once -4
0
2
• At most once u
0
2
4
6
-8
PosOver
(Neg. overflow)
𝑢 + 𝑣 + 2𝑤 𝑢 + 𝑣 < 𝑇𝑀𝑖𝑛𝑤
𝑇𝐴𝑑𝑑𝑤 𝑢, 𝑣 = ൞ 𝑢 + 𝑣 𝑇𝑀𝑖𝑛𝑤 ≤ 𝑢 + 𝑣 ≤ 𝑇𝑀𝑎𝑥𝑤
𝑢 + 𝑣 − 2𝑤 𝑇𝑀𝑎𝑥𝑤 < 𝑢 + 𝑣 (Pos. overflow)
−𝑢 𝑢 ≠ 𝑇𝑀𝑖𝑛𝑤
𝑇𝐶𝑜𝑚𝑝𝑤 𝑢 = ቊ
𝑇𝑀𝑖𝑛𝑤 𝑢 = 𝑇𝑀𝑖𝑛𝑤
𝑈𝑀𝑢𝑙𝑡𝑤 𝑢, 𝑣 •••
Discard w bits: w bits
𝑇𝑀𝑢𝑙𝑡𝑤 𝑢, 𝑣 •••
Discard w bits: w bits
Incremented by 1
Systems Programming 2019 Ch. 3: Representing Integers 49
Compiled signed division code
C function
• Uses arithmetic shift for int int idiv8(int x)
• For Java users {
return x/8;
• Arith. shift written as >> }
Arithmetic shift