# Recitation 1 – 2/19/01

 Sana 15.12.2019 Hajmi 142.5 Kb.

## 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)
• {
• }
• 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
• Little
• 7A 34 69 F2
• F2 69 34 7A

## 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.

• Assume here w = 8.
• 11
• 11
• 139
• -117
• 255
• -1

## Range of Integers

• Unsigned
• [0 … 2w - 1]
• Signed
• [-2w-1 … 2w-1 - 1]
• |Tmin| = Tmax + 1
• For w = 8,
• 255 1111 1111
• 127 0111 1111
• -128 1000 0000
• -1 1111 1111

• Assume x,y unsigned.
• 0 <= x, y <= 2w - 1
• 0 <= x + y <= 2w+1 - 2 /* could require w+1 bits to represent */
• Consider w = 8,
• x = 15010 = 1001 01102
• y = 15010 = 1001 01102
• x + y = 30010 = 1 0010 11002 => 0010 11002 = 4410

• 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

• Consider w = 8,
• Overflow
• Within range
• Underflow
• 127
• 4
• -127

## More examples

• 0000 0111
• 1111 0011
• -88
• 254 1111 1110
• -1 1111 1111
• -1 1111 1111
• 0 0000 0000
• Use w = 8,

## 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: –1s M 2E
• “Normalized” and “Denormalized” encoding
• s
• exp
• frac

## “Normalized” 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)
• And their negatives

## “Denormalized” Encoding

• exp = 0
• E = -B+1
• M = 0.{frac}
• Denormalized Numbers have Range [0, 21-B)
• And their negatives
• NaN
• NaN
• +
• 
• 0
• +Denorm
• +Normalized
• -Denorm
• -Normalized
• +0

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

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

## Typecasting Cautions

• 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