Survey
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
15-213 Recitation 1 – 2/19/01 (Edited) • • • • Outline Lab 1 Examples Endianness Integer Representation Floating Point Shaheen Gandhi e-mail: [email protected] Office Hours: Wednesday 1:00 – 2:30 Reminders • Lab 1: Tuesday, 11:59 Wean Clusters Lab 1 Examples /* * ispow2(x) returns 1 if x is * a power of 2, 0 otherwise */ int ispow2(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 0x7A3469F2. x starts at address 0x200. Big … 0x200 0x201 7A 34 0x200 Little … F2 0x201 69 0x202 69 0x202 34 0x203 F2 0x203 7A Integer Representation Signed vs. Unsigned Representation Two’s Complement Unsigned w 1 B2U(X) = xi 2 i 0 i B2T(X) = xw1 2 w 1 w 2 xi 2i i 0 • MSB acts as the sign bit • -x = ~x + 1 Signed vs. Unsigned, con’t. Assume here w = 8. Bit vector Uns. Value Signed 0x00001011 11 11 0x10001011 139 -117 255 -1 0x11111111 Range of Integers Unsigned • [0 … 2w - 1] Signed • [-2w-1 … 2w-1 - 1] • |Tmin| = Tmax + 1 Decimal For w = 8, Binary Umax 255 1111 1111 Tmax 127 0111 1111 Tmin -128 1000 0000 -1 -1 1111 1111 Addition and Overflow • Assume x,y unsigned. – 0 <= x, y <= 2w - 1 – 0 <= x + y <= 2w+1 - 2 /* could require w+1 bits to represent */ – use addition modulo 2w Consider w = 8, x = 15010 y = 15010 = 1001 01102 = 1001 01102 x + y = 30010 = 1 0010 11002 => 0010 11002 = 4410 Addition and Overflow, con’t. • Assume x,y signed. – -2w-1 <= x, y <= 2w-1 - 1 – -2w <= x + y <= 2w - 2 /* could require w+1 bits to represent */ – again just compute sum using addition modulo 2w Addition and Overflow, con’t. Consider w = 8, x Underflow -128 y x+y (x + y) mod 2w -1 -129 1 4 Within range 20 -16 4 Overflow 64 65 129 -127 More examples Use w = 8, Number Decimal Binary -- 7 0000 0111 -- -13 1111 0011 -- -88 1010 1000 Umax + Umax 254 1111 1110 Tmax + Tmin -1 1111 1111 Tmax - Tmin -1 1111 1111 Tmin + Tmin 0 0000 0000 Floating Point (Better known as “I’m going to kill the person that thought this up”) • IEEE Floating Point – – • Standard notation Tons of features we won’t look at Floating Point at a bit level: s – – – exp frac s – sign bit (S) exp – exponent (maps to E, has e bits) frac – significand (maps to M, has f bits) –1s M 2E • Numerical Equivalent: • “Normalized” and “Denormalized” encoding “Normalized” Encoding • exp 0 and exp 111…1 – • E = exp – B – – – • B is the “Bias” Usually 2e-1 – 1, but can be different exp: Unsigned integer value [1, 2e – 1] M = 1.{frac} – – • If exp = 111…1, it’s or NAN {frac} are the bits of frac frac is a fractional binary number Normalized Numbers have range [21-B, 2B+1) – And their negatives “Denormalized” Encoding • • • • exp = 0 E = -B+1 M = 0.{frac} Denormalized Numbers have Range [0, 21-B) – NaN And their negatives -Normalized +Denorm -Denorm 0 +0 +Normalized + NaN 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