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

Discussion

Bitwise & can be used to check if more than one bit in a number is on.

  • A.True
  • B.False

Answer: A

Correct answer: True

AND with a mask isolates exactly the bits the mask selects, and a mask may name as many positions as wanted. To test whether any of several bits is set, OR the masks together and AND once: if (n & (BIT2 | BIT5)) is true if either is on.

Testing that all of them are on needs a comparison rather than a mere test: if ((n & mask) == mask). That distinction - non-zero means at least one, equality means all - is the practical point behind the question.

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

Post your comment

Please log in or register to post a comment.