Machine-translated by Google. Questions are authored in English.
Top

Discussion

Which bitwise operator is suitable for checking whether a particular bit is on or off?

  • A.&& operator
  • B.& operator
  • C.|| operator
  • D.! operator

Answer: B

Correct answer: & operator

Testing a bit means isolating it and discarding everything else, which is what AND with a single-bit mask does: n & (1 << k) is non-zero if bit \( k \) is set and zero if it is not.

Note that the non-zero result is \( 2^k \) rather than 1, so a comparison with 1 would fail for any bit but the lowest - test against zero, or shift the result down. The same operator serves for clearing bits, with an inverted mask; the difference is whether the result is stored back or merely tested.

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.