In C (unsigned int is 2 bytes = 16 bits), evaluate the effect of bitwise NOT on 32 and determine the printed hexadecimal value:
#include
int main()
{
unsigned int m = 32; // 0x0020 on 16 bits
printf("%x
", ~m);
return 0;
}
Select the exact output.
-
Affff
-
B0000
-
Cffdf
-
Dddfd
-
ENone of the above
Answer
Correct Answer: ffdf
Explanation
Introduction / Context:This problem tests bitwise operations on fixed-width integers and hexadecimal formatting with printf. Assuming unsigned int is 16 bits, 32 decimal equals 0x0020 in hex.
Given Data / Assumptions:
- unsigned int width: 16 bits.
- m = 32 → hex 0x0020.
- Bitwise NOT: ~x flips each bit within the integer width.
- printf("%x") prints lowercase hexadecimal, no 0x prefix.
Concept / Approach:On 16 bits, ~0x0020 = 0xffdf because all bits invert: the bit 5 (value 0x0020) goes from 1 to 0 and the rest from 0 to 1, producing 1111 1111 1101 1111.
Step-by-Step Solution:32 = 0x0020 (binary 0000 0000 0010 0000).~0x0020 = 1111 1111 1101 1111 = 0xffdf.printf prints ffdf.
Verification / Alternative check:Compute with the identity ~x = (2^n - 1) - x for n-bit unsigned: (2^16 - 1) - 0x0020 = 0xffff - 0x0020 = 0xffdf.
Why Other Options Are Wrong:ffff: would be ~0, not ~32.0000 / ddfd: do not match the exact bitwise inversion.
Common Pitfalls:Forgetting the assumed width; mixing signed and unsigned notions; misreading printf format.
Final Answer:ffdf