0% found this document useful (0 votes)
35 views53 pages

Chapter 3: Representing Integers in C: 252-0061-00 V Systems Programming and Computer Architecture

The document discusses how integers are represented and manipulated in C. It covers integer encoding formats like two's complement, ranges of signed and unsigned integers, and bitwise operators and shift operations for manipulating integer values at the bit level.
Copyright
© © All Rights Reserved
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)
35 views53 pages

Chapter 3: Representing Integers in C: 252-0061-00 V Systems Programming and Computer Architecture

The document discusses how integers are represented and manipulated in C. It covers integer encoding formats like two's complement, ranges of signed and unsigned integers, and bitwise operators and shift operations for manipulating integer values at the bit level.
Copyright
© © All Rights Reserved
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/ 53

Chapter 3: Representing integers in C

252-0061-00 V Systems Programming and Computer Architecture

Systems Programming 2019 Ch. 3: Representing Integers 1


© Systems Group | Department of Computer Science | ETH Zürich
Goal:
Introduction to integer arithmetic on computers
• Recap of how numbers are represented
• Signed and unsigned values
• Integer ranges
• Integer addition and subtraction
• In C
• Mathematical properties
• Integer multiplication
• In C
• Mathematical properties
• Integer multiplication and division using shifts

Systems Programming 2019 Ch. 3: Representing Integers 2


3.1: Recap: Encodings and operators

Computer Architecture and Systems Programming

Systems Programming 2019 Ch. 3: Representing Integers 3


Representing integers
int A = 15213; Decimal: 15213
int B = -15213;
Binary: 0011 1011 0110 1101
long int C = 15213;
Hex: 3 B 6 D

A on ia32, x86-64 A on SPARC C on ia32 C on x86-64 C on SPARC


6D 00 6D 6D 00
3B 00 3B 3B 00
00 3B 00 00 3B
00 6D 00 00 6D
00
B on ia32, x86-64 B on SPARC 00
00
93 FF
00
C4 FF
FF C4
FF 93 Two’s complement representation
Systems Programming 2019 Ch. 3: Representing Integers 4
Bit-level operations in C
• Operations &, |, ~, ^ available in C
• Apply to any “integral” data type
• long, int, short, char, unsigned &, |, ~, ^
• View arguments as bit vectors
• Arguments applied bit-wise

• Examples (char data type):


• ~0x41 → 0xBE
~010000012 101111102
• ~0x00 → 0xFF
~000000002 111111112
• 0x69 & 0x55 → 0x41
011010012 010101012 010000012
• 0x69 | 0x55 → 0x7D
011010012 010101012 011111012

Systems Programming 2019 Ch. 3: Representing Integers 5


Contrast: Logic operations in C
• &&, ||, !
• View 0 as “False” &&, ||, !
• Anything nonzero as “True”
• Always return 0 or 1
• Early termination

• Examples (char data type)


• !0x41 → 0x00
• !0x00 → 0x01
• !!0x41 → 0x01

• 0x69 && 0x55 → 0x01


• 0x69 || 0x55 → 0x01

Systems Programming 2019 Ch. 3: Representing Integers 6


Representing & manipulating sets
• Width w bit vector represents subsets of {0, …, w–1}
• aj = 1 if j  A:
01101001 {0, 3, 5, 6 }
76543210

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

Systems Programming 2019 Ch. 3: Representing Integers 9


3.2: Integer ranges

Computer Architecture and Systems Programming

Systems Programming 2019 Ch. 3: Representing Integers 10


Encoding integers
Unsigned Two’s complement
𝑤−1 𝑤−2

𝐵2𝑈 𝑋 = ෍ 𝑥𝑖 ∙ 2𝑖 𝐵2𝑇 𝑋 = −𝑥𝑤−1 ∙ 2𝑤−1 + ෍ 𝑥𝑖 ∙ 2𝑖


𝑖=0 𝑖=0

short int x = 15213; Sign


short int y = -15213; Bit

• A C short is 2 bytes long:


Decimal Hex Binary
x 15213 3B 6D 00111011 01101101
y -15213 C4 93 11000100 10010011

• 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

Decimal Hex Binary


UMax 65535 FF FF 11111111 11111111
Values
TMax 32767 7F FF 01111111 11111111
for
w=16 TMin -32768 80 00 10000000 00000000
-1 -1 FF FF 11111111 11111111
0 0 00 00 00000000 00000000

Systems Programming 2019 Ch. 3: Representing Integers 12


Values for different word sizes
8 16 32 64
UMax 255 65,535 4,294,967,295 18,446,744,073,709,551,615
TMax 127 32,767 2,147,483,647 9,223,372,036,854,775,807
TMin -128 -32,768 -2,147,483,648 -9,223,372,036,854,775,808

• 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

Systems Programming 2019 Ch. 3: Representing Integers 13


Mapping signed  unsigned
Bits Signed Unsigned
0000 0 0
0001 1 1
0010 2 2
0011 3 = 3
0100 4 4
0101 5 5
0110 6 6
0111 7 7
1000 -8 8
1001 -7 9
1010 -6 +16 10
1011 -5 11
1100 -4 12
1101 -3 13
1110 -2 14
Systems Programming 2019 Ch. 3: Representing Integers 14
1111 -1 15
Conversion visualized UMax
UMax – 1

• 2’s Comp.  Unsigned


TMax + 1 Unsigned
• Ordering inversion TMax TMax range
• Negative  big positive

2’s complement 0 0
range –1
–2

TMin

Systems Programming 2019 Ch. 3: Representing Integers 15


Signed vs. unsigned in C
• Constants
• By default are considered to be signed integers
• Unsigned if have “U” as suffix:
0U
4294967259U
• Casting
• Explicit casting between signed & unsigned same as U2T and T2U:
int tx, ty;
unsigned ux, uy;
tx = (int) ux;
uy = (unsigned) ty;
• Implicit casting also occurs via assignments and procedure calls
tx = ux;
uy = ty;

Systems Programming 2019 Ch. 3: Representing Integers 16


Casting surprises in expression evaluation
• When mixing unsigned and
Constant 1 Constant 2 Relation Evaluation
signed in single expression,
0 0U == Unsigned
signed values implicitly cast to -1 0 < Signed
unsigned -1 0U > Unsigned
• Including comparison operators! 2147483647 -2147483647-1 > Signed
<, >, ==, <=, >= 2147483647U -2147483647-1 < Unsigned

• Examples for W = 32: -1 -2 > Signed


(unsigned)-1 -2 > Unsigned
• TMIN = -2,147,483,648 , 2147483647 2147483648U < Unsigned
• TMAX = 2,147,483,647 2147483647 (int) 2147483648U > signed

Systems Programming 2019 Ch. 3: Representing Integers 17


Code security example from way back (but…)
/* Kernel memory region holding user-accessible data */
#define KSIZE 1024
char kbuf[KSIZE];

/* Copy at most maxlen bytes from kernel region to user buffer */


int copy_from_kernel(void *user_dest, int maxlen) {
/* Byte count len is minimum of buffer size and maxlen */
int len = KSIZE < maxlen ? KSIZE : maxlen;
memcpy(user_dest, kbuf, len);
return len;
}

• Similar to code found in FreeBSD’s implementation of


getpeername
• There are legions of smart people trying to find
vulnerabilities in programs…
Systems Programming 2019 Ch. 3: Representing Integers 18
Typical usage…
/* Kernel memory region holding user-accessible data */
#define KSIZE 1024
char kbuf[KSIZE];

/* Copy at most maxlen bytes from kernel region to user buffer */


int copy_from_kernel(void *user_dest, int maxlen) {
/* Byte count len is minimum of buffer size and maxlen */
int len = KSIZE < maxlen ? KSIZE : maxlen;
memcpy(user_dest, kbuf, len);
return len;
}

#define MSIZE 528

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];

/* Copy at most maxlen bytes from kernel region to user buffer */


int copy_from_kernel(void *user_dest, int maxlen) {
/* Byte count len is minimum of buffer size and maxlen */
int len = KSIZE < maxlen ? KSIZE : maxlen;
memcpy(user_dest, kbuf, len);
return len;
} /* Declaration of library function memcpy */
void *memcpy(void *dest, void *src, size_t n);

#define MSIZE 528

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;

Decimal Hex Binary


x 15213 3B 6D 00111011 01101101
ix 15213 00 00 3B 6D 00000000 00000000 00111011 01101101
y -15213 C4 93 11000100 10010011
iy -15213 FF FF C4 93 11111111 11111111 11000100 10010011

