**15-213 Recitation 1 – 2/19/01** **Outline** - Lab 1 Examples
- Endianness
- Integer Representation
- Floating Point
- Type Casting
**Anusha** **e-mail:** - anusha@andrew.cmu.edu
**Office Hours:** - Tuesday 11:30 – 1:00
- Wean Clusters
**Reminders** - Lab 1: Tuesday, 11:59
**Lab 1 Examples** - /*
- * not(x) returns ~x without
- * explicit use of ~
- */
- int not(int x)
- {
- }
**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.
**Integer Representation** - Signed vs. Unsigned Representation
__Unsigned__ __Two’s Complement__ - B2U(X) = B2T(X) =
- MSB acts as the sign bit
- -x = ~x + 1
**Signed vs. Unsigned, con’t.** __Signed __ - [-2w-1 … 2w-1 - 1]
- |Tmin| = Tmax + 1
**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 = 1001 01102
- y = 15010 = 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.** **More examples** **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 – 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
- exp 0 and exp 111…1
- If exp = 111…1, it’s or NAN
**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}** - {frac} are the bits of frac
- frac is a fractional binary number
- Normalized Numbers have range [21-B, 2B+1)
**“Denormalized” Encoding** - exp = 0
**E = -B+1** **M = 0.{frac}** - Denormalized Numbers have Range [0, 21-B)
**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
- 0 1010 001
- 0 0110 001
- 1 1010 111
**Typecasting** - C has the property that some types can be converted (or typecast) to others.
- When programming in C, it is often unavoidable and essential that data is cast from one type to another.
**Typecasting (cont.)** - Syntax
- Assume x has type type_a
- Also assume y has some type that can be cast to type_a
- x = (type_a)y;
- /* cast y to type_a and assign x */
**Typecasting (cont.)** - What types can be cast?
- All primitive types in C can be cast to another, and pointers can be also be cast.
- For now, concentrate on integer/floating point casting.
**Typecasting Examples** - int a = 1024;
- short s = (short)a;
- -or-
- short s = a;
- -> S = 1024;
**Typecasting Examples** - float a = -3.5;
- int i = (int)a;
- -> i = -3
- (note round to zero behavior)
**Typecasting Examples** - int i = 4;
- double x = (double)i;
- int root = (int)sqrt(x);
- -> x = 4.0, root = 2
- Overflow
- Casting from one integer type to a smaller integer type
- int x = 0x0034a140;
- short s = x;
- -> s = 0xa140 = -24256
**Typecasting Cautions** - Sign Extension
- Casting from a negative integer to a larger integer type
- To preserve sign (and value), the sign bit is copied to every higher bit
- Example:
- char c = 0xf0;
- int i = c;
- -> i = -16, NOT 240
**Typecasting Cautions** - Floating Point
- Some large integers can not be represented exactly in floating point.
- Example:
- unsigned int i = 0xfffffff0;
- float f = (float)i;
- double d = (double)i;
- -> i = 4294967280, f = 4294967296.0,
- d = 4294967280.0
**Do'stlaringiz bilan baham:** |