Difficulty: Easy
Correct Answer: A-4, B-2, C-3, D-1
Explanation:
Introduction / Context:
Bitwise operators in C manipulate individual bits of integers. They are indispensable in embedded systems, device drivers, networking stacks, and performance-critical code where flags and masks are prevalent. This match-the-following tests recognition of the core operators and their semantics.
Given Data / Assumptions:
Concept / Approach:
The tilde (~) inverts every bit (one's complement). The ampersand (&) performs bitwise AND, setting a bit only if it is 1 in both operands. The vertical bar (|) performs bitwise OR, setting a bit if it is 1 in either operand. The caret (^) performs bitwise XOR, setting a bit if operands differ at that position.
Step-by-Step Solution:
Verification / Alternative check:
Quick test with an 8-bit example: let x = 0b10101010 and y = 0b11001100. Then ~x = 0b01010101, x & y = 0b10001000, x | y = 0b11101110, x ^ y = 0b01100110. These results align with the mapped meanings.
Why Other Options Are Wrong:
Common Pitfalls:
Confusing bitwise operators (&, |, ^) with their logical counterparts (&&, ||). Also, forgetting that ~ on signed integers is defined via two's complement representation and can produce negative values.
Final Answer:
A-4, B-2, C-3, D-1
Discussion & Comments