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

Discussion

Bitwise & can be used in conjunction with ~ operator to turn off 1 or more bits in a number.

  • A.Yes
  • B.No

Answer: A

Correct answer: Yes

This is the standard idiom for clearing bits. ~ inverts a mask so that the positions of interest hold 0 and everything else holds 1; AND then forces those positions to 0 and leaves the rest untouched.

So n &= ~0x02 clears bit 1, and n &= ~(0x02 | 0x08) clears two bits at once - the technique extends to as many as the mask names. It is the exact counterpart of n |= mask for setting bits, and the pair covers most bit manipulation in practice.

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

Post your comment

Please log in or register to post a comment.