Left shift and right shift in C - Time & Space Complexity
We want to see how fast the program runs when using left and right shift operations.
How does the number of steps change when we shift bits in a number?
Analyze the time complexity of the following code snippet.
int leftShift(int x, int n) {
return x << n;
}
int rightShift(int x, int n) {
return x >> n;
}
This code shifts the bits of an integer left or right by n positions.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Bit shifting done by the CPU in one step.
- How many times: The shift happens once per function call, no loops involved.
Shifting bits is done in a single CPU instruction regardless of how many bits we shift.
| Input Size (n) | Approx. Operations |
|---|---|
| 1 | 1 operation |
| 10 | 1 operation |
| 100 | 1 operation |
Pattern observation: The time to shift does not grow with the number of bits shifted.
Time Complexity: O(1)
This means shifting bits takes the same amount of time no matter how many bits you shift.
[X] Wrong: "Shifting by more bits means the program takes longer to run because it moves each bit one by one."
[OK] Correct: The CPU does the shift in one step, not bit by bit, so the time stays the same.
Understanding that bit shifts are constant time helps you explain how low-level operations work efficiently in real programs.
"What if we replaced the shift operations with a loop that shifts one bit at a time? How would the time complexity change?"
Practice
<< do to an integer in C?Solution
Step 1: Understand left shift operation
The left shift operator moves all bits in the number to the left by the specified count.Step 2: Effect on the number's value
Each left shift multiplies the number by 2, so shifting left by n multiplies by 2^n.Final Answer:
Moves bits to the left, multiplying the number by powers of two -> Option AQuick Check:
Left shift = multiply by 2^n [OK]
- Confusing left shift with right shift
- Thinking left shift divides the number
- Assuming left shift reverses bits
x right by 3 bits in C?Solution
Step 1: Identify the right shift operator in C
The right shift operator is>>and requires spaces or no spaces around it.Step 2: Check syntax correctness
x >> 3;is correct syntax.x << 3;is left shift,x >>> 3;is invalid in C, andx >>3is missing a semicolon.Final Answer:
x >> 3; -> Option BQuick Check:
Right shift syntax = x >> n; [OK]
- Using >>> which is not valid in C
- Confusing left shift << with right shift >>
- Omitting semicolon at end
int x = 4;
int y = x << 2;
printf("%d", y);Solution
Step 1: Understand the initial value of x
x is 4, which in binary is 00000100.Step 2: Apply left shift by 2 bits
Shifting left by 2 bits multiplies 4 by 2^2 = 4 * 4 = 16.Final Answer:
16 -> Option DQuick Check:
4 << 2 = 16 [OK]
- Calculating 4 << 2 as 8 instead of 16
- Confusing left shift with right shift
- Ignoring the shift count
int a = 32;
int b = a >> 2
printf("%d", b);Solution
Step 1: Check syntax line by line
The lineint b = a >> 2is missing a semicolon at the end.Step 2: Verify other parts
The operator >> is correct, printf uses %d for int, and b is declared properly.Final Answer:
Missing semicolon after right shift operation -> Option CQuick Check:
Every statement needs semicolon [OK]
- Assuming >> is invalid operator
- Thinking printf format is wrong
- Ignoring missing semicolon errors
int x = 1;, what is the value of y after this operation?int y = (x << 3) >> 1;
Solution
Step 1: Calculate left shift (x << 3)
Left shifting 1 by 3 bits multiplies 1 by 2^3 = 8.Step 2: Calculate right shift of result by 1
Right shifting 8 by 1 bit divides 8 by 2^1 = 2, so 8 / 2 = 4.Final Answer:
4 -> Option AQuick Check:
(1 << 3) >> 1 = 8 >> 1 = 4 [OK]
- Calculating (1 << 3) as 16
- Ignoring right shift effect
- Mixing up shift directions
Ready to prove it?
Pass the test and this pattern moves unknown
