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

C# Programming Operators Difficulty: Easy
Choose an option
  • A
    The | operator can be used to put OFF a bit.
  • B
    The | operator can be used to invert a bit.
  • C
    The | operator can be used to check whether a bit is ON.
  • D
    The | operator can be used to check whether a bit is OFF.
  • E
    The | operator can be used to put ON a bit.

Answer

Correct Answer: The | operator can be used to put ON a bit.

Explanation

Introduction / Context:This question covers the bitwise OR operator in C#. It is often used in low-level programming to manipulate individual bits.

Given Data / Assumptions:

  • Operator: | (bitwise OR).
  • Operation compares each bit position of two integers.

Concept / Approach:In bitwise OR, any bit position where at least one operand has 1 results in 1. This is often used to set (turn ON) bits in a bitmask.

Step-by-Step Solution:

Binary example: 0101 | 0010 = 0111. Bit 2 was turned ON. Thus OR ensures a bit becomes 1 if at least one operand has 1.

Verification / Alternative check:Apply OR with a mask: value | 0x04 sets bit 2 regardless of its previous state.

Why Other Options Are Wrong:| does not turn OFF, invert, or directly test bits. It only sets ON bits when combined with 1.

Common Pitfalls:Confusing | with & (AND) or ^ (XOR).

Final Answer:The | operator can be used to put ON a bit.

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