**Due by 11:59pm, Friday, February 6.**

The purpose of this assignment is to become more familiar with bit-level representations of integers and floating point numbers. You'll do this by solving a series of programming "puzzles". Many of these puzzles are quite artificial, but you'll find yourself thinking much more about bits in working your way through them. This is an individual project, so each person should turn in his/her own.

The managing TA for this assignment is Maria Janczak (mjanczak@u.rochester.edu). If you run into any questions with the assignment, please email her or visit her at any of the office hours below:

- 6-8pm on Wednesday, Jan 28
- noon-2pm on Monday, Feb 2
- 6-8pm on Wednesday, Feb 4
- 6-8pm on Thursday, Feb 5
- 3-5pm on Friday, Feb 6

We are providing you with a skeleton code file and testing utilities.
To obtain these, copy the file `/u/cs252/labs15/datalab-handout.tar`

to a (protected) directory in which you plan to do your work. Then type the
command "`tar xvf datalab-handout.tar`

".
This will cause a number of files to be unpacked in the directory.
The only file you will be modifying and turning in is `bits.c`

.
All other files should be left as-is.

The `bits.c`

file contains a skeleton for each of the 15
programming puzzles. Your assignment is to complete each function
skeleton using only `straightline`

code for the integer puzzles
(i.e., no loops or conditionals) and a limited number of C arithmetic
and logical operators. Specifically, you are `only`

allowed to use
the following eight operators:

! ~ & ^ | + << >>A few of the functions further restrict this list. Also, you are not allowed to use any constants longer than 8 bits. See the comments in

`bits.c`

for detailed rules and a discussion of the desired coding
style.
The file `btest.c`

allows
you to evaluate the functional correctness of your code. The file
`README`

contains additional documentation about `btest`

.
Use the command "`make btest`

" to generate the test code and run it
with the command "`./btest`

".

This section describes the puzzles that you will be solving in `bits.c`

.

**Bit manipulations**

Function | Description | Rating | Max Ops |
---|---|---|---|

`oddBits()` |
Return word with all odd-numbered bits set to 1. | 1 | 8 |

`bitXor(x,y)` |
`(x^y)` using only ` ~ ` and ` | ` |
2 | 14 |

`swapBytes(x)` |
Swap the positions of the first and last bytes of `x` (without moving the middle two bytes). |
3 | 25 |

`rotateRight(x,n)` |
Rotate `x` to the right by `n` . |
3 | 25 |

`evenBitsCount(x)` |
Returns count of number of 1’s in the even-numbered bits of word. | 4 | 40 |

The table above describes a set of functions that manipulate and
test sets of bits. The "Rating" field gives the difficulty rating
(the number of points) for the puzzle, and the "Max ops" field gives
the maximum number of operators you are allowed to use to implement
each function. See the comments in `bits.c`

for more details on
the desired behavior of the functions. You may also refer to the test
functions in `tests.c`

. These are used as reference functions to
express the correct behavior of your functions, although they don't
satisfy the coding rules for your functions.

**Two's complement arithmetic**

Function | Description | Rating | Max Ops |
---|---|---|---|

`tmax()` |
Return maximum two’s complement integer. | 1 | 4 |

`subOK(x,y)` |
Determine if can compute `x-y` without overflow. |
3 | 20 |

`rempwr2(x,n)` |
Compute `x%(2` , for `0 <= n <= 30` . |
3 | 20 |

`satMul2(x)` |
Multiplies by 2, saturating to Tmin or Tmax if overflow. | 3 | 20 |

`isGreaterOrEqual(x,y)` |
If `x>=y` then return 1, else return 0. |
3 | 24 |

`multSevenSixteenths(x)` |
Multiplies by 7/16 rounding toward 0. | 3 | 12 |

`isNonZero(x)` |
Check whether `x` is nonzero using the legal operators except `!` . |
4 | 10 |

The table above describes a set of functions that make use of the
two's complement representation of integers. Again, refer to the
comments in `bits.c`

and the reference versions in `tests.c`

for more information.

**Floating point operations**

