In C (unsigned int is 2 bytes = 16 bits), evaluate the effect of bitwise NOT on 32 and determine the printed hexadecimal value:\n\n#include<stdio.h>\n\nint main()\n{\n unsigned int m = 32; // 0x0020 on 16 bits\n printf("%x\n", ~m);\n return 0;\n}\n\nSelect the exact output.

Difficulty: Easy

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

More Questions from Bitwise Operators

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion