Bitwise AND, OR, XOR in C - Time & Space Complexity
We want to understand how the time it takes to do bitwise AND, OR, and XOR changes as the size of the numbers grows.
How does the number of steps grow when we work with bigger numbers?
Analyze the time complexity of the following code snippet.
unsigned int bitwise_operations(unsigned int a, unsigned int b) {
unsigned int and_result = a & b;
unsigned int or_result = a | b;
unsigned int xor_result = a ^ b;
return and_result + or_result + xor_result;
}
This code performs bitwise AND, OR, and XOR on two unsigned integers and returns the sum of the results.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Bitwise operations on each bit of the input numbers.
- How many times: Once per bit in the numbers (usually fixed, like 32 or 64 bits).
Each bit in the input numbers is processed once. If the number size grows, the steps grow linearly with the number of bits.
| Input Size (bits) | Approx. Operations |
|---|---|
| 8 | 8 steps |
| 32 | 32 steps |
| 64 | 64 steps |
Pattern observation: The number of steps grows directly with the number of bits in the input numbers.
Time Complexity: O(n)
This means the time to do bitwise AND, OR, and XOR grows linearly with the number of bits in the input numbers.
[X] Wrong: "Bitwise operations are instant and take the same time no matter the number size."
[OK] Correct: While bitwise operations are very fast, they still process each bit. Larger numbers with more bits take proportionally more steps.
Understanding how bitwise operations scale helps you reason about low-level code efficiency and is a useful skill in many programming tasks.
"What if we used bitwise operations on arrays of numbers instead of single numbers? How would the time complexity change?"
Practice
a & b means:Solution
Step 1: Understand bitwise AND operation
Bitwise AND compares each bit of two numbers and returns 1 only if both bits are 1.Step 2: Compare with other operators
Bitwise OR returns 1 if either bit is 1, XOR returns 1 if bits differ, and logical AND is different (works on boolean values).Final Answer:
Bitwise AND -> Option AQuick Check:
AND = & = Bitwise AND [OK]
- Confusing bitwise AND with logical AND (&&)
- Mixing AND with OR or XOR operators
- Thinking bitwise AND returns 1 if either bit is 1
a and b in C?Solution
Step 1: Identify bitwise XOR operator
In C, the caret symbol (^) is used for bitwise XOR operation.Step 2: Differentiate from other operators
&& is logical AND, | is bitwise OR, & is bitwise AND, so only ^ is XOR.Final Answer:
a ^ b -> Option AQuick Check:
^ means XOR in C [OK]
- Using && instead of ^ for XOR
- Confusing | (OR) with ^ (XOR)
- Using & (AND) instead of ^ (XOR)
int a = 6; // binary 0110
int b = 11; // binary 1011
printf("%d", a & b);Solution
Step 1: Convert numbers to binary
6 in binary is 0110, 11 in binary is 1011.Step 2: Perform bitwise AND
0110 & 1011 = 0010 (only bits where both are 1 remain 1).Step 3: Convert result back to decimal
0010 in decimal is 2.Final Answer:
2 -> Option DQuick Check:
6 & 11 = 2 [OK]
- Confusing AND with OR or XOR results
- Not converting binary back to decimal correctly
- Mixing up bit positions
int a = 5;
int b = 9;
int c = a | b;
printf("%d", c);Solution
Step 1: Check syntax correctness
All lines have semicolons; operator | is correct for bitwise OR.Step 2: Verify printf usage
%d is correct to print integer decimal value; no error present.Final Answer:
No error, code will print correct output -> Option CQuick Check:
Bitwise OR uses | and %d prints decimal [OK]
- Thinking | is wrong operator for OR
- Believing %x is mandatory for bitwise results
- Missing semicolons (not the case here)
x = 12 and y = 10, which expression toggles the bits of x where y has bits set, using bitwise XOR?Solution
Step 1: Understand toggling bits with XOR
Bitwise XOR toggles bits where the second operand has 1s.Step 2: Analyze each option
x & y keeps bits set in both; x | y sets bits set in either; ~x ^ y is unrelated toggling with complement.Final Answer:
x ^ y -> Option BQuick Check:
XOR toggles bits where second operand has 1 [OK]
- Using AND or OR instead of XOR for toggling
- Confusing complement operator ~ with XOR
- Not understanding toggling effect of XOR
Ready to prove it?
Pass the test and this pattern moves unknown
