Data Structures & AlgorithmsHardTCS NQT Digital · bit operations

Problem: What does the following print?

Problem: What does the following print?
x = 5
print(x << 3 | x >> 1, x & 6, x ^ 3)
  1. A. 40 4 6
  2. B. 42 4 6
  3. C. 42 0 6
  4. D. 40 2 6

Correct answer: B. 42 4 6

5 << 3 = 40 and 5 >> 1 = 2, and 40 | 2 = 42 (the | binds weaker than << so the first term is 40 | 2). 5 & 6 = 4 and 5 ^ 3 = 6. Operator precedence is the key trap.

More Coding Interview Problems questions

Exams that ask this topic