CS 208 s21 — Learning Block #7

Table of Contents

1 Fractional Binary Numbers

  • Exercise: what's the closest we can get to 1/3 with 6 bits?1
fraction decimal
1/2 0.5
1/4 0.25
1/8 0.125
1/16 0.0625
1/32 0.03125
1/64 0.015625
  • Exercise: What is the base 10 equivalent of the 6-bit two's complement fixed-point number 0b101.110?2

2 IEEE floating point in 6 bits

2.1 Exercise

  • With 1 sign bit, 3 exponent bits, and 2 significand bits, how close can we get to 1/33
    • using the formula \(V = (-1)^s \times M \times 2^E\)
    • where \(E\) = exp \(- Bias\) and \(Bias = 2^{k-1} - 1\) (\(k=3\) and \(Bias = 2^2 - 1 = 3\) in this case)

3 Practice

CSPP practice problems 2.47 and 2.48

4 Lab 1

  • download and extract the handout with the provided commands
  • run make test to check that everything is configured correctly, should see evaluation below
Correctness Results     Perf Results
Points  Rating  Errors  Points  Ops     Puzzle
0       6       1       0       0       sign
0       5       1       0       0       getByte
0       5       1       0       0       bitXor
0       5       1       0       0       bitAnd
0       5       1       0       0       conditional
0       5       1       0       0       logicalNeg
0       5       1       0       0       isLessOrEqual
0       5       1       0       0       floatFloat2Int

Score = 0/57 [0/41 Corr + 0/16 Perf] (0 total operators)
  • edit bits.c to implemnent each puzzle
  • demo: int sign(int x)
  • use right shift to move the most significant bit (i.e., sign bit) to the least significant position: return x >> 31;
make
./btest -f sign
Score   Rating  Errors  Function
ERROR: Test sign(2147483647[0x7fffffff]) failed...
...Gives 0[0x0]. Should be 1[0x1]
Total points: 0/6
  • need an expression that is 0x1 when x is positive
    • boolean operators are always a good bet in these kind of situations, since they result in 0x0 or 0x1
    • !!x is 0x1 when x is positive (and also when x is negative)
      • find the bitwise operator to combine x >> 31 and !!x to complete the implementation of sign

Footnotes:

1

.010101 (1/4 + 1/16 + 1/64 = 0.328125)

2

Two's complement means the most significant bit has negative weight: 0b101.110 = \(-4 + 1 + 0.5 + 0.25 = -2.25\)

3

0 001 01 (sign exponent significand) yields \(1.25 \times 2^{1 - 3} = 1.25 \times 2^{-2} = 0.3125\). This isn't as close as with a 6-bit binary fraction we could structure however we wanted, but an unstructured representation would require extra bits to locate the binary point.