15513(1) Data Representation

文章目录
  1. 1. Shift Operations
  2. 2. Expression evalutaion
    1. 2.1. Mix of unsigned/signed
    2. 2.2. Tricky thing about unsigned int
  3. 3. Floating point representation
    1. 3.1. Normalized values
    2. 3.2. Denormalized values
    3. 3.3. Special values

Since this blog, I’ll turn to writing in English:) I’m now pursuing my ms degree in CMU now in mountain view. This series of blogs will act as the memo of my 15513, introduction to computer system course. Since I’ve already take the first part of the book in my undergraduates, I’ll only briefly write down those I’m still confused with as separate bullet points.

The 15513 course this semester is taught by Randal E. Bryant and David R.O’Hallaron again! That’s why I choose to be in it again!:) It’s great.

The first part of the book is about data representations. It covers the topics of how integer/float number are stored in computers following the IEEE rule. Here it goes:

Shift Operations

Left shift: logical shift is the same as arithmetic shift

Right shift:

  • Logical shift: Fill 0s on the left
  • Arithmetic shift: Replicate most signification bit on left

I used to be confused with that. Actually, arithmetic means maintain the value that x>>n == x/(2**n), while logical is pure right shift. And thus, they behave the same in left shifts.

Expression evalutaion

Mix of unsigned/signed

  • If there is a mix of unsigned and signed in single expression, signed values implicitly cast to unsigned

Puzzles:

Constant1 Canstant2 Relation
0 0u ==
-1 0 <
-1 0u >
2147483647 -2147483647-1 >
2147483647U -2147483647-1 <
-1 -2 >
(unsigned)-1 -2 >
2147483647 2147483648U <
2147483647 (int)2147483648U >

Tricky thing about unsigned int

Here is piese of code that makes mistakes.

c
1
2
3
unsigned i;
for(i = cnt-2; i >=0; i--)
a[i] += a[i+1]

Since C standard ensures that unsigned value will never be under 0, this loop will be endless. That is 0U - 1 tmax. As a result, we should use something like this.

Floating point representation

  • Numerical from

$$
v = (-1)^S M 2^E
$$

where s, M, E stand for sign, frac field and expression respectively.

  • For 32 bits. it goes (1 + 9 + 23)
  • For 64 bits, it goes (1 + 15 + 52)

Normalized values

  • exp is not 000..000 or 111…111
  • E = exp - bias(127/1023)
  • M = 1.xxxx…x, we get an extra leading bit in this form

Denormalized values

  • exp = 000.0
  • E = 1-bias (not 0-bias, to be consistent with the normalized view)
  • M = 0.xxx…xx
  • exp = 0, frac = 0 then represent 0 value
  • exp = 0, frac != 0 then represent numbers close to 0

Special values

  • exp = 11…11
  • if frac = 0 represent infinity, where 0,1 of the sign bit controls positive/negative infinity
  • if frac != 0 represent NaN. Since no numerical values can be determined.

from slides