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

Discussion

Which bitwise operator is suitable for turning off a particular bit in a number?

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

Answer: B

Correct answer: & operator

Turning a bit off means forcing it to 0 while leaving the others untouched, and only AND can do that: \( b \mathbin{\&} 0 = 0 \) for any \( b \), while \( b \mathbin{\&} 1 = b \).

The idiom is therefore n &= ~(1 << k) - build a mask with a single 1 in position \( k \), invert it so that position alone is 0, and AND. && and || are logical operators yielding only 0 or 1 and destroying the other bits entirely; ! is logical negation of the whole value.

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

Post your comment

Please log in or register to post a comment.