Transcript ppt
15-213 Recitation 1 – 2/19/01
•
•
•
•
•
Outline
Lab 1 Examples
Endianness
Integer Representation
Floating Point
Type Casting
Reminders
• Lab 1: Tuesday, 11:59
Trent
e-mail:
[email protected]
Office Hours:
Thursday 12:30 – 2:00
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 lowest 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
00001011
10001011
11111111
Uns. Value Signed
11
11
139
-117
255
-1
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
-1
-129
Within range
20
-16
4
Overflow
64
65
129
(x + y) mod 2w
127
4
-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 000
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;
-orshort 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