Bitwise Operations 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 | meaning |
|---|---|
& | bitwise and |
| | bitwise or |
^ | bitwise exclusive or |
~ | bitwise not |
<< | shift left |
>> | shift right |
Binary Representation of Integers
-
In the Lab on Integer
Processing, we used the C
program
integer-rep.cto examine the bit representations of integers. Review that program and explain how theprint_binaryprocedure works. As part of your explanation, include an example for the printing of the decimal number 11. -
In the lab on Lab on
Floating-point Number Representation, we used the
program
data-rep.cto examine the bit representations of floating-point numbers. Review that program and use it to determine the internal representation of the integers from part 1, as actually stored on PC/Linux computers. -
The program
data-rep.cuses aunionin C as the basis of its processing.-
What can be stored or accessed in a
DATAtype? -
A
typedefstatement allows the typeunion DATAto be identified more simply as adatatype. Explain what types of data may be stored in variabledand how that data can be accessed.
-
What can be stored or accessed in a
-
The main part of this program consists of a single loop.
-
What is the significance of the number
1in thewhile (1)expression? -
Under what circumstances does the program terminate, and how is this made to happen?
Note:
continueis used in place ofbreakin 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.
-
What is the significance of the number
- Explain how the numbers are set wtih options 0 (zero), F, and I ("eye").
- Why is the number -1 used for option 1?
-
After each number is set, its value is printed using several
representations. While the
printfstatements are straightforward, theprintBitGroupsfunction may require some thought. The first use of this function comes from the callprintBitGroups (d, 1). UsingbitGroupsas 1, trace the execution ofprintBitGroups.-
Identify the initial values of
value,maskanditerations. (Variableais an array of integers, with subscripts between 0 and 31.) -
Describe the final value of variable
maskafter the first loop terminates and explain how that bit pattern is achieved. -
Explain what processing is done in the second loop; what are the
final values placed in the
aarray and how these values are determined. -
Why do you think the
valuevariable is declared asd.integer, rather than usingd.integerdirectly in the second loop ofprintBitGroups.
-
Identify the initial values of
-
Explain the purpose of the call
printBitGroups (d, 4), and how this purpose is achieved.- What is the purpose of the number 4 in this call?
-
Describe the final value of variable
maskafter the first loop terminates and explain how that bit pattern is achieved. -
Explain what processing is done in the second loop; what are the
final values placed in the
aarray and how are these values determined?
-
Write (in English) appropriate pre- and post-conditions 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 usingassertstatements or other executed tests.
Extra: More Bit Twiddling
-
Add a menu option to this program, so that the integer value of
variable
dis changed to its ones complement value. -
Add a menu option to this program that begins with the value in
variable
dand 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 000000000000000000000000000000000000000000.01000000000000000000000000000000080000000-2147483648-0.0010000000000000000000000000000004000000010737418242.0...
