Lecture 002
Lab1: Lecture 2, 3, 4 (CS:APP Chapter 2)
Unsigned Integer
Summing Unsigned: w bits -> sum to w+1 bits
- However, since we only have w bits, we drop the carry bit. -> Overflow

Ideal

Actual

Calculation
Signed Integer

Calculation

Mapping

Visualization
Multiplication
Unsigned: w bits -> 2w bits
- range: [0, (w^w - 1)^2 = 2^{2w+1} + 1]
Signed: w bits -> 2w bits
- range: [-2^{2w - 2} + 2^{w - 1}, 2^{2w - 2}]
Multiplication: discard bits
- signed vs unsigned: discarded bits are different, but lower preserved bits are the same
Power of 2 Multiplication:
- u << k = u * 2^k (both signed and unsigned)
Power of 2 Division:
Negation
-x = ~x + 1 (we plus 1 because ~x + x = -1)
- This rule is good except for T_Min
Byte Ordering

Endian

Endian 2
Big Endian: Sun (Oracle SPARC), PPC Mac, Internet
- left to right ordering of byte (from small to large address)
Little Endian: x86, ARM with Android, iOS, Linux
- right to left ordering of byte (from small to large address)
Below code truncate a number in little endian
*(short *)(&x)
Exception: strings