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](./imgs/2021-05-28-12-41-03.png)
Ideal
![Actual](./imgs/2021-05-28-12-41-16.png)
Actual
![Calculation](./imgs/2021-05-28-12-44-33.png)
Calculation
Signed Integer
![Calculation](./imgs/2021-05-28-12-45-14.png)
Calculation
![Mapping](./imgs/2021-05-28-12-45-47.png)
Mapping
![Visualization](./imgs/2021-05-28-12-46-03.png)
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](./imgs/2021-06-08-20-18-57.png)
Endian
![Endian 2](./imgs/2021-08-08-09-58-21.png)
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