Difficulty: Easy
Correct Answer: Correct
Explanation:
Introduction / Context:
Testing individual bits is a foundational skill in systems programming, device drivers, embedded development, and competitive programming. The classic approach is to use the bitwise AND operator with a mask that has the target bit set to 1 and all other bits 0.
Given Data / Assumptions:
Concept / Approach:
The expression (x & mask) != 0 evaluates to true if and only if the corresponding bit in x is 1. This works because AND preserves a 1 only where both operands have a 1. It is completely independent of endianness, as endianness affects byte ordering in memory, not the logical interpretation of bit positions within an arithmetic expression.
Step-by-Step Solution:
Verification / Alternative check:
For x = 10 (1010b) and k = 1, mask = 0010b; x & mask = 0010b != 0 → bit 1 is set. For k = 0, mask = 0001b; x & mask = 0000b → bit 0 is not set.
Why Other Options Are Wrong:
Common Pitfalls:
Using signed shifts incorrectly; forgetting to use unsigned types when shifting to build masks.
Final Answer:
Correct.
Discussion & Comments