For this part of the assignment, you will implement some common
single-precision floating-point operations. In this section, you are
allowed to use standard control structures (conditionals, loops), and
you may use both `int`

and `unsigned`

data types, including
arbitrary unsigned and integer constants. You may not use any unions,
structs, or arrays. Most significantly, you may not use any floating
point data types, operations, or constants.
Your code should perform the bit manipulations that
implement the specified floating point operations.

Function | Description | Rating | Max Ops |
---|---|---|---|

`float_abs(uf)` |
Return bit-level equivalent of absolute value of `f` . |
2 | 10 |

`float_f2i(uf)` |
Return bit-level equivalent of expression (int) `f` . |
4 | 30 |

`float_quarter(uf)` |
Return bit-level equivalent of expression `0.25*f` . |
4 | 30 |

The table above describes a set of functions that operate on the bit-level
representations of floating-point numbers. Refer to the comments in
`bits.c`

and the reference versions in `tests.c`

for more information.

We have included some tools in the handout directory --- `btest`

,
`dlc`

, `driver.pl`

, and `fshow`

--- to help you
check the correctness of your work.

**btest**: This program checks the functional correctness of the functions in`bits.c`

. Notice that you must rebuild**btest**each time you modify your`bits.c`

file. You'll find it helpful to work through the functions one at a time, testing each one as you go. You can use the`-f`

flag to instruct`btest`

to test only a single function: "`./btest -f oddBits`

".**dlc**: This is a modified version of an ANSI C compiler. You can use it to check for compliance with the coding rules for each puzzle. You can also use it to measure the operator counts of your functions. You can run it by "`./dlc -e bits.c`

". Type "`./dlc -help`

" for a list of command line options. Don't include the`stdio.h`

header file in your`bits.c`

file, as it confuses`dlc`

and results in some non-intuitive error messages. Further, the`dlc`

program enforces a stricter form of C declarations than is the case for C++ or that is enforced by`gcc`

. In particular, any declaration must appear in a block (what you enclose in curly braces) before any statement that is not a declaration. For example, it will complain about the following code:int foo(int x) { int a = x; a *= 3; /* Statement that is not a declaration */ int b = a; /* ERROR: Declaration not allowed here */ }

**driver.pl**: This is a driver program that uses`btest`

and`dlc`

to compute the correctness and performance points for your solution. It takes no arguments by running "`./driver.pl`

". The TA will use`driver.pl`

to evaluate your solution.**fshow**: This program helps you understand the structure of floating point numbers. You can use`fshow`

to see what an arbitrary pattern represents as a floating-point number, such as "`./fshow 2080374784`

".

You are asked to turn in your solution in a single file `bits.c`

.
Please include your full name and email address at the top of `bits.c`

.
Please include any appropriate comments on your code as C comments in `bits.c`

.
There is no separate `README`

file for this assignment's turn-in.

You should electronically turn in the required files through Blackboard.

Your score will be computed out of a maximum of 78 points based on the following distribution: 43 points for correctness; 30 points for performance; 5 points for style.

*Correctness points:*The 15 puzzles you must solve have been given a difficulty rating between 1 and 4, such that their weighted sum totals to 43. We will evaluate your functions using the`btest`

program. You will get full credit for a puzzle if it passes all of the tests performed by`btest`

, and no credit otherwise.*Performance points:*Our main concern at this point in the course is that you can get the right answer. However, we want to instill in you a sense of keeping things as short and simple as you can. Furthermore, some of the puzzles can be solved by brute force, but we want you to be more clever. Thus, for each function we've established a maximum number of operators that you are allowed to use for each function. This limit is very generous and is designed only to catch egregiously inefficient solutions. You will receive two points for each correct function that satisfies the operator limit.*Style points:*Finally, we've reserved 5 points for a subjective evaluation of the style of your solutions and your commenting. Your solutions should be as clean and straightforward as possible. Your comments should be informative, but they need not be extensive.

Late turn-ins will be accepted for up to three days, with 10% penalty for each
late day.
**No turn-ins more than three-day late will be accepted.**