Systems Programming 2019 Ch. 3: Representing Integers 22


Summary
• Casting signed ↔ unsigned
• Bit pattern is maintained but reinterpreted
• Can have unexpected effects: adding or subtracting 2w-1
• Expression containing signed and unsigned int
• int is cast to unsigned!
• Expanding (e.g., short int → int)
• Unsigned: zeros added
• Signed: sign extension
• Truncating (e.g., unsigned → unsigned short)
• Unsigned: modulus operation
• Signed: similar to modulus
• For small numbers yields expected behaviour

Systems Programming 2019 Ch. 3: Representing Integers 23


3.3: Integer addition and subtraction in C

Computer Architecture and Systems Programming

Systems Programming 2019 Ch. 3: Representing Integers 24


Negation: complement & increment
• Recall the following holds for 2’s complement:
~x + 1 == -x
• Complement
Observation: ~x + x == 1111…111 == -1
x 1 0 0 1 1 1 0 1

+ ~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

Decimal Hex Binary


x=0
0 0 00 00 00000000 00000000
~0 -1 FF FF 11111111 11111111
~0+1 0 00 00 00000000 00000000

Systems Programming 2019 Ch. 3: Representing Integers 26


Unsigned addition
Operands: w bits 𝑢 •••
+𝑣 •••
True sum: w+1 bits
𝑢+𝑣 •••

Discard carry: w bits 𝑈𝐴𝑑𝑑𝑤 𝑢, 𝑣 •••

• Standard addition function


• Ignores carry output
• Implements modular arithmetic
s = 𝑈𝐴𝑑𝑑𝑤 𝑢, 𝑣 = 𝑢 + 𝑣 mod 2𝑤

𝑢 + 𝑣, 𝑢 + 𝑣 < 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

• Compute true sum Add4(u , v)


• Values increase linearly
with u and v 32

28

• Forms planar surface 24

20

16
14
12 12
10
8
4
v 8
6
0 4
u 0
2
4 2
6
8 0
10
12
14

Systems Programming 2019 Ch. 3: Representing Integers 28


Visualizing unsigned addition
Overflow

• Wraps around UAdd4(u , v)

• 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

Systems Programming 2019 Ch. 3: Representing Integers 29


Mathematical properties
• Modular addition forms an Abelian group
• Closed under addition
0 ≤ 𝑈𝐴𝑑𝑑𝑤 𝑢, 𝑣 ≤ 2𝑤 − 1
• Commutative
𝑈𝐴𝑑𝑑𝑤 𝑢, 𝑣 = 𝑈𝐴𝑑𝑑𝑤 𝑣, 𝑢
• Associative
𝑈𝐴𝑑𝑑𝑤 𝑡, 𝑈𝐴𝑑𝑑𝑤 𝑢, 𝑣 = 𝑈𝐴𝑑𝑑𝑤 𝑈𝐴𝑑𝑑𝑤 𝑡, 𝑢 , 𝑣
• 0 is additive identity
𝑈𝐴𝑑𝑑𝑤 𝑢, 0 = 𝑢
• Every element has additive inverse
Let: 𝑈𝐶𝑜𝑚𝑝𝑤 𝑢 = 2𝑤 − 𝑢
Then: 𝑈𝐴𝑑𝑑𝑤 𝑢, 𝑈𝐶𝑜𝑚𝑝𝑤 𝑢 =0

Systems Programming 2019 Ch. 3: Representing Integers 30


Two’s complement addition
Operands: w bits 𝑢 •••
+𝑣 •••
True sum: w+1 bits
𝑢+𝑣 •••

Discard carry: w bits 𝑇𝐴𝑑𝑑𝑤 𝑢, 𝑣 •••

• TAdd and UAdd have identical bit-level behavior


– Signed vs. unsigned addition in C:
int s, t, u, v;
s = (int) ((unsigned) u + (unsigned) v);
t = u + v;

will give: s == t

Systems Programming 2019 Ch. 3: Representing Integers 31


TAdd overflow
True Sum
• Functionality 0 111…1 2w–1
• True sum requires w+1 bits PosOver TAdd Result
• Drop off MSB 0 100…0 2w –1 011…1

• Treat remaining bits as


