In C programming (assuming unsigned int is 2 bytes = 16 bits), what will this program print in hexadecimal?\n\n#include<stdio.h>\n\nint main()\n{\n unsigned int a = 0xffff; // 16-bit all 1s\n ~a; // bitwise NOT result is not stored back\n printf("%x\n", a);\n return 0;\n}\n\nChoose the exact output produced by printf.

Difficulty: Easy

Correct Answer: ffff

Explanation:


Introduction / Context:
This question checks understanding of the bitwise NOT operator, expression side effects, and how assignment affects variables in C. It also relies on the assumption that unsigned int is 2 bytes (16 bits), so 0xffff represents all 1s in 16 bits.



Given Data / Assumptions:

  • unsigned int is 16 bits.
  • a is initialized to 0xffff.
  • The expression ~a is evaluated but its result is not assigned back to a.
  • printf with %x prints a in lowercase hexadecimal without leading 0x.


Concept / Approach:
The operator ~ computes the bitwise complement of its operand, but unless the result is stored (e.g., a = ~a), the original variable is unchanged. Therefore the printed value is simply the original a.



Step-by-Step Solution:
Start: a = 0xffff.Compute ~a (result would be 0x0000 for 16-bit), but do not assign.a remains 0xffff.printf("%x", a) prints ffff.



Verification / Alternative check:
If the code were a = ~a; then a would become 0x0000 and the output would be 0000. Since there is no assignment, the output stays ffff.



Why Other Options Are Wrong:
0000 / 00ff / ddfd: These assume the complement was stored or confuse the value. None applies here.None of the above: Incorrect because ffff is correct.



Common Pitfalls:
Assuming operators modify variables automatically; forgetting that expressions without assignment do not change stored values.



Final Answer:
ffff

More Questions from Bitwise Operators

Discussion & Comments

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