Register bit manipulation patterns in Embedded C - Time & Space Complexity
When working with register bit manipulation, it is important to understand how the number of operations changes as we manipulate bits.
We want to know how the time to set, clear, or toggle bits grows with the number of bits involved.
Analyze the time complexity of the following code snippet.
// Set bits in a register based on a mask
void set_bits(volatile unsigned int *reg, unsigned int mask) {
*reg |= mask;
}
// Clear bits in a register based on a mask
void clear_bits(volatile unsigned int *reg, unsigned int mask) {
*reg &= ~mask;
}
// Toggle bits in a register based on a mask
void toggle_bits(volatile unsigned int *reg, unsigned int mask) {
*reg ^= mask;
}
This code sets, clears, or toggles bits in a hardware register using bitwise operations with a mask.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Single bitwise operation (OR, AND, XOR) on the register and mask.
- How many times: Exactly once per function call, no loops or repeated steps.
Each operation works on the entire register at once, regardless of how many bits are set in the mask.
| Input Size (bits in mask) | Approx. Operations |
|---|---|
| 8 | 1 |
| 16 | 1 |
| 32 | 1 |
Pattern observation: The time to perform the operation stays the same no matter how many bits are involved.
Time Complexity: O(1)
This means the operation takes the same amount of time no matter how many bits you change.
[X] Wrong: "Changing more bits means the operation takes longer because it has to handle each bit separately."
[OK] Correct: Bitwise operations work on the whole register at once, so the number of bits set in the mask does not affect the time.
Understanding that bitwise register operations run in constant time helps you reason about embedded system performance and write efficient low-level code.
"What if we used a loop to set or clear bits one by one instead of a single bitwise operation? How would the time complexity change?"
Practice
Example: Set bit 3 in register REGSolution
Step 1: Understand setting a bit
Setting a bit means making that bit 1 without changing others.Step 2: Use bitwise OR
Bitwise OR with 1 at the target bit sets it to 1, others stay same.Final Answer:
Bitwise OR (|) -> Option AQuick Check:
Set bit = OR [OK]
- Using AND (&) to set bits (it clears bits)
- Using XOR (^) which toggles bits
- Using NOT (~) which inverts all bits
REG?Solution
Step 1: Understand clearing a bit
Clearing means setting the bit to 0 without changing others.Step 2: Use AND with NOT mask
AND with NOT of (1 shifted to bit 2) clears bit 2.Final Answer:
REG = REG & ~(1 << 2); -> Option DQuick Check:
Clear bit = AND with NOT mask [OK]
- Using OR (|) which sets bits
- Using XOR (^) which toggles bits
- Using shift (<<) alone which moves bits
REG = 0b00001100;, what is the value of REG after executing REG ^= (1 << 3);?Solution
Step 1: Understand XOR toggling
XOR toggles the bit: if 1 becomes 0, if 0 becomes 1.Step 2: Toggle bit 3 of REG
Bit 3 is currently 1 (0b00001100). Toggling it changes it to 0, so REG becomes 0b00000100.Final Answer:
0b00000100 -> Option BQuick Check:
XOR toggles bit 3 from 1 to 0 [OK]
- Thinking XOR sets bit instead of toggling
- Confusing bit positions (count from 0)
- Not converting binary correctly
REG is set?if (REG & (1 << 4) == 1) {
// bit 4 is set
}Solution
Step 1: Understand bit check expression
REG & (1 << 4) returns non-zero if bit 4 is set, zero if not.Step 2: Fix comparison
Comparing to 1 is wrong because bit 4 corresponds to value 16, not 1. Should check if result != 0.Final Answer:
The comparison '== 1' is incorrect; it should check if result is non-zero. -> Option AQuick Check:
Check bits by testing non-zero result [OK]
- Comparing bit check result to 1 instead of non-zero
- Using shift right (>>) instead of left (<<)
- Using OR (|) to check bits which sets bits
REG simultaneously. Which code correctly does this?Solution
Step 1: Understand toggling multiple bits
Toggling means flipping bits. XOR (^) with mask flips bits where mask has 1.Step 2: Create mask for bits 0, 2, and 5
Mask is (1<<0) | (1<<2) | (1<<5) which sets bits 0, 2, and 5 to 1.Step 3: Apply XOR with mask
REG ^= mask flips those bits simultaneously.Final Answer:
REG ^= (1 << 0) | (1 << 2) | (1 << 5); -> Option CQuick Check:
Toggle bits with XOR and combined mask [OK]
- Using AND with NOT mask which clears bits
- Using AND (&) instead of OR (|) to combine masks
- Using wrong bit positions in mask
Ready to prove it?
Pass the test and this pattern moves unknown
