Practice mode C Language

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.

Report a problem

Answer checked How we write and check questions

Tip: press A, B, C or D to answer, and N for the next question.

Advertisement

Have a question about this answer?

Your email address will not be published. Required fields are marked *

Links and website addresses are not allowed in comments.