Difficulty: Easy
Correct Answer: if ((n & 8) == 8) Console.WriteLine('Fourth bit is ON');
Explanation:
Introduction / Context:
Testing whether a particular bit is set is a standard bitwise task. Here we need to test the 4th bit from the right (value 8) of a byte variable n.
Given Data / Assumptions:
Concept / Approach:
To test if a bit is ON, AND the value with a mask that has 1 at that bit. If the result equals the mask, the bit is set. Therefore, use mask 8 and compare with 8.
Step-by-Step Solution:
Verification / Alternative check:
Try n = 13 (00001101). (n & 8) == 8 is true; try n = 5 (00000101), also true; try n = 1, false.
Why Other Options Are Wrong:
Common Pitfalls:
Mixing up XOR and AND for bit testing; confusing 0-based with 1-based bit positions.
Final Answer:
if ((n & 8) == 8) Console.WriteLine('Fourth bit is ON');
Discussion & Comments