ProgrammingHardAccenture · Product company OA

On a two's-complement machine, what is printed?

On a two's-complement machine, what is printed?
int a = 5;
printf("%d %d %d", !a, ~a & 0xF, !!a + !~-1);
  1. A. 0 5 2
  2. B. 0 10 1
  3. C. 0 10 2
  4. D. 1 10 2

Correct answer: C. 0 10 2

!5 is 0. ~5 flips all bits (…1010), and & 0xF keeps 1010 = 10. !!a normalises 5 to 1; ~-1 is 0 (all ones flipped), so !~-1 is 1, and the sum is 2. Logical ! gives only 0 or 1; bitwise ~ flips every bit.

More C Programming questions

Exams that ask this topic