CSC 161 Grinnell College Spring, 2010 Imperative Problem Solving and Data Structures

# Machine-level Operations, Bit Manipulation, and Unions

## Goals

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.

## Bit Operations

The C programming language contains the following bitwise operations:

operation   comment
& bitwise and
| bitwise or
^ bitwise exclusive or
~ ones complement
<< shift left
>> shift right
1. 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.

## Binary Representation of Integers

1. Program ~walker/c/data-rep.c provides an alternative framework for examining the bit representations of integers and floating point numbers.

1. Write (on paper) the floating point numbers ± 5.0 and ± 11.0 using the IEEE floating-point representation of real numbers.

2. 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.

2. Program ~walker/c/data-rep.c uses a union in C as the basis of its processing.

1. What can be stored or accessed in a `DATA` type?
2. A `typedef` statement allows the type `union DATA` to be identified more simply as a `data` type. Explain what data may be stored in variable `d` and how that data can be accessed.

3. The main part of this program consists of a single loop.
1. What is the significance of the number 1 in the `while (1)` expression?
2. Under what circumstances does the program terminate, and how is this made to happen?
3. [Note that `continue` is used in place of `break` in the default option, so execution at that spot will jump back to the top of the loop rather than continuing with the printing that follows.]

4. Explain how numbers are set in options 0, F, and I.

5. Why is the number -1 used for option 1?

6. 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`.
1. Identify the initial values of `value, mask` and `iterations`. (Variable `a` is an array of integers, with subscripts between 0 and 31.)
2. Describe the final value of variable `mask` after the first loop terminates, and explain how that bit pattern is achieved.
3. Explain what processing is done in the second loop; what are the final values placed in the `a` array, and how are these values determined.
4. Why do you think the `value` variable is declared as `d.integer`, rather than using `d.integer` directly in the second loop of `printBitGroups`.

7. Explain the purpose of the call `printBitGroups (d, 4)`, and discuss how this purpose is achieved.
1. What is the purpose of the number 4 in this call?
2. Describe the final value of variable `mask` after the first loop terminates, and explain how that bit pattern is achieved.
3. Explain what processing is done in the second loop; what are the final values placed in the `a` array, and how are these values determined.

8. 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.

9. Add a menu option to this program, so that the integer value of variable `d` is changed to its ones complement.

10. 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 ...

1. Write functions to solve ONE of the following problems from King's book:

1. Exercise 20.7 (page 525),
2. Exercise 20.9 (page 526; for part b, think recursively)
3. Exercise 20.10 (page 526).

Additional problems may be done for extra credit.

## Work to turn in

• Commentary for steps 1-9, with no commentary needed for 4c.
• A program listing, with appropriate test runs and comments on correctness, for steps 10 and 11.
• A program, with appropriate test runs and comments on correctness for step 12.

This document is available on the World Wide Web as

```     http://www.cs.grinnell.edu/~walker/courses/161.sp10/labs/lab-bit-ops.shtml
```