C#.NET — Which of the following is correct about the Bitwise XOR operator (^) ?

Difficulty: Easy

Correct Answer: The ^ operator can be used to invert a bit.

Explanation:


Introduction / Context:
This question deals with the XOR (exclusive OR) bitwise operator in C#.



Given Data / Assumptions:

  • Operator: ^ (bitwise XOR).
  • Rule: 0 ^ 1 = 1, 1 ^ 1 = 0.


Concept / Approach:
XOR flips bits where the other operand has 1. This property allows toggling (inverting) selected bits.



Step-by-Step Solution:

1010 ^ 1111 = 0101 → every bit inverted. Using XOR with a mask 1 inverts those positions.


Verification / Alternative check:
For bit toggle: value ^ 0xFF inverts all bits of value.



Why Other Options Are Wrong:
XOR does not always set ON or OFF; it toggles depending on mask. It is not used for bit testing directly.



Common Pitfalls:
Confusing XOR with OR. XOR is for toggling, not just turning bits ON.



Final Answer:
The ^ operator can be used to invert a bit.

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion