ProgrammingVery hardProduct company OA

What is printed?

What is printed?
x = -13
print(x >> 2, x // 4, ~x, x & 0xF, bin(x & 0xFF).count("1"), (-x) >> 2)
  1. A. -3 -4 12 13 6 3
  2. B. -4 -4 -14 3 6 3
  3. C. -4 -4 12 3 6 3
  4. D. -4 -3 12 3 4 3

Correct answer: C. -4 -4 12 3 6 3

Python ints behave like infinite two's complement. >> is a floor shift: −13 >> 2 = ⌊−3.25⌋ = −4, the same as −13 // 4. ~x = −x − 1 = 12. −13 is …1111 0011, so the low nibble is 0011 = 3 and the low byte 1111 0011 has six 1-bits. 13 >> 2 = 3. The options with −3 truncate toward zero; 13 for x & 0xF masks the magnitude instead of the two's-complement pattern.

More Python Programming questions

Exams that ask this topic