0 000…0
2’s comp. integer 0 000…0

1 011…1 –2w –1–1 100…0

1 000…0 NegOver
–2w

Systems Programming 2019 Ch. 3: Representing Integers 32


Visualizing 2’s complement addition
NegOver
TAdd4(u , v)
• Values
• 4-bit two’s comp.
• Range from -8 to +7 8

• Wraps around 6
4

• If sum  2w–1 2

• Becomes negative 0
-2 4
6

• At most once -4
0
2

• If sum < –2w–1 -6


-2
-8 -4 v
• Becomes positive -8
-6
-4
-2
-6

• At most once u
0
2
4
6
-8
PosOver

Systems Programming 2019 Ch. 3: Representing Integers 33


Characterizing TAdd
Positive Overflow
TAdd(u , v)
• True sum requires w+1 bits
>0
• Drop off MSB v
• Treat remaining bits <0

as 2’s comp. integer <0 >0


u
Negative Overflow

(Neg. overflow)
𝑢 + 𝑣 + 2𝑤 𝑢 + 𝑣 < 𝑇𝑀𝑖𝑛𝑤
𝑇𝐴𝑑𝑑𝑤 𝑢, 𝑣 = ൞ 𝑢 + 𝑣 𝑇𝑀𝑖𝑛𝑤 ≤ 𝑢 + 𝑣 ≤ 𝑇𝑀𝑎𝑥𝑤
𝑢 + 𝑣 − 2𝑤 𝑇𝑀𝑎𝑥𝑤 < 𝑢 + 𝑣 (Pos. overflow)

Systems Programming 2019 Ch. 3: Representing Integers 34


Mathematical properties of TAdd
• Group isomorphic to unsigneds with UAdd
• Since both have identical bit patterns

𝑇𝐴𝑑𝑑𝑤 𝑢, 𝑣 = 𝑈2𝑇 𝑈𝐴𝑑𝑑𝑤 𝑇2𝑈 𝑢 , 𝑇2𝑈 𝑣

• 2’s complement under TAdd forms a group


• Closed, commutative, associative,
• 0 is additive identity
• Every element has additive inverse

−𝑢 𝑢 ≠ 𝑇𝑀𝑖𝑛𝑤
𝑇𝐶𝑜𝑚𝑝𝑤 𝑢 = ቊ
𝑇𝑀𝑖𝑛𝑤 𝑢 = 𝑇𝑀𝑖𝑛𝑤

Systems Programming 2019 Ch. 3: Representing Integers 35


3.4: Integer multiplication in C

Computer Architecture and Systems Programming

Systems Programming 2019 Ch. 3: Representing Integers 36


Multiplication
• Computing exact product of w-bit numbers x, y
• Either signed or unsigned
• Ranges
• Unsigned (up to 2𝑤 bits):
0 ≤ 𝑥 ∗ 𝑦 ≤ 2𝑤 − 1 2 = 22𝑤 − 2𝑤+1 + 1
• Two’s complement min (up to 2𝑤−1 bits):
𝑥 ∗ 𝑦 ≥ −2𝑤−1 ∗ 2𝑤−1 − 1 = −22𝑤−2 + 2𝑤−1
• Two’s complement max (up to 2𝑤 bits, but only for 𝑇𝑀𝑖𝑛𝑤 2 ):
𝑥 ∗ 𝑦 ≤ −2𝑤−1 2 = 22𝑤−2
• Maintaining exact results
• Would need to keep expanding word size with each product computed
• Done in software by “arbitrary precision” arithmetic packages

Systems Programming 2019 Ch. 3: Representing Integers 37


Unsigned multiplication in C
𝑢 •••
Operands: w bits
∗𝑣 •••

True product: 2*w bits 𝑢 ∙ 𝑣 ••• •••

𝑈𝑀𝑢𝑙𝑡𝑤 𝑢, 𝑣 •••
Discard w bits: w bits

• Standard multiplication function


• Ignores high order w bits
• Implements modular arithmetic
𝑈𝑀𝑢𝑙𝑡𝑤 𝑢, 𝑣 = 𝑢 ∙ 𝑣 mod 2𝑤

Systems Programming 2019 Ch. 3: Representing Integers 38


