| CSC 161 | Grinnell College | Spring, 2015 |
| Imperative Problem Solving and Data Structures | ||
This lab provides practice in working with data at the bit level in C. Specific work involves the representation of integers, the manipulation of bits in C, and the use of unions in C to view bit patterns in multiple ways.
The C programming language contains the following bitwise operations:
| operation | comment |
|---|---|
| & | bitwise and |
| | | bitwise or |
| ^ | bitwise exclusive or |
| ~ | ones complement |
| << | shift left |
| >> | shift right |
In the Lab on Integer Processing, we used the C program ~walker/c/fluency-book/integer-rep.c to examine the bit representations of integers. Review that program, and explain how the print_binary procedure works. As part of your explanation, include an example for the printing of the decimal number 11.
Program ~walker/c/data-rep.c provides an alternative framework for examining the bit representations of integers and floating point numbers.
Write (on paper) the floating point numbers ± 5.0 and ± 11.0 using the IEEE floating-point representation of real numbers.
Run program ~walker/c/data-rep.c to determine the internal representation of the integers from part 1, as actually stored on PC/Linux computers, and write a paragraph that explains the bits involved with the sign, mantissa, and exponent of these numbers.
Program ~walker/c/data-rep.c uses a union in C as the basis of its processing.
The main part of this program consists of a single loop.
Explain how numbers are set in options 0, F, and I.
Why is the number -1 used for option 1?
After each number is set, its value is printed using several representations. While the printf statements are straight forward, the printBitGroups function may require some thought. The first use of this function comes from the call printBitGroups (d, 1). Using bitGroups as 1, trace the execution of printBitGroups.
Explain the purpose of the call printBitGroups (d, 4), and discuss how this purpose is achieved.
Write (in English) appropriate pre- and post-condidtions for function printBitGroups. These conditions should be inserted as comments to follow the function's header, but they need NOT be checked in the code using assert statements or other executed tests.
Add a menu option to this program, so that the integer value of variable d is changed to its ones complement.
Add a menu option to this program that begins with the value in variable d and successively toggles successive bits of the variable — printing the binary, hexadecimal, integer, and float values of the results in a table. Toggling of the bits should progress from left to right. Thus, the output might have the form:
| binary | hexadecimal | integer form | float form |
| 00000000000000000000000000000000 | 00000000 | 0 | 0.0 |
| 10000000000000000000000000000000 | 80000000 | -2147483648 | -0.0 |
| 01000000000000000000000000000000 | 40000000 | 1073741824 | 2.0 |
| ... |
Development of laboratory exercises is an iterative process. Prof. Walker welcomes your feedback! Feel free to talk to him during class or stop by his office.