Difficulty: Hard
Correct Answer: unsigned char arr[NUMSLOTS(50)]; SET(arr, 20); if (TEST(arr, 40)) { /* bit 40 is on */ }
Explanation:
Introduction / Context:
This question checks understanding of how to design and use bit set style macros in C for memory efficient storage of many boolean flags. Instead of using an array of int with one element per flag, a bit set packs multiple bits into each byte. The macros provided implement a small domain specific language for setting and testing bits by index. You must interpret these macros correctly and apply them to a realistic usage scenario involving 50 bits and specific bit positions.
Given Data / Assumptions:
Concept / Approach:
To hold 50 bits, the code must allocate enough bytes for indices 0 through 49. NUMSLOTS(50) uses a ceiling style calculation: (50 + 8 - 1) / 8 equals 57 / 8, which is 7 with integer division, so seven bytes are sufficient. For setting a bit, we must call SET on the array, which uses BITSLOT and MASK internally. For testing, we must call TEST and interpret a nonzero result as true. Any solution that ignores NUMSLOTS, misuses indexes directly or fails to call SET and TEST correctly will be wrong.
Step-by-Step Solution:
Step 1: Compute the number of bytes: NUMSLOTS(50) equals 7, so declare arr as unsigned char arr[NUMSLOTS(50)].
Step 2: To turn on bit 20, use SET(arr, 20). This will compute BITSLOT(20) to choose the correct byte and OR in the mask for that bit.
Step 3: To test bit 40, call TEST(arr, 40). If the bit is on, the result will be nonzero.
Step 4: Wrap the test in an if statement: if (TEST(arr, 40)) { /* bit 40 is on */ }.
Step 5: Verify that the array type, macros and indices are used consistently with the definitions in the stem.
Verification / Alternative check:
You can verify by hand for one index. For example, y equals 20 gives BITSLOT(20) equals 2, and MASK(20) equals 1 shifted by 4, so the fifth bit of arr[2] is set. For y equals 40 you get BITSLOT(40) equals 5 and MASK(40) equals 1 shifted by 0, which selects the lowest bit of arr[5]. This confirms that the macros operate as intended with the chosen declaration and calls.
Why Other Options Are Wrong:
Option B incorrectly allocates 50 bytes instead of seven and then negates the TEST result inside the if, which reverses the logical meaning. Option C uses an int array and directly assigns indices 20 and 40, ignoring the bit packing design entirely. Option D calls only MASK without actually writing to or reading from the array, so it does not set or test any bits in arr.
Common Pitfalls:
A common mistake when using such macros is to forget that BITSLOT and MASK are expressed in bytes and bit shifts, not in plain element counts. Another pitfall is to overwrite an entire byte instead of OR combining bits, which would lose previous flags. In exam questions, carefully read each macro and check that the suggested code respects both the array type and the index mapping logic.
Final Answer:
The correct usage is to declare unsigned char arr[NUMSLOTS(50)]; then call SET(arr, 20); to set bit 20 and use if (TEST(arr, 40)) { /* bit 40 is on */ } to test bit 40.
Discussion & Comments