Question
Write a C program that compiles and runs without errors or warnings. This program has a bunch of little bit-manipulation functions in it. Your function
Write a C program that compiles and runs without errors or warnings. This program has a bunch of little bit-manipulation functions in it. Your function prototypes should match the ones Igive here. Your main function should call each of your other functions. The purpose of your main function is to convince you that your other functions are working properly.
You may assume that your program is running in an environment that has 32-bit unsigneds and 64-bit unsignedlong longs, as the MS VS environment does. When we speak of the position of a bit, let's follow the convention that the position of the lowest-order bit isposition 0. The rightmost (lowest-order) bit of an unsigned is bit #0, and the leftmost (highest-order) bit of anunsigned is bit #31. Let's say that when we say bit field (12,7], we mean the collection of the following 5 bits:bit #7, bit #8, bit #9, bit #10, and bit #11 (not bit #12). The number on the right (next to the bracket) is theposition of the lowest-order bit in the bit field, while the number on the left (next to the parenthesis) is theposition of the first bit past the bit field. Bit field (5, 5] contains no bits. Bit field (6,5] contains one bit (namely,bit #5). Bit field (32.0] contains all 32 bits, starting with bit #0 and ending with bit #31.
As always, you are free to have your functions call other functions. On this assignment, you can save yourself significant time by doing this. Its smart to read through the assignment and see where you want to have your functions delegate some of their work to other functions.
0. void show( unsigned n );
Output the 32 bits of the arg, MSB first, with spaces separating the bytes, and a newline at the end.
(MSB = Most Significant Bit = highest-order bit,
LSB = Least Significant Bit = lowest-order bit)
For example, if main says
show( 0xBadDecafU );
then the output should be
10111010 11011101 11101100 10101111
1. void show64( unsigned long long nn );
Just like show, but now our arg is an unsigned long long. Recall that C (unlike C++) has no functionoverloading, so we have to give the 32-bit and 64-bit versions of the function different names.
For example, if main says
show64( 0xFeedFaceDeadBeefULL );
then the output should be
11111110 11101101 11111010 11001110 11011110 10101101 10111110 11101111
2. unsigned f0( unsigned n );
Returns the result of messing around with the bits of n in the following way:
Starting with n, turn on the bits in bit field (5,0]. (Recall that bit field (5,0] means bit #0, bit #1, bit #2, bit #3, and bit #4 (not bit #5).)
Flip the bits (10,5].
Turn off the bits (15,10].
Swap bit fields (20, 15] and (25, 20]. For instance, if bit field (20,15] held 11011 and bit field (25,20]held 01101, then after the swap bit field (20,15] would hold 01101 and bit field (25,20] would hold 11011.
Leave the bits (32, 25] the same as they were originally in n.
For example, f0( 0xBadDecafU ) returns 0xBBB6835FU:
n: 1011101 01101 11011 11010 00101 01111
f0(n): 1011101 11011 01101 00000 11010 11111
The function does no I/O, though of course main can output the returned value if it wants.
3. unsigned long long f1( unsigned long long nn );
(Similar to f0, but with 64-bit values)
Starting with nn, flip the bits in (8, 0].
Swap bit fields (16, 8] and (32, 24], leaving (24, 16] unchanged
Reset the bits in (40, 32] to 0.
Leave the bits in (56), 40] unchanged.
Set the bits in (64, 56] to 1.
For example, f1( 0xFeedFaceDeadBeefULL ) returns 0xFFEDFA00BEADDE10ULL:
nn: 11111110 11101101 11111010 11001110 11011110 10101101 10111110 11101111
f1(nn): 11111111 11101101 11111010 00000000 10111110 10101101 11011110 00010000
4. unsigned number1Bits( unsigned n );
Returns the number of 1-bits in n.
For example, number1Bits( 0xBadDecafU ) returns 22.
5. unsigned number1Bits64( unsigned long long nn );
Just like number1Bits, but this is the 64-bit version.
For example, number1Bits64( 0xFeedFaceDeadBeefULL ) returns 48.
6. unsigned number0Bits( unsigned n );
Same as number1Bits, but now were returning the number of 0-bits instead of the number of 1-bits.
For example, number0Bits( 0xBadDecafU ) returns 10.
7. unsigned number0Bits64( unsigned long long nn );
64-bit version.
For example, number0Bits64( 0xFeedFaceDeadBeefULL ) returns 16.
8. unsigned lowBitPosition( unsigned n );
Returns the position of the lowest-order 1-bit in n.
For example, lowBitPosition( 0x0F0ABCC0U ) returns 6.
If there are no 1-bits, then the function should return 32.
9. unsigned lowBitPosition64( unsigned long long nn );
64-bit version.
For example, lowBitPosition64( 0xBABE000000000000ULL ) returns 49.
For example, lowBitPosition64( 0x00000000BABE0000ULL ) returns 17.
For example, lowBitPosition64( 0x0000000000000000ULL) returns 64.
10. unsigned lowBitValue( unsigned n );
Returns the place value (contribution) of the lowest-order 1-bit in n.
For example, lowBitValue( 0x000ABC00U ) returns 1024, which is 2^10.
If there are no 1-bits, then the function should return 0.
11. unsigned long long lowBitValue64( unsigned long long nn );
64-bit version.
For example, lowBitValue64( 0x0600000000000000ULL ) returns 144115188075855872, which is 2^57.
If there are no 1-bits, then the function should return 0.
Here are the 12 prototypes, so you can just copy & paste them into your program: void show( unsigned n );
void show64( unsigned long long nn );
unsigned f0( unsigned n );
unsigned long long f1( unsigned long long nn );
unsigned number1Bits( unsigned n );
unsigned number1Bits64( unsigned long long nn );
unsigned number0Bits( unsigned n );
unsigned number0Bits64( unsigned long long nn );
unsigned lowBitPosition( unsigned n );
unsigned lowBitPosition64( unsigned long long n );
unsigned lowBitValue( unsigned n );
unsigned long long lowBitValue64( unsigned long long n );
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started