ProgrammingHard

printf("%d", 7 & ~2); What is printed?

  1. A. 7
  2. B. 2
  3. C. 4
  4. D. 5

Correct answer: D. 5

~2 has every bit set except bit 1, so 7 & ~2 clears bit 1 of 111, leaving 101 = 5. 7 forgets that ~ inverts the bits, and 2 computes 7 & 2 instead.

More C Programming questions

Exams that ask this topic