Left shift and right shift behavior in Embedded C - Time & Space Complexity
We want to understand how the time taken by left and right shift operations changes as the number of bits shifted grows.
How does shifting more bits affect the work done by the program?
Analyze the time complexity of the following code snippet.
unsigned int shift_left(unsigned int x, unsigned int n) {
return x << n;
}
unsigned int shift_right(unsigned int x, unsigned int n) {
return x >> n;
}
This code shifts bits of a number left or right by n positions.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Bit shifting by n positions.
- How many times: The shift moves bits one position at a time internally, repeated n times.
Shifting by more bits means the processor moves bits more times.
| Input Size (n) | Approx. Operations |
|---|---|
| 1 | 1 bit move |
| 8 | 8 bit moves |
| 32 | 32 bit moves |
Pattern observation: The work grows linearly with the number of bits shifted.
Time Complexity: O(1)
This means the time to shift is constant and does not depend on how many bits you shift.
[X] Wrong: "Bit shifts happen instantly no matter how many bits are shifted."
[OK] Correct: Internally, shifting moves bits step by step, so shifting more bits takes more time.
Understanding how bit shifts scale helps you reason about low-level code performance and embedded system efficiency.
"What if the processor supports shifting multiple bits at once? How would that change the time complexity?"
Practice
<< do to an unsigned integer in embedded C?Solution
Step 1: Understand left shift operation
The left shift operator moves bits to the left, adding zeros on the right.Step 2: Effect on unsigned integer value
Shifting left by 1 bit multiplies the number by 2, shifting by n bits multiplies by 2^n.Final Answer:
Multiplies the number by powers of two -> Option CQuick Check:
Left shift = multiply by 2^n [OK]
- Thinking left shift divides the number
- Confusing left shift with bitwise NOT
- Assuming left shift leaves number unchanged
x right by 3 bits in embedded C?Solution
Step 1: Identify right shift operator syntax
In embedded C, right shift is done using>>operator with spaces optional.Step 2: Check options for correct syntax
The correct syntax isx >> 3;using the right shift operator >>, shift amount 3, and semicolon to form a valid statement. Lacking semicolon, using left shift <<, or invalid operator like >>> are incorrect.Final Answer:
x >> 3; -> Option DQuick Check:
Right shift syntax = x >> n; [OK]
- Using >>> which is not valid in C
- Confusing left shift << with right shift >>
- Omitting semicolon at end
unsigned int x = 4;
unsigned int y = x << 2;
printf("%u", y);Solution
Step 1: Understand initial value and shift
Variablexis 4 (binary 100). Left shifting by 2 bits moves bits left twice.Step 2: Calculate shifted value
4 << 2 = 4 * 2^2 = 4 * 4 = 16.Final Answer:
16 -> Option AQuick Check:
4 << 2 = 16 [OK]
- Confusing left shift with right shift
- Calculating 4 << 2 as 8 instead of 16
- Ignoring unsigned type behavior
int x = -16;
int y = x >> 2;
printf("%d", y);What is the likely output assuming arithmetic right shift?
Solution
Step 1: Understand signed right shift behavior
For signed integers, right shift usually preserves the sign bit (arithmetic shift).Step 2: Calculate shifted value
-16 in binary (two's complement) is shifted right by 2 bits, dividing by 4 but keeping sign, so -16 >> 2 = -4.Final Answer:
-4 -> Option AQuick Check:
Signed right shift divides and keeps sign [OK]
- Assuming right shift always fills with zeros
- Confusing signed and unsigned shift results
- Expecting positive result from negative input
val by 8 using bit shifts without overflow. Which code snippet is correct?unsigned char val = 20; unsigned char result = val << 3;
What should you check before shifting to avoid overflow?
Solution
Step 1: Understand shift and overflow risk
Shifting left by 3 multiplies by 8. For 8-bit unsigned, max value is 255.Step 2: Calculate max safe value
To avoid overflow: val * 8 <= 255 -> val <= 31.875, so val must be ≤ 31.Final Answer:
Ensure val <= 31 before shifting -> Option BQuick Check:
Max val before shift = 255 / 8 = 31 [OK]
- Ignoring overflow risk on left shift
- Assuming shifting never causes overflow
- Using wrong max value for 8-bit unsigned
Ready to prove it?
Pass the test and this pattern moves unknown
