15 213 Recitation 1 21901 Edited Outline Lab
15 -213 Recitation 1 – 2/19/01 (Edited) • • Outline Lab 1 Examples Endianness Integer Representation Floating Point Shaheen Gandhi e-mail: sgandhi@andrew. cmu. edu Office Hours: Wednesday 1: 00 – 2: 30 Reminders • Lab 1: Tuesday, 11: 59 Wean Clusters
Lab 1 Examples /* * ispow 2(x) returns 1 if x is * a power of 2, 0 otherwise */ int ispow 2(int x) { return !(x & (x-1)); }
Lab 1 Examples /* * not(x) returns ~x without * explicit use of ~ */ int not(int x) { return x ^ -1; }
Lab 1 Examples /* * or_n(x, y, n) returns a result where * the least significant n bits * are x | y, and the remaining high * bits are x. */ int or_n(int x, int y, int n) { int m = -1 << n; return (x & m) | ((x | y) & ~m); }
Lab 1 Examples Some bit-twiddling hacks: http: //graphics. stanford. edu/~seander/bithacks. html
Endianess • Little Endian – Least sig. byte has highest address – Compaq, Intel, PC’s • Big Endian – Most sig. byte has highest address – IBM, Sun’s, Motorola
Endianess, con’t. Notice ordered by byte not bit. Ex. Assume variable x has a value of 0 x 7 A 3469 F 2. x starts at address 0 x 200. Big Little 7 A 34 69 F 2 69 34 7 A
Integer Representation Signed vs. Unsigned Representation Unsigned Two’s Complement B 2 U(X) = B 2 T(X) = • MSB acts as the sign bit • -x = ~x + 1
Signed vs. Unsigned, con’t. Assume here w = 8. 11 139 255 11 -117 -1
Range of Integers Unsigned • [0 … 2 w - 1] Signed • [-2 w-1 … 2 w-1 - 1] • |Tmin| = Tmax + 1 For w = 8, 255 1111 127 0111 1111 -128 1000 0000 -1 1111
Addition and Overflow • Assume x, y unsigned. – 0 <= x, y <= 2 w - 1 – 0 <= x + y <= 2 w+1 - 2 /* could require w+1 bits to represent */ – use addition modulo 2 w Consider w = 8, x = 15010 y = 15010 = 1001 01102 x + y = 30010 = 1 0010 11002 => 0010 11002 = 4410
Addition and Overflow, con’t. • Assume x, y signed. – -2 w-1 <= x, y <= 2 w-1 - 1 – -2 w <= x + y <= 2 w - 2 /* could require w+1 bits to represent */ – again just compute sum using addition modulo 2 w
Addition and Overflow, con’t. Consider w = 8, Underflow 1 Within range 4 Overflow -127
More examples Use w = 8, 0000 0111 1111 0011 -88 254 1111 1110 -1 1111 0 0000
Floating Point (Better known as “I’m going to kill the person that thought this up”) • IEEE Floating Point – – • Floating Point at a bit level: s – – – • • Standard notation Tons of features we won’t look at exp frac s – sign bit (S) exp – exponent (maps to E, has e bits) frac – significand (maps to M, has f bits) Numerical Equivalent: – 1 s M 2 E “Normalized” and “Denormalized” encoding
“Normalized” Encoding • exp 0 and exp 111… 1 – • E = exp – B – – – • B is the “Bias” Usually 2 e-1 – 1, but can be different exp: Unsigned integer value [1, 2 e – 1] M = 1. {frac} – – • If exp = 111… 1, it’s or NAN {frac} are the bits of frac is a fractional binary number Normalized Numbers have range [21 -B, 2 B+1) – And their negatives
“Denormalized” Encoding • • exp = 0 E = -B+1 M = 0. {frac} Denormalized Numbers have Range [0, 21 -B) – Na. N And their negatives -Normalized +Denorm -Denorm 0 +0 +Normalized + Na. N
Examples • 8 bit FP, 1 bit sign, 4 bit exponent, 3 bit significand, Bias of 7 Representation -> Number 0 0101 011 0 0000 101 1 1011 110 0. 34375 0. 009765625 -28. 0
Examples • 8 bit FP, 1 bit sign, 4 bit exponent, 3 bit significand, Bias of 7 Number -> Representation 9. 6 -15 0 1010 001 0 0110 001 1 1010 111
- Slides: 19