Unsigned multiplication with addition forms a
commutative ring:
• Addition is a commutative group
• Closed under multiplication
0 ≤ 𝑈𝑀𝑢𝑙𝑡𝑤 𝑢, 𝑣 ≤ 2𝑤 − 1
• Multiplication is commutative
𝑈𝑀𝑢𝑙𝑡𝑤 𝑢, 𝑣 = 𝑈𝑀𝑢𝑙𝑡𝑤 𝑣, 𝑢
• Multiplication is associative
𝑈𝑀𝑢𝑙𝑡𝑤 𝑡, 𝑈𝑀𝑢𝑙𝑡𝑤 𝑢, 𝑣 = 𝑈𝑀𝑢𝑙𝑡𝑤 𝑈𝑀𝑢𝑙𝑡𝑤 𝑡, 𝑢 , 𝑣
• 1 is multiplicative identity
𝑈𝑀𝑢𝑙𝑡𝑤 𝑢, 1 = 𝑢
• Multiplication distributes over addition
𝑈𝑀𝑢𝑙𝑡𝑤 𝑡, 𝑈𝐴𝑑𝑑𝑤 𝑢, 𝑣 = 𝑈𝐴𝑑𝑑𝑤 𝑈𝑀𝑢𝑙𝑡𝑤 𝑡, 𝑢 , 𝑈𝑀𝑢𝑙𝑡𝑤 𝑡, 𝑣

Systems Programming 2019 Ch. 3: Representing Integers 39


Signed multiplication in C
𝑢 •••
Operands: w bits
∗𝑣 •••

True product: 2*w bits 𝑢 ∙ 𝑣 ••• •••

𝑇𝑀𝑢𝑙𝑡𝑤 𝑢, 𝑣 •••
Discard w bits: w bits

• Standard multiplication function


• Ignores high order w bits
• Some of which are different for signed vs. unsigned multiplication
• Lower bits are the same

Systems Programming 2019 Ch. 3: Representing Integers 40


Signed multiplication
• Isomorphic algebra to unsigned multiplication and addition
• Both isomorphic to ring of integers mod 2w

• Comparison to (mathematical) integer arithmetic


• Both are rings
• True integers obey ordering properties, e.g.,
𝑢 >0⇒𝑢+𝑣 >𝑣
𝑢 > 0, 𝑣 > 0 ⇒ 𝑢 ∙ 𝑣 > 0
• Not the case for two’s complement arithmetic:
TMax + 1 == TMin
15213 * 30426 == -10030 (e.g. for 16-bit words)

Systems Programming 2019 Ch. 3: Representing Integers 41


3.5: Integer multiplication and division using shifts

Computer Architecture and Systems Programming

Systems Programming 2019 Ch. 3: Representing Integers 42


Power-of-2 multiply with shift
• Operation
• u << k gives u * 2k
k
• Both signed and unsigned
u •••
Operands: w bits
* 2k 0 ••• 0 1 0 ••• 0 0

True product: w+k bits u · 2k ••• 0 ••• 0 0


Discard k bits: w bits 𝑈𝑀𝑢𝑙𝑡𝑤 𝑢, 2𝑘 ••• 0 ••• 0 0
𝑇𝑀𝑢𝑙𝑡𝑤 𝑢, 2𝑘
• Examples
• u << 3 == u*8
• (u << 5) – (u << 3) == u * 24
• Most machines shift and add faster than multiply
• Compiler generates this code automatically
Systems Programming 2019 Ch. 3: Representing Integers 43
Compiled multiplication code
C Function
int mul12(int x)
{
return x*12;
}

Compiled arithmetic operations Explanation


leal (%eax,%eax,2), %eax t <- x+x*2
sall $2, %eax return t << 2;

• C compiler automatically generates shift/add code when multiplying by


constant
Systems Programming 2019 Ch. 3: Representing Integers 44
Unsigned power-of-2 divide with shift
• Quotient of unsigned by power of 2
• u >> k gives  u / 2k  k
• Uses logical shift u ••• ••• Binary Point
Operands:
/ 2k 0 ••• 0 1 0 ••• 0 0
Division: u / 2k 0 ••• 0 0 ••• . •••

Result:  u / 2k  0 ••• 0 0 •••

Division Computed Hex Binary


