ProgrammingVery hardTCS CodeVita · Infosys HackWithInfy

What is printed?

What is printed?
x = 255
print(x.bit_length(), bin(x).count('1'), (x & -x), (x - 1) & x)
  1. A. 8 8 255 0
  2. B. 7 8 1 254
  3. C. 8 8 1 254
  4. D. 8 7 1 254

Correct answer: C. 8 8 1 254

255 is 11111111 in binary, so bit_length is 8 and it has eight 1-bits. x & -x isolates the lowest set bit, 1. (x - 1) & x clears that lowest bit, giving 254.

More Python Programming questions

Exams that ask this topic