The bits.c file contains a skeleton for each of the 13 programming puzzles. Your assignment is to
complete each function skeleton using only straightline code for the integer puzzles (i.e., no loops or con-
ditionals) 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.
4 The Puzzles
This section describes the puzzles that you will be solving in bits.c.
Table 1 lists the puzzles in rought order of difficulty from easiest to hardest. 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.
Name Description Rating Max ops
bitXor(x,y) x || y using only & and ˜. 1 14
tmin() Smallest two’s complement integer 1 4
isTmax(x) True only if x x is largest two’s comp. integer. 1 10
allOddBits(x) True only if all odd-numbered bits in x set to 1. 2 12
negate(x) Return -x with using - operator. 2 5
isAsciDigit(x) True if 0x30 ≤ x ≤. 3 15
conditional Same as x ? y : z 3 16
isLessOrEqual(x, y) True if x ≤ y, false otherwise 3 24
logicalNeg(x)) Compute !x without using ! operator. 4 12
howManyBits(x) Min. no. of bits to represent x in two’s comp. 4 90
floatScale2(uf) Return bit-level equiv. of 2
*
f for f.p. arg. f. 4 30
floatFloat2Int(uf) Return bit-level equiv. of (int)f for f.p. arg. f. 4 30
floatPower2(x) Return bit-level equiv. of 2.0ˆx for integer x. 4 30
Table 1: Datalab puzzles. For the floating point puzzles, value f is the floating-point number having the
same bit representation as the unsigned integer uf.
For the floating-point puzzles, you will implement some common single-precision floating-point operations.
For these puzzles, 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. Instead, any floating-point operand will be passed to the function as having type
2