ProgrammingVery hardTCS NQT · Infosys

Which expression tests whether bit k of an unsigned x is set?

  1. A. x & k != 0
  2. B. (x >> k) & 1
  3. C. x >> k == 2
  4. D. x & (1 << k) == 1

Correct answer: B. (x >> k) & 1

Shifting right moves bit k to position 0, and masking with 1 isolates it. The x & (1 << k) == 1 form is wrong because == binds tighter than &, so it compares 1 << k with 1.

More C Programming questions

Exams that ask this topic