ProgrammingVery hardProduct company OA

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

On a two's-complement machine with C99 division, what is printed?
printf("%d %d", -13 % 8, -13 & 7);
  1. A. -5 -5
  2. B. -13 3
  3. C. -5 3
  4. D. 3 3

Correct answer: C. -5 3

% truncates toward zero, so −13 % 8 = −5 (because −13 = −1·8 − 5). & 7 keeps the low three bits of the two's-complement pattern, which gives the non-negative remainder 3. The x & 7 trick for x % 8 is valid only for non-negative x.

More C Programming questions

Exams that ask this topic