| CSC 201 | Grinnell College | Spring, 2005 |
| Data Representation, Memory Management, and Formal Methods | ||
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 |
The text (page 53) shows the precedence of these operations together with the more familiar arithmetic and logical operations.
The first half of this lab examines the binary representation
of integers, using a program to display the representation of various integers.
More specifically, program ~walker/c/data-rep shows the
internal bit representation of numbers specified by the user. This part
of the lab asks you to run this program, focusing on the numbers and their
representation. The second part of the lab examines the program itself.
Write the integers ± 1, ± 2, ± 3, ± 6, ± 9 using sign/magnitude notation, ones complement notation, and twos complement notation.
Run program ~walker/c/data-rep to determine the internal
representation of the integers from part 1, as actually stored on PC/Linux
computers.
This part of the lab examines the program
data-rep.c, which allowed the experimentation done
above. Copy this program (~walker/c/data-rep.c) to your
account, examine the C code itself, and answer the following questions on
the program.
Review program data-rep.c carefully.
DATA type?
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.
while (1)
expression?
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.]
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.
value, mask and
iterations.
(Variable a is an array of integers, with subscripts between 0
and 31.)
mask after the first loop
terminates, and explain how that bit pattern is achieved.
a array, and how are these values
determined.
value variable is declared as
d.integer, rather than using d.integer directly
in the second loop of printBitGroups.
printBitGroups (d, 4), and
discuss how this purpose is achieved.
mask after the first loop
terminates, and explain how that bit pattern is achieved.
a array, and how are these values
determined.
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.
d is changed to its ones complement.
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 |
| ... |
This document is available on the World Wide Web as
http://www.cs.grinnell.edu/~walker/courses/201.sp05/lab-integers.shtml
|
created September 11, 2001 last revised February 3, 2005 |
|
| For more information, please contact Henry M. Walker at walker@cs.grinnell.edu. |