ProgrammingVery hardJEE Main · CAT placement

What is printed?

What is printed?
n = 13
print(n ^ (n >> 1), [bin(i ^ (i >> 1)) for i in range(4)])
  1. A. 13 ['0b0', '0b1', '0b11', '0b10']
  2. B. 11 ['0b0', '0b1', '0b10', '0b11']
  3. C. 11 ['0b0', '0b1', '0b11', '0b10']
  4. D. 11 ['0b1', '0b1', '0b11', '0b10']

Correct answer: C. 11 ['0b0', '0b1', '0b11', '0b10']

x ^ (x >> 1) is the Gray code. For 13 (1101) it is 1101 ^ 0110 = 1011, which is 11. The first four Gray codes are 0, 1, 3 and 2.

More Python Programming questions

Exams that ask this topic