ProgrammingHardGATE CS · Product-company placement

What is printed?

What is printed?
print(bin(-5), (-5).bit_length(), ~5, -5 >> 1, -5 & 0b111, 5 ^ -1)
  1. A. 0b11111011 3 -6 -2 3 -6
  2. B. -0b101 3 -6 -3 3 -6
  3. C. -0b101 3 -6 -2 3 -6
  4. D. -0b101 4 -5 -3 5 4

Correct answer: B. -0b101 3 -6 -3 3 -6

bin() shows negatives with a minus sign. Bitwise ops act as on infinite two's complement: ~5 = -6, -5 >> 1 floors to -3, -5 & 7 = 3 (…11011 & 111 = 011), and 5 ^ -1 = ~5 = -6.

More Python Programming questions

Exams that ask this topic