ProgrammingHard
printf("%d", 7 & ~2); What is printed?
- A. 7
- B. 2
- C. 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
- printf("%d", (1 << 4) | (1 << 2)); What is printed?
- printf("%d", 0x80 >> 7); What is printed?
- int x = 5; x ^= x << 1; printf("%d", x); What is printed?
- printf("%d", (3 & 1) + (3 | 1) + (3 ^ 1)); What is printed?
- unsigned int x = 0xF0F0F0F0u; printf("%u", x >> 28); What is printed?
- printf("%d", 20 / 6 6 + 20 % 6); What is printed?