## Floating Point Data

In this lab we will be exploring the bit-level representation of floating point numbers. This will require you to correctly manipulate binary data (bits) in C and assembly. This prelab will help you practice these skills by solving the programming “puzzles” below. To make things a little more difficult you **will have to solve each puzzle by only using a certain set of bit operations provided with each exercise**. **You cannot use loops (do, while, for) or conditional statements (if, case).**

Although this lab is in C, you should re-read zyBook section 5.6 to review bitwise operations.

## Setup

The file **~cs206/Labs/Lab10/bits.c** contains all of the code you will need for this prelab. Copy it to your **local Lab10** directory and add it to **git**. You do not need to work on the mips machine for this, any Unix machine with gcc will do.

## Exercise 1: Get Byte

The **get_byte** function extracts a single byte from a 32-bit word. The function prototype looks like:

1 2 3 4 5 |
/* get_byte - Extract byte n from word x * example: get_byte(0x12345678, 1) = 0x56 * legal ops: ~ ! & ^ | + << >> */ int get_byte(int x, int n); |

And the baseline implementation is:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
/* baseline implementation of get_byte * this uses illegal ops */ int baseline_get_byte(int x, int n) { unsigned char byte; switch(n) { case 0: byte = x; break; case 1: byte = x >> 8; break; case 2: byte = x >> 16; break; default: byte = x >> 24; break; } return (int) (unsigned) byte; } |

Your goal is to implement **get_byte** so that it produces the same results as the **baseline_get_byte** function **using ONLY bit-level operations, WITHOUT using a case statement (or if statements)**. Your **get_byte** should use bit mask operations (**&** and **|**) and bit shifts (**<< **and** >>**) to produce the same result. For more on bit manipulation check wikipedia (or a textbook).

## Exercise 2: Negate

Implement the **negate** function.** You may not use the C negation operator (“-“)** or conditional statements. You must manipulate the bits in the two’s complement number directly. Think about other ways you could negate a two’s complement number. Your function should match the results of **baseline_negate**.

## Exercise 3: Is Positive

Implement the **is_positive** function. Again you must find a bitwise expression to accomplish this. **Hint: **think about the logic needed for <=0 (i.e., is_negative_or_zero) and then invert this logic. Your function should match the results given by** baseline_is_positive**.

## Exercise 4: Tmin and Tmax

Implement the **tmin** and **tmax** functions. These compute the smallest (most negative) and largest two’s-complement numbers that can be represented by **n** bits.

## Exercise 5: Floating point min and max

The maximum value that can be expressed using IEEE754 single-precision floating point format is (2-2^-23)*2^127 (approximately 3.4028235e+38). The smallest positive value is 2^-126 (approximately 1.1754944e-38). **Create the file prelab.txt** and in it **clearly describe the process** used to arrive at these numbers. Then,** derive the same values** (maximum and minimum positive) for the IEEE754 double precision floating point format.

**Submission**

Once you complete the work, add, commit, and push the file **bits.c** to your git repo.

**Grading Rubric:**

**25 points total:**

**[5 points each]**Exercise 1-4: Function implemented in C. Logic is correct and only used allowed operators.**[5 points]**Exercise 5: Clear derivation for both single and double precision floating point min and max positive numbers.

## Acknowledgement

This lab was adapted from material by Randal E. Bryant and David R. O’Hallaron (CMU) by the Bucknell University CS faculty.

## Leave a Reply