Bit Manipulation Basics AND OR XOR NOT Left Right Shift in DSA C - Time & Space Complexity
Bit manipulation operations are very fast and work directly on the bits of numbers.
We want to understand how the time needed changes as the size of the input number grows.
Analyze the time complexity of the following code snippet.
unsigned int bitOps(unsigned int x, unsigned int y) {
unsigned int a = x & y; // AND
unsigned int b = x | y; // OR
unsigned int c = x ^ y; // XOR
unsigned int d = ~x; // NOT
unsigned int e = x << 1; // Left shift
unsigned int f = y >> 1; // Right shift
return a + b + c + d + e + f;
}
This code performs basic bitwise operations on two unsigned integers.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Each bitwise operation processes all bits of the input numbers.
- How many times: The operations run once each, but internally they handle each bit of the numbers.
Each bitwise operation touches every bit of the input numbers.
| Input Size (bits) | Approx. Operations |
|---|---|
| 8 | About 8 steps per operation |
| 32 | About 32 steps per operation |
| 64 | About 64 steps per operation |
Pattern observation: The time grows linearly with the number of bits in the input.
Time Complexity: O(b)
This means the time grows linearly with the number of bits in the input numbers.
[X] Wrong: "Bitwise operations take constant time no matter the input size."
[OK] Correct: While bitwise operations are very fast, they actually process each bit, so the time depends on the number of bits in the input.
Understanding bit manipulation time helps you write efficient low-level code and reason about performance in systems programming and algorithms.
"What if we used a loop to apply bitwise operations on an array of n numbers? How would the time complexity change?"
Practice
5 and 3? (5 = 0101, 3 = 0011 in binary)Solution
Step 1: Convert numbers to binary
5 in binary is 0101, and 3 in binary is 0011.Step 2: Perform bitwise AND operation
0101 AND 0011 = 0001 (only bits set in both numbers remain set).Final Answer:
1 -> Option BQuick Check:
5 & 3 = 1 [OK]
- Confusing AND with OR operation
- Not converting decimal to binary correctly
- Mixing bit positions during calculation
x by 2 bits in C?Solution
Step 1: Understand left shift operator in C
The left shift operator is written as << and shifts bits to the left.Step 2: Check syntax correctness
x << 2; usesx << 2;which correctly shifts bits of x left by 2 positions.Final Answer:
x << 2; -> Option CQuick Check:
Left shift syntax = x << 2; [OK]
- Using >>> which is invalid in C
- Confusing left shift with right shift
- Incorrect operator placement or syntax
int x = 6; // binary 0110
int y = x ^ 3; // 3 = 0011
printf("%d", y);Solution
Step 1: Convert numbers to binary and apply XOR
6 in binary is 0110, 3 is 0011. XOR operation: 0110 ^ 0011 = 0101.Step 2: Calculate decimal value of XOR result
0101 in binary equals 5 decimal.Final Answer:
5 -> Option DQuick Check:
6 ^ 3 = 5 [OK]
- Mixing XOR with OR or AND
- Incorrect binary conversion
- Misreading bit positions
int x = 8;
int y = ~x << 1;
printf("%d", y);Solution
Step 1: Understand operator precedence
The bitwise NOT (~) has higher precedence than shift (<<), so ~x is evaluated first.Step 2: Check if parentheses needed
Without parentheses, ~x is computed, then shifted left. This is correct but may cause confusion if intended differently.Step 3: Identify common mistake
If intention was to shift x first then NOT, parentheses are needed. Otherwise, no syntax error but logic may be wrong.Final Answer:
Using ~ operator without parentheses causes wrong shift -> Option AQuick Check:
Operator precedence matters; use parentheses [OK]
- Ignoring operator precedence
- Assuming shift applies before NOT
- Thinking code has syntax error when it doesn't
n, which expression correctly checks if the 3rd bit (from right, zero-based) is set (1)?Solution
Step 1: Understand bit position and mask
The 3rd bit (zero-based) means bit at position 3. Mask is 1 shifted left by 3: (1 << 3) = 8.Step 2: Check if bit is set using AND
Using (n & (1 << 3)) checks if that bit is 1. If result is not zero, bit is set.Final Answer:
(n & (1 << 3)) != 0 -> Option AQuick Check:
Bit check uses AND with mask [OK]
- Using OR or XOR instead of AND for checking
- Wrong bit position shift
- Comparing shifted bit to zero incorrectly
Ready to prove it?
Pass the test and this pattern moves unknown
