ProgrammingVery hardProduct company OA

Assume 32-bit unsigned int. What is printed?

Assume 32-bit unsigned int. What is printed?
unsigned int x = 65536;
printf("%u %u", x * x, x * (x - 1) / 2);
  1. A. 0 2147450880
  2. B. 65536 2147450880
  3. C. 0 2147483648
  4. D. 0 0

Correct answer: A. 0 2147450880

Unsigned arithmetic is modulo 2^32. x * x = 2^32 wraps to 0. x * (x − 1) = 2^32 − 2^16 also wraps to 4294901760, which is below 2^32, and halving it gives 2147450880. The division happens after the wrap, so this is not the true n(n−1)/2.

More C Programming questions

Exams that ask this topic