What is printed by this C code: unsigned x = 0x0F; printf(“%u”, ~x & 0xFF);?
Correct answer D. 240
Explanation
~x flips every bit of the 32-bit unsigned value 0x0000000F, giving 0xFFFFFFF0. Masking with 0xFF keeps only the low byte, 0xF0, which is 240. Without the mask, 4294967280 would be printed. Using an unsigned operand keeps ~ well defined and avoids negative results, which is why bit manipulation is normally done on unsigned types.
Answer checked How we write and check questions
Discussion