ProgrammingHardGATE CS · Product-company placement

What is printed?

What is printed?
x = 6
print(x & (x - 1), x & -x, (x ^ (x >> 1)) & 1, x.bit_length(), (-x).bit_length())
  1. A. 4 2 1 3 3
  2. B. 4 2 0 3 3
  3. C. 4 2 1 3 4
  4. D. 4 2 1 2 3

Correct answer: A. 4 2 1 3 3

x & (x - 1) clears the lowest set bit (6 to 4), and x & -x isolates it (2). bit_length uses the magnitude, so -6 also has length 3.

More Python Programming questions

Exams that ask this topic