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 (XOR) |
~ | Bitwise NOT |
<< | Shift Left |
>> | Shift Right |
A: 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. -
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 values printed and how are 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
computations in the sub-expressions inside the
printf? what do they mean?
-
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.
B: Bit Utilities
-
Complete the function
int set_bit(int word, int bit)that sets the bit at position
bitin the valuewordto1, returning the result.For example,
set_bit(50, 3)returns 58, because in binary, the number 50=32+16+2 is represented as the 32-bit value0000 0000 0000 0000 0000 0000 0011 0010
and when bit 3 is set to 1, we we have,0000 0000 0000 0000 0000 0000 0011 1010
which is equivalent to adding 8.Bonus: Note that we may have just as easily (or perhaps more easily) invoked the function with the hexadecimal literal representation of 50, which is to say 0x32, rather than the decimal representation by writing
set_bit(0x32,3). When we do so, it is often easier for the reader (and programmer) to reason more explicitly about the underlying bits. -
Complete the function
that sets the bit at positionint clear_bit(int word, int bit)bitin the valuewordto 0, returning the result. -
Complete the function
that returns the value of the bit at position bit in the value word.bool test_bit(int word, int bit) -
Complete the function
that sets the bit at position bit in the value word to its complement. (Hint: Use the XOR operator.)int toggle_bit(int word, int bit) -
Complete the function
int swap_halves(int word)that transposes the top and bottom two bytes of the (presumed) four-byte
intparameterword, returning the result.For example, if
wordhas the value0xBA5EBA77(note that each hexadecimal digit is 4 bits), then applying swap_halves should return0xBA77BA5E. Similarly given0xDEADBEEF,swap_halvesshould return0xBEEFDEAD.
For Those With Extra Time: More Bit Twiddling
-
Add a menu option to the
data-rep.cprogram, so that the integer value of variabledis 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...