u 15213 15213 3B 6D 00111011 01101101
u >> 1 7606.5 7606 1D B6 00011101 10110110
u >> 4 950.8125 950 03 B6 00000011 10110110
u >> 8 59.4257813 59 00 3B 00000000 00111011

Systems Programming 2019 Ch. 3: Representing Integers 45


Compiled unsigned division code
C Function
unsigned udiv8(unsigned x)
{
return x/8;
}

Compiled Arithmetic Operations Explanation


shrl $3, %eax # Logical shift
return x >> 3;

• Uses logical shift for unsigned


• For Java users: logical shift written as >>>
Systems Programming 2019 Ch. 3: Representing Integers 46
Signed power-of-2 divide w/ shift

• Quotient of Signed by Power of 2


k
• s >> k gives  s / 2k  Binary Point
𝑠 ••• •••
• Uses arithmetic shift Operands:
Τ2𝑘 0 ••• 0 1 0 ••• 0 0
• Rounds wrong 𝑠ൗ
direction when Division: 2𝑘 ••• ••• . •••
s < 0.
Result: 𝑅𝑜𝑢𝑛𝑑𝐷𝑜𝑤𝑛 𝑠ൗ 𝑘 ••• •••
2

Division Computed Hex Binary


s -15213 -15213 C4 93 11000100 10010011
s >> 1 -7606.5 -7607 E2 49 11100010 01001001
s >> 4 -950.8125 -951 FC 49 11111100 01001001
s >> 8 -59.4257813 -60
Systems Programming 2019 FF C4 Integers11111111 11000100
Ch. 3: Representing 47
Correct power-of-2 divide
• Quotient of negative number by power of 2
• We want sΤ2𝑘 (round toward 0)
• We compute it as s + 2𝑘 − 1 Τ2𝑘
• In C: (s + (1<<k)-1) >> k
• Biases the dividend toward 0
k
s 1 ••• 0 ••• 0 0
• Case 1: No rounding Dividend:
+2𝑘 − 1 0 ••• 0 0 1 ••• 1 1
Biasing has no effect
1 ••• 1 ••• 1 1 Binary Point
Divisor: Τ2𝑘 0 ••• 0 1 0 ••• 0 0
𝑠Τ2𝑘 1 ••• 1 1 1 ••• . 1 ••• 1 1

Systems Programming 2019 Ch. 3: Representing Integers 48


Correct power-of-2 divide (Cont.)
• Case 2: Rounding:
k
Dividend: s 1 ••• •••
+2𝑘 − 1 0 ••• 0 0 1 ••• 1 1
1 ••• •••
Biasing adds 1 to final result
Incremented by 1 Binary Point

Divisor: Τ2𝑘 0 ••• 0 1 0 ••• 0 0


𝑠Τ2𝑘 1 ••• 1 1 1 ••• . •••

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

Compiled arithmetic operations


testl %eax, %eax
js L4
Explanation L3:
sarl $3, %eax
if x < 0 ret
x += 7; L4:
# Arithmetic shift addl $7, %eax
return x >> 3; jmp L3

Systems Programming 2019 Ch. 3: Representing Integers 50


Summary
• Signed/unsigned multiply:
x * 2k = x << k
• Unsigned divide:
Logical
u / 2k = u >> k shift
• Signed divide:
s / 2k = s >> k for s > 0
s / 2k = s + (2k – 1) >> k for s < 0

Arithmetic shift

Systems Programming 2019 Ch. 3: Representing Integers 51


3.6: C Integer puzzles

Computer Architecture and Systems Programming

Systems Programming 2019 Ch. 3: Representing Integers 52


Integer C Puzzles
• x < 0  ((x*2) < 0)
• Assume 32-bit word size, two’s • ux >= 0
complement integers • x & 7 == 7  (x<<30) < 0
• For each of the following C • ux > -1
expressions, either: • x > y  -x < -y
• Argue that it is true for all argument • x * x >= 0
values • x>0 && y>0  x + y > 0
• Give an example where it is not true • x >= 0  -x <= 0
Initialization
• x <= 0  -x >= 0
int x = foo();
• (x|-x)>>31 == -1
int y = bar(); • ux >> 3 == ux/8
unsigned ux = x; • x >> 3 == x/8
unsigned uy = y; • x & (x-1) != 0

Systems Programming 2019 Ch. 3: Representing Integers 53

You might also like