# 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

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

• 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:

• u >> k = roundDown(u / 2^k)

• (x + (0x01 << k) - 1) >> k = roundZero(x / 2 ^k)

## Negation

-x = ~x + 1 (we plus 1 because ~x + x = -1)

• This rule is good except for T_Min

## Byte Ordering

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

Table of Content