LearnFrenzy
Sign in Take a free test

Bitwise Operators · Bitwise Operators MCQ · Question 3

How to test whether bit k is set in C Answer, explanation & discussion

  • MCQ
  • Easy
  • 0 comments
  • Answer explained
  • Moderated community
Question 3 Back to Exercise

Which expression is 1 when bit k of an unsigned x is set, and 0 otherwise?

  • A x & k
  • B (x >> k) & 1u Correct
  • C x | (1u << k)
  • D x ^ k
Correct Answer: Option B

Shifting right by k brings bit k to position 0, and & 1u keeps only that bit. x & (1u << k) also works as a true/false test, but gives the bit's value (such as 8), not 1.

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links